Java 一种从句子中提取短语的算法
我一直致力于实现从句子中提取短语的算法。每个短语必须包含2到5个单词。例如,我有一句话“这篇文章是关于我想测试它”。我需要从这句话中得到一个包含以下短语的数组:Java 一种从句子中提取短语的算法,java,php,c,algorithm,Java,Php,C,Algorithm,我一直致力于实现从句子中提取短语的算法。每个短语必须包含2到5个单词。例如,我有一句话“这篇文章是关于我想测试它”。我需要从这句话中得到一个包含以下短语的数组: 本文 这篇文章是 这篇课文是关于 这篇课文是关于我的 文本是 文本是关于 文本是关于我的 文本是关于我的愿望 是关于 是关于我的 是关于我的愿望 是关于我的愿望 关于我的 关于我的愿望 关于我的愿望 关于我的考试愿望 我的愿望 我希望 我想测试一下 我想测试一下 想 想测试一下吗 想试试吗 检验 测试它 测试一下 在PHP中,我从以下代
$text = 'This text is about my wish to test it';
$words = explode(' ', $text);
// $words = ['This', 'text', 'is', 'about', 'my', 'wish', 'to', 'test', 'it']
请帮我实现主算法。它可以通过任何其他编程语言(C、Java、Python),而不仅仅是PHP 我需要的算法代码:
$text = 'This text is about my wish to test it';
$words = explode(' ', $text);
$wordsCount = count($words);
for ($i = 0; $i < $wordsCount; $i++) {
$window = 2;
$windowEnd = 5;
if ($i + $windowEnd > $wordsCount) {
$windowEnd = $wordsCount - $i;
}
if ($windowEnd < $window) {
break;
}
while ($window <= $windowEnd) {
for ($j = $i; $j < $i + $window; $j++) {
echo $words[$j], "\n";
}
echo "\n";
$window++;
}
}
$text='此文本是关于我希望测试它的内容';
$words=分解(“”,$text);
$wordscont=count($words);
对于($i=0;$i<$wordscont;$i++){
$window=2;
$windowEnd=5;
如果($i+$windowEnd>$WordScont){
$windowEnd=$wordsCount-$i;
}
如果($windowEnd<$window){
打破
}
而Java中的($window)
String text = "This text is about my wish to test it";
int indexFirst = 0;
while (indexFirst > -1 && text.length() > indexFirst +1) {
int indexLast = text.indexOf(" ", indexFirst + 1);
indexLast = text.indexOf(" ", indexLast + 1);
while (indexLast > -1 && text.length() > indexLast + 1) {
System.out.println(text.substring(indexFirst, indexLast));
indexLast = text.indexOf(" ", indexLast + 1);
}
System.out.println(text.substring(indexFirst));
indexFirst = text.indexOf(" ", indexFirst + 1);
}
返回
This text
This text is
This text is about
This text is about my
This text is about my wish
This text is about my wish to
This text is about my wish to test
This text is about my wish to test it
text is
text is about
text is about my
text is about my wish
text is about my wish to
text is about my wish to test
text is about my wish to test it
is about
is about my
is about my wish
is about my wish to
is about my wish to test
is about my wish to test it
about my
about my wish
about my wish to
about my wish to test
about my wish to test it
my wish
my wish to
my wish to test
my wish to test it
wish to
wish to test
wish to test it
to test
to test it
test it
欢迎使用SO。请阅读和SO并不是免费的编码或教程服务。您必须表明您已经做出了一些努力来解决自己的问题。这可以通过滑动窗口完成,其中窗口以两个单词开头,从第一个、第二个、第三个等单词开始。然后将窗口大小更改为三个单词,再次从第一个、第二个等等。在“.”上爆炸,然后根据窗口大小显示数组中所需的元素数量,怎么样?@JoachimPileborg谢谢你的想法,问题解决了!