考虑我有一个
string1 = "hello hi goodmorning evening [...]"
Run Code Online (Sandbox Code Playgroud)
我有一些小关键字
compare1 = "hello evening"
compare2 = "hello hi"
Run Code Online (Sandbox Code Playgroud)
我需要一个函数来返回文本和关键字之间的关联.例:
function(string1,compare1); // returns: 4
function(string1,compare2); // returns: 5 (more relevant)
Run Code Online (Sandbox Code Playgroud)
请注意,5和4仅作为示例.
你可以说 - 编写一个计算出现次数的函数 - 但是对于这个例子,这不起作用,因为它们都有2次出现,但是compare1的相关性较低,因为"你好晚上"并不是在string1中找到的(2个字你好和晚上是你好比你好更多)
有没有任何已知的算法来做到这一点?
ADD1:
在这种情况下,像编辑距离这样的算法是行不通的.因为string1是一个完整的文本(如300-400个单词),并且比较字符串最多为4-5个单词.
嘿伙计:)我想问一些解决方案.现在,我有字典words.txt,这里有一些例子:
happy
laugh
sad
Run Code Online (Sandbox Code Playgroud)
我有俚语字符串:
hppy
Run Code Online (Sandbox Code Playgroud)
我想搜索并匹配那个俚语字符串到我的字典,这意味着它会返回"happy",因为那些字符串在字典中引用"happy".
最近我一直在使用similar_text(),但对其有效性没有信心.你们能为我的问题推荐更好的解决方案吗?谢谢 :)
在这里我把我的代码:
function searchwords($tweet){
//echo $tweet;
$find = false;
$handle = @fopen("words.txt", "r");
if ($handle)
{
while (!feof($handle))
{
$buffer = fgets($handle);
similar_text(trim($tweet),trim($buffer),$percent);
if ($percent == 100){ // this exact match
$find = true;
}else if ($percent >= 90){ //there is the possibility of errors
$find = true;
}
}
fclose($handle);
}
if ($find == true){
unset($tweet); …Run Code Online (Sandbox Code Playgroud) 在不包含PHP中空格的情况下,在两个字符串中查找通用字母的优雅代码是什么?
还返回相似性索引,即计算常见字符数,并以占字符总数的百分比返回。
假设我有一个字符串“ LEGENDARY”,而另一个则为“ BARNEY STINSON”,因此我需要找到两个不包含空格的常用字母b / w。
同样,我的代码应返回相似性索引,即计算常见字符的数量,并以占字符总数的百分比返回。
对于这两个字符串,常见字符为“ ARNEY”,因此得分为5/22〜= 22%。有什么帮助吗?