gpt4 book ai didi

php - 如何通过关键字识别相似的字符串

转载 作者:搜寻专家 更新时间:2023-10-31 21:07:21 24 4
gpt4 key购买 nike

keywords: all the words with more than 3 characters

我想比较具有这些条件的两个字符串之间的关键字:

  1. 移动单词并不重要(example1 适用于这种情况)
  2. 不计算少于 3 个字符的单词(example2 适用于这种情况)
  3. 将较短的句子放入 str1(字符数)。 (示例 3 适用于这种情况)
  4. 我只想在 str1 和 str2 中使用不同的词。 (示例 4 适用于这种情况)

事实上,我有一个每天攻击两个新闻网站并将新闻复制到我的数据库的机器人。然后我需要一种算法来比较新闻标题并识别重复新闻。 (如您所知,同一新闻在不同的新闻网站上有不同的标题。但通常,同一新闻的标题包含相同的关键字)

示例 1:动词无所谓

str1= 'hello petter'
str2= 'petter hello'

result: 0

示例 2:小于3个字符的词不计算

str1= 'hello !!'
str2= 'petter hello'

result: 0 // '!!' are less than 3characters and str1 is 'hello'. then result:0

str1= 'hello petter‌ how are u?'
str2= 'petter hello how are you'

result: 0 // str1 is 'hello petter how are'

示例 3:必须更改变量

str1= 'hello petter‌ how are you ?'
str2= 'petter hello how are you?'
// Then
str1= 'hello petter‌ how are you?'
str2= 'petter hello how are you ?'

result:1 // 1 is for 'you' (in str1)

示例 4:不同的词在 str2 中并不重要

str1= 'hello petter‌ how are you?'
str2= 'petter hello how are you ?'

result: 1 // str2 is 'petter hello how are you', then 1 is for: 'you?' (in str1)

Note: 'you' (in str2) is not important for me, because it isn't match with any words of str1.

咒语示例: (更多信息)

str1= 'petter‌ hello how are you pal?'
str2= 'petter hello how are... !!'

// In first str1 change with str2
str1= 'petter hello how are... !!'
str2= 'petter‌ hello how are you pal?'

// Then remove '!!' (in str1)
str1= 'petter hello how are...'
str2= 'petter‌ hello how are you pal?'

result: 1 // 1 for 'are...' (in str1) - ['are','you','pal?' does not matter (in str2)]

最后,我需要一个函数来通过结果和关键字数量(所有超过 3 个字符的单词)来识别重复新闻

$keywords_numb=7;
$result=2;

function identify_duplicate($keywords_numb,$result){
if($keywords_numb / 3 >= $result){
$Specified = 'this is a new news';
}

else $Specified = 'this is a duplicate news';
return $Specified;

}

echo $Specified;

输出:

this is a new news

有人知道我该如何编写这个程序吗?问候

最佳答案

为此您不需要正则表达式..您可以使用以下函数并以任何顺序传递字符串:

function identify_duplicate($var1, $var2){
if(strlen($var1)>=strlen($var2)){
$str1 = $var1;
$str2 = $var2;
}
else{
$str1 = $var2;
$str2 = $var1;
}
$str1 = explode(" ", $str1);
$str2 = explode(" ", $str2);

$return = sizeof($str1);

foreach($str1 as $val){
if(in_array($val, $str2) || strlen($val) <= 3){
$return = $return - 1;
}
}

return $return;
}

关于php - 如何通过关键字识别相似的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30279214/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com