2012-04-23 21 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