如何比较数组中的 n 个多个句子并使用 php 和 array_intersect 获取常用词并将常用词存储在数组中
how to compare n multiple sentences which are in array and get the common words using php using array_intersect and store the common words in an array
嗨,朋友们,我正在尝试比较数组中的 n 个句子,并获取出现在多个句子中的常用词。
这是示例
$cars=("hello how","hello when","when you came",........,n sentences);
array_intersect 用于比较 2 个或 3 个数组,它们在所有三个数组中都有完全相同的单词,谁能帮我解决这些问题
$cars = array_filter(
array_count_values(
array_merge(
...array_map(
function($x) { return array_unique(explode(' ', $x));},
$cars))),
function($x) { return $x > 1; });
print_r($cars);
循序渐进
- 分解字符串并只保存唯一的单词
- 合并所有数组
- 计算每个单词的频率
- 获取出现不止一次的词
嗨,朋友们,我正在尝试比较数组中的 n 个句子,并获取出现在多个句子中的常用词。 这是示例
$cars=("hello how","hello when","when you came",........,n sentences);
array_intersect 用于比较 2 个或 3 个数组,它们在所有三个数组中都有完全相同的单词,谁能帮我解决这些问题
$cars = array_filter(
array_count_values(
array_merge(
...array_map(
function($x) { return array_unique(explode(' ', $x));},
$cars))),
function($x) { return $x > 1; });
print_r($cars);
循序渐进
- 分解字符串并只保存唯一的单词
- 合并所有数组
- 计算每个单词的频率
- 获取出现不止一次的词