0

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++;
    }
2
Contributors
16
Replies
17
Views
5 Years
Discussion Span
Last Post by cr7489
0

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

0

What have you tried? Something like this should work:

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

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

0

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

0

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'];
0

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.

Edited by pritaeas: Addition.

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.