/** * Returns the item to insert from these referenced matches, or null if none * * @param label the label of the matches */ public Item toItem(String label) { if (matches.size()==0) return null; if (matches.size()==1) return matches.get(0).toItem(label); PhraseItem phrase=new PhraseItem(); // TODO: Somehow allow AND items instead here phrase.setIndexName(label); for (Iterator<Match> i=matches.iterator(); i.hasNext(); ) { phrase.addItem(i.next().toItem(label)); } return phrase; }
/** * Convert AndItem to PhraseItem<br> * * e.g. (AND a b) to "a b" * @param andItem query tree to be converted * @return converted PhraseItem */ private static PhraseItem convertAndToPhrase(AndItem andItem) { PhraseItem result = new PhraseItem(); Iterator<Item> subItems = andItem.getItemIterator(); while(subItems.hasNext()) { Item curr = (subItems.next()); if(curr instanceof IntItem) { WordItem numItem = new WordItem(((IntItem)curr).stringValue()); result.addItem(numItem); } else { result.addItem(curr); } } return result; }
public void replace() { PhraseItem phrase=new PhraseItem(); TermItem firstWord=(TermItem)owner.setItem(startIndex,phrase); replace(firstWord,0); phrase.setIndexName(firstWord.getIndexName()); phrase.addItem(firstWord); for (int i=1; i<length; i++) { TermItem followingWord=(TermItem)owner.removeItem(startIndex+1); replace(followingWord,i); phrase.addItem(followingWord); } }
private void replaceItemByGrams(Item item, Item grams, int indexInParent) { if (!(grams instanceof CompositeItem) || !(item.getParent() instanceof PhraseItem)) { // usually, simply replace item.getParent().setItem(indexInParent, grams); } else { // but if the parent is a phrase, we cannot add the AND to it, so add each gram to the phrase PhraseItem phraseParent = (PhraseItem)item.getParent(); phraseParent.removeItem(indexInParent); int addedTerms = 0; for (Iterator<Item> i = ((CompositeItem)grams).getItemIterator(); i.hasNext(); ) { phraseParent.addItem(indexInParent+(addedTerms++),i.next()); } } }
case PHRASE: if (getAnnotation(word, ORIGIN, Map.class, null, ORIGIN_DESCRIPTION, false) == null) { phrase.addItem(instantiatePhraseItem(field, word)); } else { phrase.addItem(instantiatePhraseSegmentItem(field, word, true)); phrase.addItem(instantiateWordAlternativesItem(field, word)); break; default: phrase.addItem(instantiateWordItem(field, word, phrase.getClass()));
/** * Add custom highlight phrase * @param field Field name * @param phrase List of terms to be highlighted as a phrase */ public void addHighlightPhrase(String field, List<String> phrase) { PhraseItem pi = new PhraseItem(); pi.setIndexName(field); for (String s : phrase) { pi.addItem(new WordItem(toLowerCase(s), field, true)); } addHighlightItem(field, pi); }
@NonNull private Item instantiatePhraseItem(String field, String key, Inspector value) { assertHasOperator(key, PHRASE); PhraseItem phrase = new PhraseItem(); phrase.setIndexName(field); HashMap<Integer, Inspector> children = getChildrenMap(value); for (Inspector word : children.values()) { if (word.type() == STRING) phrase.addItem(new WordItem(word.asString())); else if (word.type() == OBJECT && word.field(PHRASE).valid()) phrase.addItem(instantiatePhraseItem(field, key, getChildren(word))); } return leafStyleSettings(getAnnotations(value), phrase); }
phrase.addItem(word); } else if (firstWord != null) { phrase = new PhraseItem(); phrase.addItem(MarkerWordItem.createStartOfHost()); true, asInt.getOrigin()); phrase.addItem(firstWord); phrase.addItem(word); } else if (word instanceof PhraseItem) { phrase = (PhraseItem) word; phrase.addItem(MarkerWordItem.createEndOfHost()); phrase = new PhraseItem(); if (addStartOfHostMarker) { phrase.addItem(MarkerWordItem.createStartOfHost()); firstWord = new WordItem(asInt.stringValue(), asInt.getIndexName(), true, asInt.getOrigin()); phrase.addItem(firstWord); if (!starAfterFirst) { phrase.addItem(MarkerWordItem.createEndOfHost());
public void produce(RuleEvaluation e,int offset) { PhraseItem newPhrase=new PhraseItem(); newPhrase.setIndexName(getLabel()); for (String term : terms) newPhrase.addItem(new WordItem(term)); if (replacing) { Match matched=e.getNonreferencedMatch(0); insertMatch(e,matched,newPhrase,offset); } else { newPhrase.setWeight(getWeight()); if (e.getTraceLevel()>=6) e.trace(6,"Adding '" + newPhrase + "'"); e.addItem(newPhrase,getTermType()); } }