2016-08-12 112 views
0

我正在嘗試計算從'句子'數組列表中每個句子出現uniqueBagOfWords的每個單詞的次數。如何計算每個單詞出現多少次?

uniqueBagOFwords = [我一樣,來,玩,網球,認爲,足球,需求大,變化]

我希望能夠指望從uniqueBagOfWords一個字有多少次出現在每個句子....目前,我只能在單詞出現的位置添加1,但我想添加它出現的次數。目前,它打印出這一點:

我喜歡打網球= 1111100000

我認爲足球需要大的變化= 1000011111

我喜歡足球足球= 1100001000

我將如何改變這個代碼,以便它打印出以下..

我喜歡打網球= 1111100000

我覺得足球需要大的變化= 1000011111

我喜歡足球足球= 1100002000

public static void main(String[] args) { 
     List<String> sentences = new ArrayList<String>(); 
     sentences.add("i like to play tennis"); 
     sentences.add("i think football needs big changes"); 
     sentences.add("i like football football"); 

    List<String[]> bagOfWords = new ArrayList<String[]>(); 
    for (String str : sentences) { 
     bagOfWords.add(str.split(" ")); 

    } 
    Set<String> uniqueBagOfWords = new LinkedHashSet<String>(); 
    for (String[] s : bagOfWords) { 
     for (String ss : s) 
      for (String st : ss.split(" ")) 
       if (!uniqueBagOfWords.contains(st)) 
        uniqueBagOfWords.add(st); 
    } 

    for (String s : sentences) { 
     StringBuilder numOfOccurences = new StringBuilder(); 
     int count = 0; 

     for (String word : uniqueBagOfWords) { 

      if (s.contains(word)) { 

       numOfOccurences.append(count+1); 
      } else { 
       numOfOccurences.append("0"); 
      } 
     } 
     System.out.println(s + " = " + numOfOccurences); 
    } 
} 
+0

你能有點用你的問題是什麼更直接? – Javant

+0

您是否想過您不是第一個嘗試這樣做的人? – shmosel

回答

0

您可能重寫了最後的這樣的循環:

for (String s : sentences) { 
    StringBuilder numOfOccurences = new StringBuilder(); 

    for (String word : uniqueBagOfWords) { 
     int count = 0; 
     for (String wordFromSentence : s.split(" ")) { 
      if (wordFromSentence.equals(word)) { 
       count++; 
      } 
     } 
     numOfOccurences.append(count); 
    } 
    System.out.println(s + " = " + numOfOccurences); 

} 
+0

這工作!謝謝!!!! –

+0

你可能會說我再次分裂每個句子來做雙重工作(你已經在主要方法的第一部分做過了),但是除非你有很多句子,否則我認爲它應該不重要。當然,如果需要,可以通過進一步重寫來消除雙重工作。 –

-1

我不能完全確定你的目標。

如果只想打印出你的輸出在一行,而不是必須在每個號碼的最後一個換行,那麼只需使用:

System.out.print(s + " = " + numOfOccurences); 

而不是

System.out.println(s + " = " + numOfOccurences); 

注使用print代替printlnprintln自動將換行符(\n)附加到輸出的末尾。

但也許還可以看看java.lang.Array的一些有用的搜索工具。注意:在搜索之前,數組需要被排序。

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html

很多在這裏很好的工具。

祝您好運:-)

+0

感謝您的回覆。我只是編輯了我的問題,希望更清楚。 –

0

這確實不是最好的修補程序,但它的工作原理

public static void main(String[] args) { 
    List<String> sentences = new ArrayList<String>(); 
    sentences.add("i like to play tennis"); 
    sentences.add("i think football needs big changes"); 
    sentences.add("i like football football"); 


List<String[]> bagOfWords = new ArrayList<String[]>(); 
for (String str : sentences) { 
    bagOfWords.add(str.split(" ")); 

} 
Set<String> uniqueBagOfWords = new LinkedHashSet<String>(); 
for (String[] s : bagOfWords) { 
    for (String ss : s) 
     for (String st : ss.split(" ")) 
      if (!uniqueBagOfWords.contains(st)) 
       uniqueBagOfWords.add(st); 

} 



for (String st : sentences) { 
    StringBuilder numOfOccurences = new StringBuilder(); 
    int[] array ={0,0,0,0,0,0,0,0,0,0}; 
    int num=0; 
    for (String s : st.split(" ")){ 
     num=0; 
     for (String word : uniqueBagOfWords) { 

      if (s.equals(word)) { 
       array[num] = array[num]+1; 
      } 
      num++; 
     } 
    } 
    num=0; 
    for(int number : array){ 
     numOfOccurences.append(number); 
    } 
    System.out.println(st + " = " + numOfOccurences); 

} 

這是我得到的輸出:

我喜歡打網球= 1111100000

我覺得足球需要大變化= 1000011111

我喜歡f ootball足球= 1100002000

相關問題