2017-08-04 49 views
-2

说我有一段是这样的:如何从java中的段落中找到多组特定单词?

String str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s"; 

setA将包含类似的Lorem,文本,假字。
setB将包含像Ipsum,印刷,工业等词。
setC将包含像Lorem,文字,假人,Ipsum,印刷,工业等词。

而在这之后

if(str.equals(set A)) 
{ 
    Some logic 
} 
else if{ 
    Set B logic 
} 
else{ 
Set C logic 
} 

如何在Java代码中呢?

+1

你有没有任何paragraphe已经尝试了一些? –

+0

雅我尝试使用StringUtils。但是我刚开始学习java,这对我来说有点困难。 – normalactivity

回答

0

也许是奇怪的解决方案,但它可以帮助你在很长的段落,所以平时我用String::matches与像一些正则表达式:

//Only one word 
(?i)(?=.*\bword\b).* 
//-----------^ 

//Multiple words 
(?i)(?=.*\bword1\b).*(?=.*\bword2\b).* 
//---------^-----------------^ 

这样的想法很简单,为您的字的图案,然后用火柴来验证,如果段落包含的所有单词或不:

代码示例:

class Main { 

    public static void main(String as[]) { 
     String str = "Lorem Ipsum is simply dummy text of the printing and " 
      + "typesetting industry. Lorem Ipsum has been the industry's " 
      + "standard dummy text ever since the 1500s"; 

     String setA = "Lorem, text, dummy"; 
     String setB = "Ipsum, printing, industry"; 
     String setC = "Lorem, text, dummy,Ipsum, printing, industry"; 
     Main m = new Main(); 

     if (str.matches(m.getPattern(setA))) { 
      //Do something 
     } else if (str.matches(m.getPattern(setB))) { 
      //Do something 
     } else if (str.matches(m.getPattern(setC))) { 
      //Do something 
     } 

    } 

    //The important method 
    private String getPattern(String words) { 
     StringBuilder pattern = new StringBuilder(); 
     System.out.println(Arrays.toString(words.split(",\\s*"))); 
     Arrays.asList(words.split(",\\s*")) 
       .stream() 
       .map(t -> "(?=.*\\b" + t + "\\b).*") 
       .forEach(pattern::append); 
     return "(?i)" + pattern.toString(); 
    } 
} 

方法getPattern需要的话setAsetBsetC的列表...,它可以是任何东西,然后在:

  1. (1)拆分这句话例如用于组A它将给你[Lorem, text, dummy](我认为输入是一个字符串,因为我使用拆分,如果你有一个集合,你可以避免使用拆分和使用这个集合,就像它是)
  2. (2)循环抛出单词列表来创建一个模式,以便稍后可以使用它来匹配您的输入(我使用Stream of Java 8而不是普通循环来简化模式的创建)。

因此,对于例如:setA它会返回一个模式像这样(?i)(?=.*\bLorem\b).*(?=.*\btext\b).*(?=.*\bdummy\b).*,它可以匹配包含所有单词Loremtext和虚拟

检查regex demo

1

试试这样的:

public boolean hasAny(final String txt, final Collection<String> words) 
{ 
    for (final String word : words) 
     if (txt.contains(word)) 
     return true; 
    return false; 
} 

public boolean hasAll(final String txt, final Collection<String> words) 
{ 
    boolean result = true; 
    for (final String word : words) 
    result &= txt.contains(word); 
    return result; 
} 

类似可以用Java8做流,太...

0

你希望每个组一个整数返回告诉你显灵的数量,或布尔值告诉你每个集合是否有多个幻影?

我会计算字符串中每个单词的幻影数并返回最低值,或者当每个幻影的计数大于等于2时返回布尔值,具体取决于您想要执行的操作。

我们首先必须类型的结构

Map<Integer,String> setA = new HashMap<Integer,String>();// Integer is the number of apparitions of the String in the set 

伪代码:

For each set{ 
For each word in the set{ 
count_of_word=str.count_number_of_apparitions_of(word) 
} 
for each word in the set{ 
if count_of_word<min_count_of_word{ 
min_count_of_word=count_of_word 
} 
return min_count_of_word 
} 

让我知道这是你想要什么,我会带给你的代码即可。

相关问题