@NonNull private Item instantiateEquivItem(String field, OperatorNode<ExpressionOperator> ast) { List<OperatorNode<ExpressionOperator>> args = ast.getArgument(1); Preconditions.checkArgument(args.size() >= 2, "Expected 2 or more arguments, got %s.", args.size()); EquivItem equiv = new EquivItem(); equiv.setIndexName(field); for (OperatorNode<ExpressionOperator> arg : args) { switch (arg.getOperator()) { case LITERAL: equiv.addItem(instantiateWordItem(field, arg, equiv.getClass())); break; case CALL: assertHasFunctionName(arg, PHRASE); equiv.addItem(instantiatePhraseItem(field, arg)); break; default: throw newUnexpectedArgumentException(arg.getOperator(), ExpressionOperator.CALL, ExpressionOperator.LITERAL); } } return leafStyleSettings(ast, equiv); }
private static void removeDuplicates(EquivItem composite) { int origSize = composite.getItemCount(); for (int i = origSize - 1; i >= 1; --i) { Item deleteCandidate = composite.getItem(i); for (int j = 0; j < i; ++j) { Item check = composite.getItem(j); if (deleteCandidate.getClass() == check.getClass()) { if (deleteCandidate instanceof PhraseItem) { PhraseItem phraseToCheck = (PhraseItem) check; if (phraseDeletionCandidate.getIndexedString().equals(phraseToCheck.getIndexedString())) { composite.removeItem(i); break; PhraseSegmentItem phraseSegmentToCheck = (PhraseSegmentItem) check; if (phraseSegmentDeletionCandidate.getIndexedString().equals(phraseSegmentToCheck.getIndexedString())) { composite.removeItem(i); break; BlockItem blockToCheck = (BlockItem) check; if (blockDeletionCandidate.stringValue().equals(blockToCheck.stringValue())) { composite.removeItem(i); break;
@Override boolean serialize(StringBuilder destination, Item item) { EquivItem e = (EquivItem) item; String annotations = leafAnnotations(e); destination.append(getIndexName(e.getItem(0))).append(" contains "); if (annotations.length() > 0) { destination.append("([{").append(annotations).append("}]"); } destination.append(EQUIV).append('('); int initLen = destination.length(); for (Iterator<Item> i = e.getItemIterator(); i.hasNext();) { Item x = i.next(); if (destination.length() > initLen) { destination.append(", "); } if (x instanceof PhraseItem) { PhraseSerializer.serialize(destination, x, false); } else { destination.append('"'); escape(((IndexedItem) x).getIndexedString(), destination); destination.append('"'); } } if (annotations.length() > 0) { destination.append(')'); } destination.append(')'); return false; }
/** * create an EQUIV with the given item as child. * The new EQUIV will take connectivity, * significance and weight from the given item. * * @param item Will be modified and added as a child. **/ public EquivItem(Item item) { addItem(item); // steal other item's connectivity: if (item.connectedItem != null) { setConnectivity(item.connectedItem, item.connectivity); item.connectedItem = null; item.connectivity = 0.0; } TaggableItem back = (TaggableItem)item.connectedBacklink; if (back != null) { back.setConnectivity(this, back.getConnectivity()); item.connectedBacklink = null; } // steal other item's significance: if (item.explicitSignificance) { setSignificance(item.significance); } // steal other item's weight: setWeight(item.getWeight()); // we have now stolen all of the other item's unique id needs: item.setHasUniqueID(false); }
/** * create an EQUIV with the given item and a set * of alternate words as children. * The new EQUIV will take connectivity, * significance and weight from the given item. * * @param item Will be modified and added as a child. * @param words Set of words to create WordItems from. **/ public EquivItem(Item item, Collection<String> words) { this(item); String idx = ((IndexedItem)item).getIndexName(); for (String word : words) { WordItem witem = new WordItem(word, idx); addItem(witem); } } }
@NonNull private Item instantiateEquivItem(String field, String key, Inspector value) { HashMap<Integer, Inspector> children = getChildrenMap(value); Preconditions.checkArgument(children.size() >= 2, "Expected 2 or more arguments, got %s.", children.size()); EquivItem equiv = new EquivItem(); equiv.setIndexName(field); for (Inspector word : children.values()){ if (word.type() == STRING || word.type() == LONG || word.type() == DOUBLE){ equiv.addItem(new WordItem(word.asString(), field)); } if (word.type() == OBJECT){ word.traverse((ObjectTraverser) (key2, value2) -> { assertHasOperator(key2, PHRASE); equiv.addItem(instantiatePhraseItem(field, key2, value2)); }); } } return leafStyleSettings(getAnnotations(value), equiv); }