Jump to content

Stop Word Filter Code Not working...


natasha_thomas

Recommended Posts

Friends,

 

I have a code to filter out stopwords (from text file) out of a string supplied.

 

Seems its not filtering out the stop words.  :'(

 

<?php

$CommonWords = file_get_contents('http://minimate.co.uk/stopwords.txt');
$CommonWords = explode("\n", $CommonWords);


$keywords = 'This is did samle a as any ass';

$search_keywords = strtolower(trim($keywords));
$arrWords = explode(' ', $search_keywords);
$arrWords = array_unique($arrWords);

foreach ($arrWords as $word) {
    if (!in_array($word, $CommonWords) && (trim($word) != '')) {
        $searchWords[] = $word;
    } 

}
print_r($searchWords);

?>

 

 

Output is:

Array ( [0] => this [1] => is [2] => did [3] => sample [4] => a [5] => as [6] => any [7] => ass )

 

So the output is not filtering out any words from that text file.

 

Can someone help me with this?

 

Cheers

Natasha T

Link to comment
https://forums.phpfreaks.com/topic/229548-stop-word-filter-code-not-working/
Share on other sites

There's whitespace in the $CommonWords array elements

 

$CommonWords = file_get_contents('http://minimate.co.uk/stopwords.txt');
$CommonWords = explode("\n", $CommonWords);
$CommonWords = array_map('trim', $CommonWords); // <---- ADD THIS LINE

There's whitespace in the $CommonWords array elements

 

$CommonWords = file_get_contents('http://minimate.co.uk/stopwords.txt');
$CommonWords = explode("\n", $CommonWords);
$CommonWords = array_map('trim', $CommonWords); // <---- ADD THIS LINE

 

Many Thanks!

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.