2012-04-23 55 views
0

我對Java很新,所以請和我一起裸照。Java程序 - 字統計

我創建了一個Java程序,用於根據關鍵字文本文件從各種社交媒體API中返回搜索結果,並將返回的結果寫入另一個文本文件。

我現在試圖讓另一個班級單獨班級來掃描搜索結果文本文件,並返回搜索結果中找到'關鍵字'的次數的統計信息。我基本上試圖對keywords.txt文件運行搜索。

我一直試圖讓下面的代碼工作,但我只能得到它返回的所有單詞,而不是那些來自keywords.txt文件。

我試圖讓輸入StreamReader工作,但無濟於事。任何幫助將不勝感激。


package search; 

import java.util.*; // Provides TreeMap, Iterator, Scanner 
import java.io.*; // Provides FileReader, FileNotFoundException 

public class SearchResults 
{ 

    public static void main(String[] args) 
    { 
     TreeMap<String, Integer> frequencyData = new TreeMap<String, Integer>(); 
     readWordFile(frequencyData); 
     printAllCounts(frequencyData); 
    } 

    public static int getCount(String word, TreeMap<String, Integer> frequencyData) 
    { 
     if (frequencyData.containsKey(word)) 
     { // The word has occurred before, so get its count from the map 
      return frequencyData.get(word); // Auto-unboxed 
     } 
     else 
     { // No occurrences of this word 
      return 0; 
     } 
    } 

    public static void printAllCounts(TreeMap<String, Integer> frequencyData) 
    { 
     System.out.println("-----------------------------------------------"); 
     System.out.println(" Occurrences Word"); 

     for(String word : frequencyData.keySet()) 
     { 
      System.out.printf("%15d %s\n", frequencyData.get(word), word); 
     } 

     System.out.println("-----------------------------------------------"); 
    } 

    public static void readWordFile(TreeMap<String, Integer> frequencyData) 
    { 
     Scanner wordFile; 
     String word; // A word read from the file 
     Integer count; // The number of occurrences of the word 

     try 
     { 
      wordFile = new Scanner(new FileReader("SearchResults.txt")); 
     } 
     catch (FileNotFoundException e) 
     { 
      System.err.println(e); 
      return; 
     } 

     while (wordFile.hasNext()) 
     { 
      // Read the next word and get rid of the end-of-line marker if needed: 
      word = wordFile.next(); 

      // Get the current count of this word, add one, and then store the new count: 
      count = getCount(word, frequencyData) + 1; 
      frequencyData.put(word, count); 
     } 
    } 
} 
+0

如果我理解正確,你可能想添加另一個if語句來檢查count!= 0,然後將它添加到你的frequencyData映射中嗎? – BeRecursive 2012-04-23 08:49:34

+0

你究竟試過了什麼?該代碼中有零行處理keywords.txt。這聽起來很可疑,當你聲明你試圖讓它工作而沒有一條線就可以證明這一點。 – 2012-04-23 09:18:43

回答

1

在那裏,我清理你的代碼,增加了由keywords.txt找到的所有關鍵字過濾。

public static void main(String[] args) throws Exception { 
    printAllCounts(
    readWordFile("SearchResults.txt", loadKeywords("keywords.txt"))); 
} 

private static Map<String, Integer> readWordFile(
    String fname, Set<String> keywords) throws FileNotFoundException 
{ 
    final Map<String, Integer> frequencyData = new TreeMap<String, Integer>(); 
    for (Scanner wordFile = new Scanner(new FileReader(fname)); 
    wordFile.hasNext();) 
    { 
    final String word = wordFile.next(); 
    if (keywords.contains(word)) 
     frequencyData.put(word, getCount(word, frequencyData) + 1); 
    } 
    return frequencyData; 
} 

private static void printAllCounts(Map<String, Integer> frequencyData) { 
    System.out.println("-----------------------------------------------"); 
    System.out.println(" Occurrences Word"); 
    for(Map.Entry<String, Integer> e : frequencyData.entrySet()) 
    System.out.printf("%15d %s\n", e.getValue(), e.getKey()); 
    System.out.println("-----------------------------------------------"); 
} 

private static int getCount(String word, Map<String, Integer> frequencyData) { 
    return frequencyData.containsKey(word)? frequencyData.get(word) : 0; 
} 

private static Set<String> loadKeywords(String fname) 
throws FileNotFoundException 
{ 
    final Set<String> result = new HashSet<String>(); 
    for (Scanner s = new Scanner(new FileReader(fname)); s.hasNext();) 
    result.add(s.next()); 
    return result; 
} 
+0

感謝Marko,我的appologies。我沒有包括我曾嘗試使用Input StreamReader嘗試處理keyword.txt文件的原因,因爲它是一團糟,根本沒有任何功能。感謝您提供的代碼,非常感謝,它的工作原理與我正在嘗試的完全一樣。 – 2012-04-24 01:36:10

+0

很高興幫助。只是請,[接受](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)答案(作爲StackOverflow禮節的問題)。 – 2012-04-24 05:38:11