我想从文本中得到一些句子。
下面是示例文本,
狮ry低声打断。渡渡鸟说,“一点也不。”指着木头混乱的喧闹声-(尽管她对他很想。格里芬低声打断了。“一点也不,”渡渡鸟指着那混乱的东西说。树林的喧闹声-(尽管她向你考虑了他。狮phon低声打断了。“一点也不,”渡渡鸟指着木头的混乱叫声说-(尽管
到目前为止,我已经能够从一个较大的文本中获得30个单词,但是最后,我的句子不完整,我想删除这样的句子。
这里是获取30个单词的功能,
/**
* @param $sentence
* @param int $count
* @return mixed
*/
function get_words($sentence, $count = 30) {
preg_match("/(?:\w+(?:\W+|$)){0,$count}/", $sentence, $matches);
return $matches[0];
}
我从下面的问题中使用了上面的功能
How to select first 10 words of a sentence?
当我在文本上方传递给函数时,我得到了这样的输出,
狮ry低声打断。 “一点也不,”渡渡鸟说,指着木头的混乱叫声-(尽管她对你算是他了。狮phon打断了
这里的最后一句话是不完整的,我不希望在输出中这样。
有什么办法可以做到这一点?
我正在使用PHP和Laravel,任何帮助和建议都将受到赞赏。
答案 0 :(得分:1)
下面的代码可能会对您有所帮助。
<?php
$sen="Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.";
$cropped_data = get_words($sen);
$strlength = strlen ( $cropped_data );
$remains= complete_sentence(substr($sen,$strlength));
function complete_sentence($content) {
$pos = strpos($content, '.');
return substr($content, 0, $pos+1);
}
function get_words($sentence, $count = 30) {
preg_match("/(?:\w+(?:\W+|$)){0,$count}/", $sentence, $matches);
return $matches[0];
}
echo "complete sentence<br/>".$cropped_data.$remains;
?>
谢谢。
答案 1 :(得分:0)
function get_words($sentence, $count = 30) {
preg_match("/(?:w+(?:W+|$)){0,$count}/", $sentence, $matches);
return $matches[0];
};
$sentence = "Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.";
$cropSentence = get_words($sentence);
$finalSentence= substr($cropSentence, 0, strrpos($cropSentence, "."));
echo $finalSentence;
它将返回直到最后一次出现(。);
狮ry低声打断。 “一点也不,”渡渡鸟说, 指着木头的混乱喧闹-(她认为他是 你,虽然