tags:

views:

89

answers:

4

I have an array of phrases (max 2 words) like

$words = array('barack obama', 'chicago', 'united states');

and then I have a string like:

$sentence = "Barack Obama is from Chicago. Barack Obama's favorite food it pizza.";

I want to find/create an efficient algorithm that would return the number of occurrences of the words in the array $words in the string $sentence. In this case it would be:

'barack obama' => 2
'chicago' => 0

How can I built this?

A: 

something like this would do it.

$res = array();
foreach($words as $word){
  $res[$word] = preg_match_all("/{$word}/i", $sentence);
}

note: since it's using regular expression you have to make sure your word don't have regular expression symbols and escape them, also a solution based on str_pos might perform better so it depends the number of sentence you have to analyze and number of words involved.

using @Ofri solution

$res = array();
foreach($words as $word){
  $res[$word] = substr_count($sentence,$word);
}
RageZ
Do you need a case insensitive flag in there for the preg_match_all statement?
Doug Neiner
Why regex? he's just looking for words occurrences. its a waste of CPU.
Ofri Raviv
@Sean: the solution of @ofri using `substr_count` seems better. I wasn't aware of that function ;-)
RageZ
+6  A: 

Read the docs about substr_count. Use it in a loop over $words.

 $res = array();
 foreach($words as $word){
    $res[$word] = substr_count($sentence,$word);
 }
Ofri Raviv
+1. would be useful for asker to added a sample, besides the link.
seanmonstar
+2  A: 

This is known as entity extraction in Natural Language Processing. It may look simple in your example but it can grow quite complex. If you are going to be using it seriously you should consider looking at toolkits which do this such as NLTK, OpenNLP and Lucene.

peter.murray.rust
A: 

Here's another regex implementation:

$words = array('barack obama', 'chicago', 'united states');
$sentence = "Barack Obama is from Chicago. Barack Obama's favorite food it pizza. He is president of the United States";
$re= sprintf('/(%s)/i', implode('|',  $words));
if (preg_match_all($re, $sentence, $m))
 print_r(array_count_values($m[0]));

Easy to extend - just update $words and $sentence with whatever you want.

pygorex1