arrays - PHP: array_diff count issue due to multiple similar name -
how can match similar words in array_diff count
problem of multiple name single words tv-television,inches-inch,mobile-mobile phones,mobile-phones.so create wrong percentage in array_diff count
example :
$str1 = "samsung television 21 inches led bh005de"; $str2 = "samsung 21 inch led tv"; $arr1 = explode(' ', $str1); $arr2 = explode(' ', $str2); $differencecount = count(array_diff($arr2, $arr1));
in above str1 , str2 contain television,tv , inches,inch words..how can solve problem
the obvious way use synonyms that:
$str1 = "samsung television 21 inches led bh005de"; $str2 = "samsung 21 inch led tv"; //synonyms: $syns = [ 'tv' => ['tv', 'television'], 'inch' => ['inch', 'inches'] ]; //replace: $str1 = array_reduce(array_keys($syns), function($c, $x) use ($syns) { return $c = preg_replace('/\b'.join('\b|\b', $syns[$x]).'\b/', $x, $c); }, $str1); //now, str1 looks "samsung tv 21 inch led bh005de" $str2 = array_reduce(array_keys($syns), function($c, $x) use ($syns) { return $c = preg_replace('/\b'.join('\b|\b', $syns[$x]).'\b/', $x, $c); }, $str2); //now, str2 looks "samsung 21 inch led tv" $arr1 = explode(' ', $str1); $arr2 = explode(' ', $str2); //var_dump(array_diff($arr1, $arr2));//['bh005de']
in case can't rely on sort of word forms (like inch
- inches
) - because need parse abbreviations - , cases specific meanings. thus, using synonyms may way resolve matter cases.
Comments
Post a Comment