php - 如何将行中的单词与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 =?
解决方案
试试这个,不完美,但它可以完成工作:
// 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
推荐阅读
- apache - 在 htaccess 中重定向到真实页面
- python - 如何从框架中删除所有标签但保留菜单/按钮?tkinter,蟒蛇
- apache-nifi - 无法连接到自定义 NiFi 端口
- java - 我正在从一项服务连接到另一个面临的转换问题
- swift - 从匹配类型的字符串中获取 Swift 枚举类型,而不是 rawValue
- sql - SQL 2 选择查询
- google-drive-api - 查询“sharedWithMe=false”无效值?
- java - 访问多对多关系的 Hibernate JPA 问题:标识符无效
- hazelcast - 了解 Hazelcast 成员加入或离开集群会影响哪些数据
- c - 如何解决错误:开关量不是整数和错误:案例标签不会减少为整数常量”