throws IOException, InterruptedException {
Iterator<StringTuple> it = values.iterator();
if (!it.hasNext()) {
return;
}
StringTuple value = it.next();
Vector vector = new RandomAccessSparseVector(dimension, value.length()); // guess at initial size
if (maxNGramSize >= 2) {
ShingleFilter sf = new ShingleFilter(new IteratorTokenStream(value.getEntries().iterator()), maxNGramSize);
sf.reset();
try {
do {
String term = sf.getAttribute(CharTermAttribute.class).toString();
if (!term.isEmpty() && dictionary.containsKey(term)) { // ngram
int termId = dictionary.get(term);
vector.setQuick(termId, vector.getQuick(termId) + 1);
}
} while (sf.incrementToken());
sf.end();
} finally {
Closeables.close(sf, true);
}
} else {
for (String term : value.getEntries()) {
if (!term.isEmpty() && dictionary.containsKey(term)) { // unigram
int termId = dictionary.get(term);
vector.setQuick(termId, vector.getQuick(termId) + 1);
}
}