2010-12-16 32 views
0

我有一個問題來計算Java中的單詞。Java字計數器

我有一個地圖

Map<String,StringBuilder> files_and_text = new TreeMap<String,StringBuilder>(); 

字符串是一個文件名,而StringBuilder的包含文件的文本。

例如

StringBuilder file_tex = new StringBuilder(); 
StringBuilder file_tex2 = new StringBuilder(); 

file_text.append("some contents some file one"); 
files_and_tex.put("file1", file_text); 

file_text2.append("test words test test words");  
files_and_tex.put("file2", file_text2); 

現在我想打一個字典,可以告訴我:

  |word 1 | word 2 | word 3 ........ 
file 1 | 3  | 1 | 0 ......... 
file 2 | 6  | 2 | 9 ......... 
....... 
....... 

的話1,2,3等等都是語料庫的話。文件1,2,3等是文件名。此矩陣中的每個值表示在當前文件中出現此類單詞的次數。我最近從C移動到Java,我知道如何編寫凌亂的代碼(結構化的) 來解決這個問題;我想知道如何在純面向對象的風格,特別是在Java中。

注意:這不是一項任務!

回答

0

這裏是一個應該讓你去一個例子:

Map<String, StringBuilder> files_and_tex = new HashMap<String, StringBuilder>(); 

StringBuilder file_text = new StringBuilder(); 
StringBuilder file_text2 = new StringBuilder(); 
file_text.append("some contents some file one"); 
files_and_tex.put("file1", file_text); 

file_text2.append("test words test test words");  
files_and_tex.put("file2", file_text2); 

// Maps from file-name to word to count 
Map<String, Map<String, Integer>> wordCounts = 
     new HashMap<String, Map<String, Integer>>(); 

// Go through each filename (key in files_and_tex) 
for (String file : files_and_tex.keySet()) { 

    // Create a map to keep track of word counts for this file 
    Map<String, Integer> wc = new HashMap<String, Integer>(); 
    wordCounts.put(file, wc); 

    Scanner s = new Scanner("" + files_and_tex.get(file)); 
    while (s.hasNext()) { 
     String word = s.next(); 
     if (!wc.containsKey(word)) 
      wc.put(word, 0); 
     wc.put(word, wc.get(word) + 1); 
    } 
} 

// And here is how to access the resulting data 
System.out.println(wordCounts.get("file1").get("file")); // prints 1 
System.out.println(wordCounts.get("file2").get("test")); // prints 3 

順便說一句,在Java約定建議駝峯式的標識符。

+0

也許if(!wc.containsKey(word))wc.put(word,1); else wc.put(word,wc.get(word)+1);'?或'得到;檢查null,把1,否則得到+ 1'? – khachik 2010-12-16 16:49:42

+0

我認爲目前的解決方案更清潔。如果沒有值存在,它將被視爲0,並且無論如何,當前值應該增加1. – aioobe 2010-12-16 16:55:07

+0

感謝您的示例,但它不創建字典,因爲我在我的問題 – Tweet 2010-12-16 17:25:05

3

谷歌的Guava Libraries有一些非常有用的工具和數據結構來解決這類問題。

將文件分割成的話,你可以使用分配器:

Iterable<String> wordsInFile = 
    Splitter.on(' ').trimResuls().omitEmptyStrings().split(fileAsString);

要統計一個給定的詞的出現,你可以使用多重集:

Multiset<String> countOfEachWord = HashMultiset.create(); 
countOfEachWord.addAll(wordsInFile);

你可以建立在這些兩部分來創建一些像WordLookupTable這樣的對象。即:

public class WordLookupTable { 

    private static final Splitter SPLITTER = Splitter.on(' ').trimResults().omitEmptyStrings(); 
    private final Map<String, Multiset<String>> filenameToWordCountSet = Maps.newHashMap(); 

    public void addFile(String filename, String fileText) { 
    Multiset<String> wordsInFile = getWordSetForFile(filename); 

    for (String word : SPLITTER.split(fileText)) { 
     wordsInFile.add(word); 

    } 
    } 

    // Gets the count of all words for the file 
    public long getCountOfWordsForFile(String filename) { 
    return getWordSetForFile(filename).size(); 

    } 

    public long getCountOfWordInFile(String filename, String word) { 
    return getWordSetForFile(filename).count(word); 
    } 

    public long getCountOfWordOverAllFiles(String word) { 
    long count = 0; 
    for (Multiset<String> wordSet : filenameToWordCountSet.values()) { 
     count += wordSet.count(word); 
    } 
    return count; 
    } 

    private Multiset<String> getWordSetForFile(String filename) { 
    Multiset<String> wordsInFile = filenameToWordCountSet.get(filename); 
    if(wordsInFile == null) { 
     wordsInFile = HashMultiset.create(); 
     filenameToWordCountSet.put(filename, wordsInFile); 
    } 
    return wordsInFile; 
    } 
}
1

有很多方法可以做到這一點,讓我向你解釋的方式,既高效又易於理解。當然OO的和。

[步驟1]你必須有兩張地圖,一張存儲文件特定數據,另一張存儲文件名和文件數據。你可以選擇任何你想要的,而不是文件名。

private static HashMap<String, MutableInt> wordMap1 = new HashMap<String, MutableInt>(); 
private static HashMap<String, MutableInt> wordMap2 = new HashMap<String, MutableInt>(); 
private static HashMap<String, HashMap> fileMap = new HashMap<String, HashMap>(); 

[步驟2]對MutableInt類(在技術上你想這樣做第一) 現在你可能會問,什麼是MutableInt,它是您將創建,這樣就可以增加一個給定值的一類就像你遇到它一樣。

這裏是MutableInt類的例子:

class MutableInt { 
    int value = 1; 
    public void increase() { ++value; } 
    public int getValue() { return value; } 
    public String toString(){ 
     return Integer.toString(value); 
    } 
} 

[步驟3]現在,對於給定文件中的每個單詞執行以下操作:

  1. 文件創建一個新的wordMap你解析
  2. 從文件中獲取單詞
  3. 使用wordmap.get(「word」)檢查單詞是否在wordMap中。
  4. 如果輸出爲空,那麼你知道它是一個新單詞。
  5. 把這個詞在地圖上,並使用
  6. wordmap.put('字」,新MutableInt())把MutableInt它的價值;
  7. 如果輸出不爲空,那麼你知道有這不是一個新單詞使用wordMap.getValue(「word」.increase();)來增加計數器。
  8. 一旦你完成了這個與文件中的所有單詞,你想把wordMap放入fileMap使用fileMap.put(「文件名」,wordMap);