2016-08-12 118 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

相关问题