2017-08-04 33 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 
} 

讓我知道這是你想要什麼,我會帶給你的代碼即可。

相關問題