2012-04-23 3 views
0

私はJavaにとってとても新しいので、私と一緒に裸にしてください。Javaプログラム - 単語統計

私は外出し、キーワードテキストファイルに基づいてさまざまなソーシャルメディアAPIから検索結果を返し、返された結果を別のテキストファイルに書き出すJavaプログラムを作成しました。

私は現在、検索結果テキストファイルをスキャンし、検索結果に「キーワード」が何回見つかったかに関する統計を返す別のクラスの別のクラスを作成しようとしています。私は基本的に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

私が正しく理解しているのであれば、count!= 0であるかどうかをチェックしてfrequencyDataマップに追加する別のif文を追加したいでしょうか? – BeRecursive

+0

あなたは正確に何を試しましたか?そのコードにkeywords.txtを扱う行はゼロになっています。それは、その証拠となる単一の行を持たずに動作させようとしていると述べると、非常に疑わしいと思えます。 –

答えて

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、My appologies。それは混乱していて全く機能しなかったので、私はInput StreamReaderを使って、keyword.txtファイルを処理しようとしたものは含まなかった。あなたが提供したコードをありがとう、非常に感謝し、私がしようとしていたとおりに正確に動作します。 –

+0

喜んで助けてください。 (StackOverflowエチケットの問題として)回答を受け入れてください(http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)。 –