到目前为止,我已经获得了我的Java代码。现在,我正在尝试添加它,以便它可以为列表中的每个单词报告其频率(存在的次数)以及单词出现的行号。
CODE:
// 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的人来说,这看起来是否正确?
CODE:
// 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
答案 0 :(得分:0)
创建一个具有您需要的所有属性的类DictionaryEntry
,然后将其放入Dictionary
,而不只是一组int
的行号。