【问题标题】:Extending my program to report frequency of word in list and its line number?扩展我的程序以报告列表中单词的频率及其行号?
【发布时间】:2013-12-30 04:31:18
【问题描述】:

到目前为止,我的 Java 代码如下所示。现在我要做的是添加到它,以便它可以报告列表中每个单词的频率(它存在的次数)以及该单词出现的行号。

代码:

// Concordance

import java.util.*;

// A concordance is a listing of words from a text, with each word being followed the line/page numbers on which the word appears. 

public class Concordance 
{

    private Dictionary dict = new Hashtable();

    private boolean allowDupl = true;

    public Concordance (boolean allowDupl ) 
    {
        this.allowDupl = allowDupl;
    } // end Concordance()

    public Concordance ( ) { this(true); }

    public void enterWord (Object word, Integer line) 
    {
        Vector set = (Vector) dict.get(word);
        if (set == null) // word not in dictionary
        {  
        set = new Vector( );
        dict.put(word, set);  // enter word and empty Vector
        } 
        if (allowDupl || !set.contains(line))
        { 
        set.addElement(line);
        }
    } // end enterWord()

    public Enumeration keys( ) 
    {
        return dict.keys( );
    } // end keys()

    public Enumeration getNumbers (Object word) 
    {
        return ((Vector)dict.get(word)).elements( );
    } // end getNumbers()

} // end class Concordance

这是我一直在尝试的东西,但 Java 根本不是我的强语言。任何人都可以提供有关如何进行的建议吗?

编辑:

我已使用以下内容更新了我的代码。对于那些更熟悉 Java 的人来说,这看起来是否正确?

代码:

// Concordance

import java.util.*;

// A concordance is a listing of words from a text, with each word being followed the line/page numbers on which the word appears. 

public class Concordance 
{

    private Dictionary dict = new Hashtable();

    private boolean allowDupl = true;

    public Concordance (boolean allowDupl ) 
    {
        this.allowDupl = allowDupl;
    } // end Concordance()

    public Concordance ( ) { this(true); }

    public void enterWord (Object word, Integer line) 
    {
        Vector set = (Vector) dict.get(word);
        if (set == null)  // word not in dictionary
        {
        set = new Vector( );
        dict.put(word, set);  // enter word and empty Vector
        } 
        if (allowDupl || !set.contains(line)) 
        {
        set.addElement(line);
        }
    } // end enterWord()


    public void generateOutput(PrintStream output) 
    {
        Enumeration e = dict.keys();
        while (e.hasMoreElements()) 
        {
            String word = (String) e.nextElement();
            Vector set = (Vector) dict.get(word);
            output.print(word + ": ");
            Enumeration f = set.elements();
        }
        while (f.hasMoreElements())
        {
            output.print(f.nextElement() + " ");
            output.println("");
        }
    }

    public Enumeration keys( ) 
    {
        return dict.keys( );
    } // end keys()

    public Enumeration getNumbers (Object word) 
    {
        return ((Vector)dict.get(word)).elements( );
    } // end getNumbers()

} // end class Concordance

【问题讨论】:

  • 您需要定义/解释“报告”。
  • 报告、写入、显示、弹出、单词存在的次数以及在哪些行号。
  • 您希望界面报告是什么?例如,类应该写入标准输出或文件,创建对话框,还是简单地返回正确的值?它应该对一个单词或所有单词执行此操作,如果是,它们应该按任何特定顺序进行吗?等等等等。

标签: java class dictionary hashtable


【解决方案1】:

创建一个类DictionaryEntry,它具有您需要的所有属性,然后将其放入您的Dictionary 而不是一组int 作为行号。

【讨论】:

  • 埃德加,不确定我是否关注?
  • 如果我理解正确的话,您希望将多个属性与每个单词关联起来。因此,最简单的方法是将所有这些属性放入一个类中,然后将您的单词映射为dict.put(word, dictionaryEntry);,其中 dictionaryEntry 是您的具有属性的类
猜你喜欢
  • 1970-01-01
  • 2011-12-17
  • 1970-01-01
  • 2013-12-28
  • 1970-01-01
  • 2022-08-21
  • 1970-01-01
相关资源
最近更新 更多