php does word appear before another word
hi, i am trying to write a method that looks for the word 'not' before another word and these words i.e being positive or negative are stored in my database so for example this is not good, the problem im having is getting the word from my DB to be identified
$pos = mysql_query("SELECT word FROM positive");
$neg = mysql_query("SELECT word FROM negative");
$poswords = $pos['word'];
$negwords = $neg['word'];
$find = $review_text;
if (preg_match("/(?<=not) $negwords/i", $find))
{
echo $good++;
}
if (preg_match("/(?<=not) $poswords/i", $find))
{
echo $bad++;
}
cr7489
Junior Poster in Training
53 posts since Dec 2011
Reputation Points: 25
Solved Threads: 0
Skill Endorsements: 0
Is it always not?
Could it be "isn't", "aren't"
diafol
Keep Smiling
10,655 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,513
Skill Endorsements: 57
for what i am trying to do i need to use 'not' but other words will be bought in later i.e very
cr7489
Junior Poster in Training
53 posts since Dec 2011
Reputation Points: 25
Solved Threads: 0
Skill Endorsements: 0
The reason I was asking is that if you want a simple solution for not - fine. But if you want something a little more flexible, contributors need to know otherwise a lot of time could be wasted.
Don't think your code will work.
How many good / bad words do you have? This may determine which method of attack to employ.
diafol
Keep Smiling
10,655 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,513
Skill Endorsements: 57
Your preg works just fine if the variable holds a single word. I think we discussed this in another thread. Are you sure it's not caused by something else?
pritaeas
Posting Prodigy
9,293 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,462
Skill Endorsements: 86
I made a demo for you. It counts right in my test, 4 negative, 2 positive. Hope this helps:
$subject = 'This weird move is bad bad bad, not awful, funny but not good.';
$negWords = array ('bad', 'awful');
$posWords = array ('good', 'funny');
$negCount = 0;
$posCount = 0;
foreach ($negWords as $negWord) {
# negative word prepended by not
preg_match_all("/(?<=\bnot\b)\b$negWord\b/i", $subject, $matches);
$posCount += count($matches[0]);
# negative word
preg_match_all("/(?<!\bnot\b)\b$negWord\b/i", $subject, $matches);
$negCount += count($matches[0]);
}
foreach ($posWords as $posWord) {
# positive word prepended by not
preg_match_all("/(?<=\bnot\b)\b$posWord\b/i", $subject, $matches);
$negCount += count($matches[0]);
# positive word
preg_match_all("/(?<!\bnot\b)\b$posWord\b/i", $subject, $matches);
$posCount += count($matches[0]);
}
echo "<br/>";
echo "pos: $posCount";
echo "<br/>";
echo "neg: $negCount";
pritaeas
Posting Prodigy
9,293 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,462
Skill Endorsements: 86
im a bit confused as to how yor method works so for
this is not good not bad not poor not boring not cheesy not great not good not good
id expect pos to = 4 and neg to = 4 also for my words i have about 200 of each pos and neg
cr7489
Junior Poster in Training
53 posts since Dec 2011
Reputation Points: 25
Solved Threads: 0
Skill Endorsements: 0
Add the good words (good, great) to the posWords array, and the bad words (bad, poor, boring, cheesy) to the negWords array. If you then change subject to the line you showed, you'll see it counts 4 and 4. If you put the words in the right array, it will check whether or not it has the word "not" in front of it. "Bad" counts as negative, whereas "Not bad" counts as positive.
pritaeas
Posting Prodigy
9,293 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,462
Skill Endorsements: 86
so how would i put my words in to an array ?? i have attempted this
$poswords = array ($pos['word']);
$negwords = array ($neg['word']);
cr7489
Junior Poster in Training
53 posts since Dec 2011
Reputation Points: 25
Solved Threads: 0
Skill Endorsements: 0
To fill the arrays use something like this:
$pos = mysql_query("SELECT word FROM positive");
$posWords = array ();
if ($pos) {
while ($row = mysql_fetch_assoc($pos)) {
$posWords[] = $row['word'];
}
}
$neg = mysql_query("SELECT word FROM negative");
$negWords = array ();
if ($neg) {
while ($row = mysql_fetch_assoc($neg)) {
$negWords[] = $row['word'];
}
}
pritaeas
Posting Prodigy
9,293 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,462
Skill Endorsements: 86
thanks alot, your 2 methods seem to have done the trick that was what i was missing :)
cr7489
Junior Poster in Training
53 posts since Dec 2011
Reputation Points: 25
Solved Threads: 0
Skill Endorsements: 0
Please mark the thread solved then.
pritaeas
Posting Prodigy
9,293 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,462
Skill Endorsements: 86
Question Answered as of 1 Year Ago by
pritaeas,
diafol
and
Biiim just a quick one regarding this how can i make it so that if 'not' or a positive and negative word is the first word it will detect it as it is not picking it up
cr7489
Junior Poster in Training
53 posts since Dec 2011
Reputation Points: 25
Solved Threads: 0
Skill Endorsements: 0
Am not sure what you mean. Can you give an example of what's not working in code?
pritaeas
Posting Prodigy
9,293 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,462
Skill Endorsements: 86
actually the code is working okay, i basically had a problem for example if a sentence started as:
not good but it was ok, then the 'not good' was not being picked up but this was due to having a space in (?<=\b not\b)
cr7489
Junior Poster in Training
53 posts since Dec 2011
Reputation Points: 25
Solved Threads: 0
Skill Endorsements: 0