【发布时间】: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