首页 > 解决方案 > 如何将行中的单词与php进行比较?

问题描述

我正在尝试编写遍历 .CSV 文件并识别第一行和第二行、第一行和第三行之间的相似词等的 php 代码。

我的 test.csv 文件如下所示:

1,"most of the birds lives in the village"
2,"birds in the village are free"
3,"no birds can be trapped in the village"
4,"people of the village are so kind"
5,"these birds are special species where most of them are so small" 

注意:上例中的数字在 test.csv 文件中,但它们是比较的关键,它称为句子 ID,我将使用它们进行比较。

所以通过使用上面的 test.csv 文件,我想要做的是比较第 1 行和第 2 行并告诉有多少单词是相似的,然后比较第 1 行和第 3 行并告诉有多少相似的单词等等上,并完成以下格式:

1:2 = ?

1:3 = ?

1:4 = ?

1:5 = ?

2:3 =?

2:4 =?

2:5 =?

3:4 =?

3:5 =?

4:5 =?

标签: php

解决方案


试试这个,不完美,但它可以完成工作:

// You can use PHP's array_intersect 
function checkSimilarity ($string1, $string2) {
    $arr1 = explode(" ",$string1 );
    $arr2 = explode(" ",$string2 );
    $result = array_intersect(array_unique($arr1) , array_unique($arr2)); //matched elements with duplicates removed
    return count($result); //number of matches
}

$sentences = [
    1 => "most of the birds lives in the village",
    2 => "birds in the village are free",
    3 => "no birds can be trapped in the village",
    4 => "people of the village are so kind",
    5 => "these birds are special species where most of them are so small"
];

// loop through array
foreach ($sentences as $mainKey => $value) {
    // for each number, loop others check similarity
    foreach ($sentences as $key => $v) {
        // if number key exist
        $compareTo = $key + 1;
        if(array_key_exists($compareTo, $sentences) && $compareTo != $mainKey && $mainKey < $compareTo) {
            echo $mainKey . ":" . $compareTo  . " = " . checkSimilarity($value, $sentences[$compareTo]) . "\n";
        }
    }
}

沙箱示例: http: //sandbox.onlinephpfunctions.com/code/c571beb140a1dc114b42bfd884fbe33e348f76c5


推荐阅读