Jump to content

read file two and see how many words occur


Recommended Posts

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.

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.

 

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
)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.