質問

だから私はWeka Machine Learning LibraryのJava APIを使用しています。次のコードがあります。

    String html = "repeat repeat repeat";

    Attribute input = new Attribute("html",(FastVector) null);

    FastVector inputVec = new FastVector();
    inputVec.addElement(input);

    Instances htmlInst = new Instances("html",inputVec,1);
    htmlInst.add(new Instance(1));  
    htmlInst.instance(0).setValue(0, html);

    StringToWordVector filter = new StringToWordVector();
    filter.setUseStoplist(true);

    filter.setInputFormat(htmlInst);
    Instances dataFiltered = Filter.useFilter(htmlInst, filter);

    Instance last = dataFiltered.lastInstance();
    System.out.println(last);

stringtowordvectorは、「繰り返し」という単語を3回カウントする代わりに、文字列内で発生する単語をカウントすることになっていますが、カウントは1としてのみ出てきます

私は何が間違っているのですか?

役に立ちましたか?

解決

gee ...それらのすべてのコード行。代わりにこれらのいくつかの行はどうですか?

public static Map<String, Integer> countWords(String input) {
    Map<String, Integer> map = new HashMap<String, Integer>();
    Matcher matcher = Pattern.compile("\\b\\w+\\b").matcher(input);
    while (matcher.find())
        map.put(matcher.group(), map.containsKey(matcher.group()) ? map.get(matcher.group()) + 1 : 1);
    return map;
}

これが動作中のコードです:

public static void main(String[] args) {
    System.out.println(countWords("sample, repeat sample, of text"));
}

出力:

{of=1, text=1, repeat=1, sample=2}

他のヒント

デフォルトの設定は、0/1として存在/不在を報告することです。明示的にカウントを有効にする必要があります。追加:

filter.setOutputWordCounts(true);

そして再実行。

Wekaには明示的なメーリングリストがあります。そのような質問を投稿すると、より速い回答が得られるかもしれません。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top