private Item collapsePhrase(PhraseItem root) { if (root.getItemCount() == 1) return root.getItem(0); else return root; }
private void repeatedConsecutiveTermsInPhraseCheck(PhraseItem phrase) { if (phrase.getItemCount() > MAX_REPEATED_CONSECUTIVE_TERMS_IN_PHRASE) { String prev = null; int repeatedCount = 0; for (int i = 0; i < phrase.getItemCount(); ++i) { Item item = phrase.getItem(i); if (item instanceof TermItem) { TermItem term = (TermItem) item; String current = term.getIndexedString(); if (prev != null) { if (prev.equals(current)) { repeatedCount++; if (repeatedCount >= MAX_REPEATED_CONSECUTIVE_TERMS_IN_PHRASE) { repeatedConsecutiveTermsInPhraseRejections.add(); throw new IllegalArgumentException("More than " + MAX_REPEATED_CONSECUTIVE_TERMS_IN_PHRASE + " ocurrences of term '" + current + "' in a row detected in phrase : " + phrase.toString()); } } else { repeatedCount = 0; } } prev = current; } else { prev = null; repeatedCount = 0; } } } } private static final class Count {
for (int i = 0; i < phrase.getItemCount(); ++i) { if (i > 0) { destination.append(", ");
private void repeatedTermsInPhraseCheck(PhraseItem phrase) { if (phrase.getItemCount() > MAX_REPEATED_TERMS_IN_PHRASE) { Map<String, Count> repeatedCount = new HashMap<>(); for (int i = 0; i < phrase.getItemCount(); ++i) { Item item = phrase.getItem(i); if (item instanceof TermItem) { TermItem term = (TermItem) item; String current = term.getIndexedString(); Count count = repeatedCount.get(current); if (count != null) { if (count.get() >= MAX_REPEATED_TERMS_IN_PHRASE) { repeatedTermsInPhraseRejections.add(); throw new IllegalArgumentException("Phrase contains more than " + MAX_REPEATED_TERMS_IN_PHRASE + " occurrences of term '" + current + "' in phrase : " + phrase.toString()); } count.inc(); } else { repeatedCount.put(current, new Count(1)); } } } } }
private static Item collapsePhrase(PhraseItem phrase) { if (phrase.getItemCount() == 1 && phrase.getItem(0) instanceof WordItem) { // TODO: Other stuff which needs propagation? WordItem word = (WordItem) phrase.getItem(0); word.setWeight(phrase.getWeight()); return word; } else { return phrase; } }