我正在构建一个必须阅读大量文本文档的分类器,但我发现我的countWordFrequenties方法越慢,它处理的文档就越多.下面的这个方法花了60ms(在我的电脑上),而阅读,规范化,标记化,更新我的词汇和均衡不同的整数列表只需要3-5ms(在我的电脑上).我的countWordFrequencies方法如下:
public List<Integer> countWordFrequencies(String[] tokens)
{
List<Integer> wordFreqs = new ArrayList<>(vocabulary.size());
int counter = 0;
for (int i = 0; i < vocabulary.size(); i++)
{
for (int j = 0; j < tokens.length; j++)
if (tokens[j].equals(vocabulary.get(i)))
counter++;
wordFreqs.add(i, counter);
counter = 0;
}
return wordFreqs;
}
Run Code Online (Sandbox Code Playgroud)
加快这个过程的最佳方法是什么?这个方法有什么问题?
这是我的整个班级,还有另一个班级类别,在这里发布这个也不错,或者你们不需要吗?
public class BayesianClassifier
{
private Map<String,Integer> vocabularyWordFrequencies;
private List<String> vocabulary;
private List<Category> categories;
private List<Integer> wordFrequencies;
private int trainTextAmount;
private int testTextAmount;
private GUI gui;
public BayesianClassifier()
{ …Run Code Online (Sandbox Code Playgroud)