zintani Posted September 26, 2011 Share Posted September 26, 2011 Hi, I have got two files, each of which is a text document. The first one has a bunch of words while the second one has a proper text document properties (talks about something). I would like to check the second file against the first one to see how many words of the file one mentioned in the second file. For example, File one: ('see', 'read', 'study', 'China', 'cloudy', 'hungry', 'answer'.) File two:( I went back home to see my family as I was studying in China. By the time I arrived home I was so hungry and the weather was cloudy). The output should be see read study cloudy hungry answer China Yes No Yes Yes Yes NO Yes where all the yes's mean the word did appear in the second file, nos mean the word didn't appear. Any suggestions please or any guidance on how to do it. I tried to use count but I am not good at loops, it makes my brain looping in a closed loop. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/247893-read-file-two-and-see-how-many-words-occur/ Share on other sites More sharing options...
requinix Posted September 26, 2011 Share Posted September 26, 2011 Here's something to get you started. $file = "I went back home to see my family as I was studying in China. By the time I arrived home I was so hungry and the weather was cloudy."; $words = array("see", "reader", "study", "China", "cloudy", "hungry", "answer"); preg_match_all('/\b(' . implode("|", array_map("preg_quote", $words)) . ')/i', $file, $foundwords); print_r($foundwords[0]); Note that you'll get false positives with words like again/against, but you will get study/studying. Quote Link to comment https://forums.phpfreaks.com/topic/247893-read-file-two-and-see-how-many-words-occur/#findComment-1272963 Share on other sites More sharing options...
.josh Posted September 27, 2011 Share Posted September 27, 2011 Note that you'll get false positives with words like again/against, but you will get study/studying. IMO those shouldn't count as the same. OP: Are you sure you want something like "studying" to count for "study"? If so, there's no real easy way around that, other than to allow for false positives (like requinix's example), unless you basically make a lookup table of all tenses/forms of a given word to check for, in addition to the actual word you're looking for. Anyways, here is a non-regex approach that may or may not be faster... (which also allows for false positives) $file = "I went back home to see my family as I was studying in China. By the time I arrived home I was so hungry and the weather was cloudy."; $words = array("see", "reader", "study", "China", "cloudy", "hungry", "answer"); foreach ($words as $word) { $results[$word] = (stripos($file, $word)!==false) ? 'yes' : 'no'; } // print out results print_r($results); output: Array ( [see] => yes [reader] => no [study] => yes [China] => yes [cloudy] => yes [hungry] => yes [answer] => no ) Quote Link to comment https://forums.phpfreaks.com/topic/247893-read-file-two-and-see-how-many-words-occur/#findComment-1273059 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.