使用PHP将句子拆分为较短的句子

使用PHP将句子拆分为较短的句子,php,string,Php,String,是否有某种方法可以应用于将句子拆分为较小的句子以执行数据库搜索。我的客户希望执行如下示例中所示的数据库搜索: 第一项请求:索赔人在咖啡店发生事故 第二项请求:索赔人在咖啡中发生事故(如果文件说明是酒吧等) 第三项请求:索赔人在索赔过程中发生事故 ... 最后请求:索赔人 我发现很多关于逐字拆分句子的话题,但没有关于词块的话题。有什么建议吗?您可以使用explode(“,$str)。然后,您可以逐字重建句子(除了特定迭代之外的单词) 类似这样(循环可能已关闭,我以前从未编写过PHP): for($

是否有某种方法可以应用于将句子拆分为较小的句子以执行数据库搜索。我的客户希望执行如下示例中所示的数据库搜索:

第一项请求:索赔人在咖啡店发生事故 第二项请求:索赔人在咖啡中发生事故
(如果文件说明是酒吧等)
第三项请求:索赔人在索赔过程中发生事故 ... 最后请求:索赔人


我发现很多关于逐字拆分句子的话题,但没有关于词块的话题。有什么建议吗?

您可以使用
explode(“,$str)
。然后,您可以逐字重建句子(除了特定迭代之外的单词)

类似这样(循环可能已关闭,我以前从未编写过PHP):

for($x=count($句);$x>0;$x--){
对于($y=0;$y<$x;$y++){
echo$cars[$y];
回声“
”; } 回声“
” }
您可以编写一个基于标点符号拆分的regexp。本例将句点、问号、感叹号和行尾分开,您应该开始学习:

$data = 'First request: Claimant lost $500 in the coffee shop.  Second request: (unknown) Claimant had an accident?  Third request: Claimaint went to the hospital!  End of report';

preg_match_all("/\s*(.*?(?:[\.\?\!]|$))/", $data, $matches);
foreach ($matches[1] as $sentence) {
    if (preg_match("/\S/", $sentence) ) {
        print "Sentence: $sentence\n";
    }
}
结果:

Sentence: First request: Claimant lost $500 in the coffee shop.
Sentence: Second request: (unknown) Claimant had an accident?
Sentence: Third request: Claimaint went to the hospital!
Sentence: End of report

具有
分解
内爆
阵列片
功能的简单解决方案:

$str = "Claimant had an accident in the coffee shop";
$words = explode(" ", $str);
$count = count($words);

echo implode(" ", array_slice($words, 0, $count)) . "<br>"; // first request
while (--$count) {
    echo implode(" ", array_slice($words, 0, $count)) . "<br>";
}
$string = 'Claimant had an accident in the coffee shop';

echo "$string<br>";  // use the entire string first as first iteration of
                     // the loop will chop off the last word

while ($string = substr($string, 0, strrpos($string, ' '))) {
    echo "$string<br>";
}

使用字符串函数的另一个选项:

$str = "Claimant had an accident in the coffee shop";
$words = explode(" ", $str);
$count = count($words);

echo implode(" ", array_slice($words, 0, $count)) . "<br>"; // first request
while (--$count) {
    echo implode(" ", array_slice($words, 0, $count)) . "<br>";
}
$string = 'Claimant had an accident in the coffee shop';

echo "$string<br>";  // use the entire string first as first iteration of
                     // the loop will chop off the last word

while ($string = substr($string, 0, strrpos($string, ' '))) {
    echo "$string<br>";
}
$string='索赔人在咖啡店发生事故';
回显“$string
”;//首先使用整个字符串作为 //循环将切掉最后一个单词 而($string=substr($string,0,strrpos($string,,)){ 回显“$string
”; }
有很多方法可以剥下这只猫的皮。如何定义任务中不同请求的“旋转”机制?我的意思是,程序如何知道每个请求应该包含多少个单词?@RomanPerekhrest,每个新请求应该等于前一个请求减去一个单词。如果我使用相反的方法,这在某种程度上是正确的。@IvanVenediktov可以随意使用它。如果你觉得我的回答有帮助,你也可以接受。你是真正的MVP!很高兴听到这个消息!(非常感谢)