java weka stringtowordvector未正确计算单词出现次数 [英] java weka stringtowordvector is not counting word occurences properly
本文介绍了java weka stringtowordvector未正确计算单词出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我使用的是Weka机器学习库的JAVA API,并且我有以下代码:
so I'm using Weka Machine Learning Library's JAVA API and I have the following code:
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应该对字符串中出现的单词进行计数,而不是对"repeat"一词进行3次计数,但计数结果仅为1
though StringToWordVector is supposed to count the word occurences within the string, instead of having the word 'repeat' counted 3 times, the count only comes out as 1
我在做什么错?
推荐答案
Gee ...所有这些代码行.那几行呢?
Gee... all those lines of code. How about these few lines instead?
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}
这篇关于java weka stringtowordvector未正确计算单词出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文