使用 StringTokenizer 统计每个词出现的频率
Use StringTokenizer to count frequency of each word
我对我的作业有几个问题。
作业是让用户输入一个句子,程序统计每个单词的出现频率,当用户输入一个空字符串时,程序退出。此外,该程序区分大小写。例如,Apple is an apple is a phone,结果就是Apple-1;是-2;一个-1; a-1; phone-1。这是我的代码:
public static void main(String[] args)
{
while (true)
{
System.out.println("Enter a sentence:");
Scanner keyboard = new Scanner(System.in);
String sentence = keyboard.nextLine();
if (sentence.isEmpty()) // quit the program when user enter an empty string
{
break;
}
else
{
StringTokenizer st = new StringTokenizer(sentence);
while (st.hasMoreTokens())
{
List<String> sentenceElement = new ArrayList<String>();
sentenceElement.add(st.nextToken());
}
System.out.println(sentenceElement);
}
}
我有几个问题。
- 我尝试将所有的token保存到一个名为sentenceElement的数组中,并尝试输出,但是失败了。编译器显示
error: cannot find symbol
System.out.println(sentenceElement);
- 如何计算每个词出现的频率?
非常感谢,非常感谢您的回答和解决方案。
- 如何计算每个词出现的频率?
使用 HashMap 将单词存储为键,将计数存储为值。然后遍历所有单词,如果它 return 为 null,则首先从 hashmap 中获取单词作为键,然后将字母添加到值为 1 的 hashmap,如果循环中出现相同的键,那么 hashmap 的获取将不会 return null 它将 return 旧计数,即 1 不会将其递增到 2,并在所有单词完成后再次将其存储回去,您的 hashmap 中有计数,只需迭代它并打印 key->value 。
您可以使用
将输入转换为标记
String tokens[]=input.split(" ");
接下来就是统计每个词出现的频率。您可以为此使用 Hashmap。
HashMap < String, Integer > hmap = new HashMap < Integer, String > ();
for (str: tokens) {
if (hmap.get(str) == null) hmap.put(str, 1);
else hmap.put(str, hmap.get(str) + 1);
}
Iterator it = hmap.iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
System.out.println(pair.getKey() + " = " + pait.getValue());
it.remove();
}
我对我的作业有几个问题。
作业是让用户输入一个句子,程序统计每个单词的出现频率,当用户输入一个空字符串时,程序退出。此外,该程序区分大小写。例如,Apple is an apple is a phone,结果就是Apple-1;是-2;一个-1; a-1; phone-1。这是我的代码:
public static void main(String[] args)
{
while (true)
{
System.out.println("Enter a sentence:");
Scanner keyboard = new Scanner(System.in);
String sentence = keyboard.nextLine();
if (sentence.isEmpty()) // quit the program when user enter an empty string
{
break;
}
else
{
StringTokenizer st = new StringTokenizer(sentence);
while (st.hasMoreTokens())
{
List<String> sentenceElement = new ArrayList<String>();
sentenceElement.add(st.nextToken());
}
System.out.println(sentenceElement);
}
}
我有几个问题。
- 我尝试将所有的token保存到一个名为sentenceElement的数组中,并尝试输出,但是失败了。编译器显示
error: cannot find symbol System.out.println(sentenceElement);
- 如何计算每个词出现的频率?
非常感谢,非常感谢您的回答和解决方案。
- 如何计算每个词出现的频率?
使用 HashMap 将单词存储为键,将计数存储为值。然后遍历所有单词,如果它 return 为 null,则首先从 hashmap 中获取单词作为键,然后将字母添加到值为 1 的 hashmap,如果循环中出现相同的键,那么 hashmap 的获取将不会 return null 它将 return 旧计数,即 1 不会将其递增到 2,并在所有单词完成后再次将其存储回去,您的 hashmap 中有计数,只需迭代它并打印 key->value 。
您可以使用
将输入转换为标记 String tokens[]=input.split(" ");
接下来就是统计每个词出现的频率。您可以为此使用 Hashmap。
HashMap < String, Integer > hmap = new HashMap < Integer, String > ();
for (str: tokens) {
if (hmap.get(str) == null) hmap.put(str, 1);
else hmap.put(str, hmap.get(str) + 1);
}
Iterator it = hmap.iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
System.out.println(pair.getKey() + " = " + pait.getValue());
it.remove();
}