i have a db with 3 tables review, pos and neg words for a film review im trying to detect if the word 'not' appears before a positive or negative word then add 1 to the pos count if it appears before a neg word ie this was not bad, and add 1 to the neg count if it appears before a pos word i.e this was not good

currently i have this method but it only seems to detect the not before a word once, how can i get it to detect through the whole text for example if i had, the film was not great, the acting was not good but it was not bad, the pos count should = 1 and neg count should = 2

Code blocks are created by indenting at least 4 spaces
... and can span multiple lines
$find = $review_text;
if (preg_match("/(?<=not) $negwords/i", $find)) 
   {
    echo $good++;
    }
if (preg_match("/(?<=not) $poswords/i", $find)) 
    {
    echo $bad++;
    }

i have tried that and it doesnt seem to work, also it requires 3 values

What have you tried? Something like this should work:

$count = preg_match_all("/(?<=not) $negwords/i", $find);
if ($count > 0)
    $good += $count;

all i did was change preg match to preg match all

it says that there needs to be 3 values in the parameter

You can try:

$count = preg_match_all("/(?<=not) $negwords/i", $find, $matches);

still isnt working if i try it on not good not bad not great im geetting 3 for both the good and bad count

what ive noticed is its getting the count for the number of not then words that appear and not identifying the pos or neg word

the words are stored on a db so for pos and neg i have

Code blocks are created by indenting at least 4 spaces
... and can span multiple lines
$pos = mysql_query("SELECT word FROM positive");
$neg = mysql_query("SELECT word FROM negative");

$poswords = $pos['word'];
$negwords = $neg['word'];

Okay, I was thrown off by the plural form. Have you tried:

preg_match_all("/(?<=not) $negwords/i", $find, $matches);
count($matches[0]);

And why do you use the look-behind, you should just be able to use not the following should work the same:

preg_match_all("/not $negwords/i", $find, $matches);
count($matches[0]);

You may need to include some boundary checks though.

ok thanks i think the problem is getting the pos and neg words

i will give that a try andlet you know

i dont understand what the method does from count matches how does it do the count

is there anything else that i can try ?

how would i get the words or review in to an array ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.