Jump to content

Compare words in two Arrays and calculate number of hits


djinnov8

Recommended Posts

Hi guys,

 

I am new to PHP/coding and am trying to look for

1. A way of comparing the words in one static array against other dynamically created arrays (created from mysql queries)

2. Work out how many similar words there are - then assign that number to that array

 

My static array is...$comparewithme = array with values = this is an example of an array

 

Mysql_query("select id, words from table_example")

Results from query are put into an array that is named according to id..

$result2 = array with values = this is an example of queries

$result3 = array with values = this is not an example of php

 

Comparison should give the following info

Comparing $comparewithme with $result2 should generate a hit rate of 5 (similar words=this is an example of)...

Comparing $comparewithme with $result3 should generate a hit rate of 4 (similar words=this is an example)...

 

Any ideas greatly appreciated...thanks in advance

I came across a post that contained something kinda similar...

http://stackoverflow.com/questions/1957989/compare-5000-strings-with-php-levenshtein

 

Would Levenshtein do this and if so, I wonder what the syntax would look like?

Found this at ...

http://php.tonnikala.org/manual/cs/function.levenshtein.php

 

 

june05 at tilo-hauke dot de

06-Jun-2005 10:44

//levenshtein for arrays

function array_levenshtein($array1,$array2)

        {  $aliases= array_flip(array_values(array_unique(array_merge($array1,$array2))));

            if(count($aliases)>255) return -1;

            $stringA=''; $stringB='';

            foreach($array1 as $entry) $stringA.=chr($aliases[$entry]);

            foreach($array2 as $entry) $stringB.=chr($aliases[$entry]);

            return levenshtein($stringA,$stringB);

        }

       

// e.g. use array_levenshtein to detect special expressions in unser-inputs

 

echo array_levenshtein(split(" ", "my name is xxx"), split(" ","my name is levenshtein"));

 

//output: 1

 

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.