【问题标题】:Java Program - Word StatisticsJava 程序 - 单词统计
【发布时间】:2012-05-03 21:03:51
【问题描述】:

我创建了一个 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);
        }
    } 
}

【问题讨论】:

  • 如果我理解正确,您是否想添加另一个 if 语句来检查 count != 0 是否为 0,然后才将其添加到您的 frequencyData 映射中?
  • 你到底尝试了什么?该代码中有零行处理keywords.txt。当你说你试图让它工作而没有一行可以作为证据的情况下,这听起来很可疑。

标签: java count statistics word keyword


【解决方案1】:

在那里,我清理了您的代码并添加了按关键字.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;
}

【讨论】:

  • 谢谢马尔科,我的道歉。我没有包括我尝试使用 Input StreamReader 来尝试处理 keyword.txt 文件的内容,因为它一团糟而且根本不起作用。感谢您提供的代码,非常感谢,它完全按照我的想法工作。
  • 很高兴为您提供帮助。请accept 回答(作为 StackOverflow 礼节的问题)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-10
相关资源
最近更新 更多