【问题标题】:Return only one sentence based on the words found in array根据数组中找到的单词只返回一个句子
【发布时间】:2016-03-12 16:05:41
【问题描述】:

我有一个 Java 代码,可以根据单词数组检索句子。 文本字符串:

String text = "This is a sample text. Simple yet elegant. Everyone dies. I don't care. This text is nice.";

我还有一个单词数组如下:

String[] words = new String[] {"text", "care", "nice"};

现在,我需要获取数组中包含特定单词的句子。因此,要输出的句子应该包含单词“text”、“care”或“nice”。结果输出应如下所示:

This is a sample text. //contains the word "text"
I don't care. //contains the word "care"
This text is nice. //contains the word "text" and "nice" 

我的代码是:

    public static void main(String[] args) {
    String text = "This is a sample text. Simple yet elegant. Everyone dies. I don't care. This text is nice.";
    String[] words = new String[] {"text", "care", "nice"};
    String[] parts = text.split("\\.");

    for(String w: words){
        for(String sentence: parts){
            if(sentence.contains(w)){
                System.out.println(sentence +" //contains: "+w);
            }
        }
    }   
}

但是,如果句子包含数组中的两个单词,它将打印该句子两次。例如:

The text is nice //contains: text
The text is nice//contains: nice.

如何只打印一次句子? 谢谢。

【问题讨论】:

  • 那么当你只打印一次句子时,你是否希望它包含的所有单词也都打印出来?
  • 是的,只检索数组中具有指定单词的句子,并列出句子中找到的所有单词。
  • 发表了我的答案。看看吧。

标签: java arrays


【解决方案1】:

Java 8 解决方案。

    for (String sentence : parts) {

        List<String> wordsInCurrentSentence = new LinkedList<String>();

        for (String w : words) {
            if (sentence.contains(w)) {
                wordsInCurrentSentence.add(w);
            }
        }

        if (!wordsInCurrentSentence.isEmpty()) {
            String result = wordsInCurrentSentence.stream().collect(Collectors.joining(","));
            System.out.println(sentence.trim() + " //contains: " + result);
        }
    }

【讨论】:

    【解决方案2】:

    我认为最好将外部循环放入。这样您可以检查您想要的单词是否被命中并将它们添加到本地列表中。像这样的:

    for(String sentence: parts){
    
        List<String> hitList = new ArrayList<String>();
    
        for(String w: words){
            if(sentence.contains(w)){
                hitList.add(w);
            }
        }
    
        System.out.println(sentence +" //contains: "+ hitList != null ? hitList : "No match" );
    }
    

    这样你就可以解决你指出的那种情况这段文字很好。 //包含单词“text”和“nice”

    【讨论】:

    • 如果我们先检查hitAlready,我们可以跳过对contains()的调用,对吧?
    • 我编辑了代码,因为我拥有的那个不会打印出所有的热门词,只有第一个。根据您所写的内容,您希望能够打印所有的热门歌曲。
    • 我所做的改变更适合你的情况和你想要的。您将所有匹配项保存在列表中,比较之后,您只需编写/显示它们。
    • 在打印之前添加一个检查 hitList 不为空,你就会得到我的 +1。
    【解决方案3】:

    反转循环并添加中断。其他人已经提出了更好的方法来做到这一点。但是只要对你的代码做些小改动,它就应该可以工作。交换循环并在成功时添加中断。

    for(String sentence: parts){
            for(String w: words){
                if(sentence.contains(w)){
                    System.out.println(sentence +" //contains: "+w);
                    break;
                }
            }
        }
    

    【讨论】:

      【解决方案4】:

      我会使用正则表达式:

      String regex = ".*?(" + String.join("|", words) + ").*?";//either of one word in the sentence
      for (String sentence: parts) {
          if(sentence.matches(regex)) {
              //...
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-20
        • 1970-01-01
        • 2021-06-22
        • 2022-06-10
        相关资源
        最近更新 更多