首页 > 解决方案 > 在 Java 中向 Concordance 函数添加频率计数

问题描述

希望得到关于我的逻辑是否正确的意见。我的任务只是修改函数以跟踪词频。我想我已经添加了一个计数器来做到这一点。

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

} // enterWord

原始代码示例输入:

this is just a test or is it
is
just
a
test

输出:

a: 1 4
this: 1
it: 1
is: 1 1 2
or: 1
just: 1 3
test: 1 5

使用我添加的计数器,输出如下所示:

a: 1 1 4 1
this: 1 1
it: 1 1
is: 1 1 1 1 2 1
or: 1 1
just: 1 1 3 1
test: 1 1 5 1

标签: javadictionaryhashtableword-frequency

解决方案


推荐阅读