我有一串文字如下:
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" ,"关心"或者"很好" 。结果输出应如下:
This is a sample text. //contains the word "text"
I don't care. //contains the word "care"
This text is nice. //contains the "nice"
为此,我试图将每个句子存储在数组String[] sentences
中,这将生成如下输出:
[This, is, a, sample, text], [Simple, yet, elegant] , [Everyone, dies], [I, don't, care], [This, text, is, nice]
不确定下一步该从哪里开始。感谢帮助。
答案 0 :(得分:2)
您可以尝试这样的事情:
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);
}
}
}
}
输出:
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
答案 1 :(得分: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;
}
答案 2 :(得分:0)
你可以试试这个
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]);
}
}
}
其中 targetArr 包含具有分隔数据的数组
[This, is, a, sample, text], [Simple, yet, elegant] , [Everyone, dies], [I, don't, care], [This, text, is, nice]
答案 3 :(得分:0)
[[This, is, a, sample, text], [Simple, yet, elegant] , [Everyone, dies], [I, don't, care], [This, text, is, nice]]
好吧,如果你得到上面的输出,我想你有和数组字符串数组。
如果是这种情况,那么您可以遍历第一组,然后检查该数组中的任何单词是否匹配。
以下是我的一些代码:
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)
另一种方法是使用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);
应该打印
This is a sample text
I don't care
This text is nice