根据单词数组检索句子

时间:2021-10-12 12:46:41

I have a string of text as below:

我有一串文字如下:

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

I also have an array of words as below:

我也有一系列的单词如下:

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

Now, I need to get the sentences that contains the specific word in the array. So, the sentences to be outputted should contain either the word "text" , "care" or "nice" . The resultant output should be as below:

现在,我需要获得包含数组中特定单词的句子。因此,要输出的句子应该包含单词“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 "nice"

To do that, I have attempted to store each sentences in an array String[] sentences that will produce output like this:

为此,我试图将每个句子存储在一个String []句子中,这些句子会产生如下输出:

[This, is, a, sample, text], [Simple, yet, elegant] , [Everyone, dies], [I, don't, care], [This, text, is, nice]

Not sure where to go from here next. Help is appreciated.

不知道从哪里开始下一步。感谢帮助。

6 个解决方案

#1


2  

You could try something like this:

你可以尝试这样的事情:

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);
            }
        }
    }   
}

OUTPUT:

OUTPUT:

This is a sample text //contains: text
This text is nice //contains: text
I don't care //contains: care
This text is nice //contains: nice

#2


1  

 public List<String> getSentencesWithWord(String searchWord, String text) {
    List<String> resultList = Stream.of(text.split("\\.")).map(s -> s.trim()).collect(Collectors.toList());
    for (int i = resultList.size() -1; i >= 0; i--) {
        if (! resultList.get(i).contains(searchWord)) {
            resultList.remove(i);
        }
    }
    return resultList;
}

#3


0  

[[This, is, a, sample, text], [Simple, yet, elegant] , [Everyone, dies], [I, don't, care], [This, text, is, nice]]

Well if you get have the above output i suppose you have and array of string arrays.

好吧,如果你有上面的输出,我想你有和字符串数组的数组。

If that is the case then you can iterate through the first set and then check to see if any words in that array match.

如果是这种情况,那么您可以遍历第一组,然后检查该数组中的任何单词是否匹配。

Here is some of my code:

这是我的一些代码:

String[][] sentences = new String[][]{};
String[] keywords = new String[]{};
for (String[] sentence: sentences )
{
  boolean match = false;
  wordsLoop: for (String word : sentence)
  {
    for (String keyword : keywords)
    {
      if(word.equals(keyword))
      {
        match = true;
        break wordsLoop; // this stops the outside wordloop loop
      }
    }
  }
  if(match)
  {
    System.out.println(String.join(" ",sentence)+".");
  }
}

#4


0  

private ArrayList<String> getWordsFromSentence(String prm_objString){
      ArrayList<String> AllMatchedSentences;
      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 [] AllSentence = text.split("\\."); //Split Your Text In Sentences.
      AllMatchedSentences = new ArrayList<>();
      foreach(String eachSentense : AllSentence) //Go Through with each Sentences.
      {
           for(int i=0; i<words .length; i++) // Go Through withh each Words
             {
               if(eachSentense.contains(words[i].toString())) // All Possible Match.
               //Add all Matched Sentences to Array List

                   AllMatchedSentences.add(eachSentense +" //contains "+words[i].toString()); 
             }
            System.out.println(eachSentense);
      }
     return AllMatchedSentences; //Return all Possible Sentences ias a ArrayList Object.
}

#5


0  

An alternative is to use java 8 features.

另一种方法是使用java 8功能。

java.util.List<String> sentences = Stream.of(text.split("\\.")).filter(s -> Stream.of(words).anyMatch(p -> s.contains(p))).collect(Collectors.toList());

sentences.forEach(System.out::println);

should print

应该打印

This is a sample text
 I don't care
 This text is nice

#6


0  

you could try this

你可以试试这个

             String targetArr[]={"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"};
         for(int i=0;i<words.length;i++)
         {
           for(int j=0;j<targetArr.length;j++)
              {
              if(targetArr[j].contains(words[i]))
                 {
                  System.out.println(targetArr[j]);
                  }
               }
           }

where targetArr contains your array with separated data

其中targetArr包含具有分隔数据的数组

 [This, is, a, sample, text], [Simple, yet, elegant] , [Everyone, dies], [I, don't, care], [This, text, is, nice]

#1


2  

You could try something like this:

你可以尝试这样的事情:

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);
            }
        }
    }   
}

OUTPUT:

OUTPUT:

This is a sample text //contains: text
This text is nice //contains: text
I don't care //contains: care
This text is nice //contains: nice

#2


1  

 public List<String> getSentencesWithWord(String searchWord, String text) {
    List<String> resultList = Stream.of(text.split("\\.")).map(s -> s.trim()).collect(Collectors.toList());
    for (int i = resultList.size() -1; i >= 0; i--) {
        if (! resultList.get(i).contains(searchWord)) {
            resultList.remove(i);
        }
    }
    return resultList;
}

#3


0  

[[This, is, a, sample, text], [Simple, yet, elegant] , [Everyone, dies], [I, don't, care], [This, text, is, nice]]

Well if you get have the above output i suppose you have and array of string arrays.

好吧,如果你有上面的输出,我想你有和字符串数组的数组。

If that is the case then you can iterate through the first set and then check to see if any words in that array match.

如果是这种情况,那么您可以遍历第一组,然后检查该数组中的任何单词是否匹配。

Here is some of my code:

这是我的一些代码:

String[][] sentences = new String[][]{};
String[] keywords = new String[]{};
for (String[] sentence: sentences )
{
  boolean match = false;
  wordsLoop: for (String word : sentence)
  {
    for (String keyword : keywords)
    {
      if(word.equals(keyword))
      {
        match = true;
        break wordsLoop; // this stops the outside wordloop loop
      }
    }
  }
  if(match)
  {
    System.out.println(String.join(" ",sentence)+".");
  }
}

#4


0  

private ArrayList<String> getWordsFromSentence(String prm_objString){
      ArrayList<String> AllMatchedSentences;
      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 [] AllSentence = text.split("\\."); //Split Your Text In Sentences.
      AllMatchedSentences = new ArrayList<>();
      foreach(String eachSentense : AllSentence) //Go Through with each Sentences.
      {
           for(int i=0; i<words .length; i++) // Go Through withh each Words
             {
               if(eachSentense.contains(words[i].toString())) // All Possible Match.
               //Add all Matched Sentences to Array List

                   AllMatchedSentences.add(eachSentense +" //contains "+words[i].toString()); 
             }
            System.out.println(eachSentense);
      }
     return AllMatchedSentences; //Return all Possible Sentences ias a ArrayList Object.
}

#5


0  

An alternative is to use java 8 features.

另一种方法是使用java 8功能。

java.util.List<String> sentences = Stream.of(text.split("\\.")).filter(s -> Stream.of(words).anyMatch(p -> s.contains(p))).collect(Collectors.toList());

sentences.forEach(System.out::println);

should print

应该打印

This is a sample text
 I don't care
 This text is nice

#6


0  

you could try this

你可以试试这个

             String targetArr[]={"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"};
         for(int i=0;i<words.length;i++)
         {
           for(int j=0;j<targetArr.length;j++)
              {
              if(targetArr[j].contains(words[i]))
                 {
                  System.out.println(targetArr[j]);
                  }
               }
           }

where targetArr contains your array with separated data

其中targetArr包含具有分隔数据的数组

 [This, is, a, sample, text], [Simple, yet, elegant] , [Everyone, dies], [I, don't, care], [This, text, is, nice]