【问题标题】:Algorithm for extracting phrases from sentence [closed]从句子中提取短语的算法[关闭]
【发布时间】:2016-05-13 20:47:28
【问题描述】:

我一直坚持实现从句子中提取短语的算法。每个短语必须包含 2 到 5 个单词。例如,我有一个句子'This text is about my hope to test it'。我需要从这句话中得到一个包含下一个短语的数组:

  1. 这段文字
  2. 此文本是
  3. 本文是关于
  4. 这篇文章是关于我的
  5. 文字是
  6. 文字是关于
  7. 文字是关于我的
  8. 文字是我的愿望
  9. 是关于
  10. 是关于我的
  11. 是关于我的愿望
  12. 是关于我的愿望
  13. 关于我的
  14. 关于我的愿望
  15. 关于我的愿望
  16. 关于我的测试愿望
  17. 我的愿望
  18. 我的愿望
  19. 我想测试一下
  20. 我想测试一下
  21. 希望
  22. 希望测试
  23. 想测试一下
  24. 测试
  25. 进行测试
  26. 测试一下

在 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。

【问题讨论】:

  • 欢迎来到 SO。请阅读What topics can I ask aboutHow to ask a good questionthe perfect question SO 不是免费的编码或教程服务 你必须证明你已经为解决自己的问题付出了一些努力。
  • 这可以通过一个滑动窗口来完成,窗口从两个单词开始,从第一个、第二个、第三个等单词开始。然后将窗口大小更改为三个单词,再次从第一个、第二个等单词开始。
  • 在“。”上爆炸怎么样?然后根据窗口大小显示数组中所需的元素数量?
  • @JoachimPileborg 谢谢你的想法,问题解决了!

标签: java php c algorithm


【解决方案1】:

我需要的算法代码:

$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++;
    }
}

【讨论】:

    【解决方案2】:

    在 Java 中

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-24
      • 2020-10-27
      • 1970-01-01
      • 2020-06-21
      • 2013-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多