private static <T> Set<T> generateMappedSet(Set<T> originalSet, BidirectionalMap<T, T> mapOrigToGenerated) { Set<T> ret = new LinkedHashSet<T>(); for (T orig : originalSet) { ret.add( mapOrigToGenerated.leftGet(orig) ); } return ret; }
/** * Ctor * @param originalTree * @param generatedTree * @param bidiMap * @throws AnnotatorException */ public TwoTreesAndTheirBidirectionalMap(N originalTree, M generatedTree, BidirectionalMap<N, M> bidiMap) throws AnnotatorException { super(); this.originalTree = originalTree; this.generatedTree = generatedTree; this.bidiMap = bidiMap; if (bidiMap.leftGet(originalTree) != generatedTree) throw new AnnotatorException("The two roots do not map in the given bidirectional map:\n " + originalTree +"\n"+generatedTree); }
public void annotateTheTree() throws AnnotationOperationException { treeWasAltered = false; // cross the tree-to-LHS map with the LHS-to-RHS-annotations map Map<ExtendedConstructionNode, BasicRuleAnnotations> mapTreeNodesToAnnotations = new LinkedHashMap<ExtendedConstructionNode, BasicRuleAnnotations>(); for (ExtendedNode lhsNode : mapLhsToAnnotations.keySet()) { if (!mapLhsToTree.leftContains(lhsNode)) throw new AnnotationOperationException("Matching bug! This node is mapped in the rule but not matched against the text tree: " + lhsNode); mapTreeNodesToAnnotations.put(mapLhsToTree.leftGet(lhsNode), mapLhsToAnnotations.get(lhsNode)); } // replace annotations in the tree replaceAnnotationsOfTree(treeAndParentMap.getTree(), mapTreeNodesToAnnotations); affectedNodes = mapTreeNodesToAnnotations.keySet(); }
private String debug_print_mapLhsToTree() { StringBuilder sb = new StringBuilder(); for (RN ruleNode : mapLhsToTree.leftSet()) { String ruleLemma = "null"; if (ruleNode!=null) {ruleLemma = InfoGetFields.getLemma(ruleNode.getInfo());} TN treeNode = mapLhsToTree.leftGet(ruleNode); String treeLemma = "null"; if (treeNode!=null) {treeLemma = InfoGetFields.getLemma(treeNode.getInfo());} sb.append(ruleLemma).append(" --> ").append(treeLemma).append("\n"); } return sb.toString(); }
protected void updateAntecedents() { for (ExtendedNode generatedNode : mapGeneratedToOrig.keySet()) { if (mapGeneratedToOrig.get(generatedNode).getAntecedent()!=null) { if (mapOrigToGenerated.leftContains(mapGeneratedToOrig.get(generatedNode).getAntecedent())) { generatedNode.setAntecedent(mapOrigToGenerated.leftGet(mapGeneratedToOrig.get(generatedNode).getAntecedent())); } } } }
private void updateAntecedents() { Set<ExtendedNode> originalTreeNodes = AbstractNodeUtils.treeToLinkedHashSet(textTree.getTree()); for (ExtendedNode originalTreeNode : originalTreeNodes) { if (originalTreeNode.getAntecedent()!=null) { ExtendedNode originalTreeNodeAntecedent = originalTreeNode.getAntecedent(); mapOrigToGenerated.leftGet(originalTreeNode).setAntecedent(mapOrigToGenerated.leftGet(originalTreeNodeAntecedent)); } } }
public static <L, R, M> BidirectionalMap<L, R> concatenateBidiMaps(BidirectionalMap<L, M> map1,BidirectionalMap<M, R> map2) throws TeEngineMlException { boolean failed = false; BidirectionalMap<L, R> ret = new SimpleBidirectionalMap<L, R>(); for (L l : map1.leftSet()) { M m = map1.leftGet(l); if (!map2.leftContains(m)) failed=true; ret.put(l, map2.leftGet(m) ); } if (failed) throw new TeEngineMlException("Concatenation failed."); else return ret; } }
protected void flipToValueSetMap() { this.valueSetMap = new SimpleValueSetMap<K, V>(); for (K key : this.bidirectionalMap.leftSet()) { V value = this.bidirectionalMap.leftGet(key); this.valueSetMap.put(key, value); } this.bidirectionalMap = null; }
/** * @param node * @param mapNodeToId * @param id * @param nodeToConllStringConvertor * @param variableId * @return * @throws ConllConverterException */ private static <I extends Info, N extends AbstractNode<I, N>> StringBuilder nodeToConll(N node, BidirectionalMap<N, N> alignments, Map<N, Integer> mapNodeToId, RuleConllStringConverter<I, N> nodeToConllStringConvertor) throws ConllConverterException { StringBuilder conllStr = new StringBuilder(); int variableId = -1; // -1 means no alignment if (alignments.leftContains(node)) variableId = mapNodeToId.get(alignments.leftGet(node)); else if (alignments.rightContains(node)) variableId = mapNodeToId.get(alignments.rightGet(node)); conllStr.append(nodeToConllStringConvertor.convert(node, mapNodeToId, variableId)).append('\n'); if (node.hasChildren()) for (N child : node.getChildren()) conllStr.append(nodeToConll(child, alignments , mapNodeToId, nodeToConllStringConvertor)); return conllStr; }
@Override protected void generateTheTree() throws OperationException { this.affectedNodes = new LinkedHashSet<ExtendedNode>(); mapOrigToGenerated = new SimpleBidirectionalMap<ExtendedNode, ExtendedNode>(); this.generatedTree = copyTree(this.textTree.getTree()); updateAntecedents(); ExtendedNode parent = mapOrigToGenerated.leftGet(whereToInsert); ExtendedNode insertedNode = new ExtendedNode(nodeToInsert); // ExtendedNode insertedNode = new ExtendedNode(new ExtendedInfo(nodeToInsert.getId(),nodeToInsert.getNodeInfo(),nodeToInsert.getEdgeInfo(),AdditionalInformationServices.emptyInformation())); affectedNodes.add(insertedNode); parent.addChild(insertedNode); }
/** * Adds all (L,R) keys-values from <code>mapToAdd</code> to <code>map</code> * @param <L> * @param <R> * @param map * @param mapToAdd */ public static <L,R> void BidiMapAddAll(BidirectionalMap<L, R> map, BidirectionalMap<L, R> mapToAdd) { for (L l : mapToAdd.leftSet()) { if (!map.leftContains(l)) { map.put(l, mapToAdd.leftGet(l)); } } }
private static <T> ValueSetMap<T, T> generateMapWithMappedValues(BidirectionalMap<T, T> bidiMap, ValueSetMap<T, T> originalValueSetMap) { ValueSetMap<T, T> ret = new SimpleValueSetMap<T, T>(); for (T element : originalValueSetMap.keySet()) { for (T mappedInOriginal : originalValueSetMap.get(element)) { ret.put(element, bidiMap.leftGet(mappedInOriginal)); } } return ret; }
private void postProcessContentAncestor() throws TeEngineMlException { ContentAncestorSetter ancestorSetter = new ContentAncestorSetter(generatedTree); ancestorSetter.generate(); BidirectionalMap<ExtendedNode, ExtendedNode> mapping = ancestorSetter.getNodesMap(); ExtendedNode newTree = mapping.leftGet(generatedTree); Set<ExtendedNode> newAffectedNodes = generateMappedSet(affectedNodes, mapping); ValueSetMap<ExtendedNode, ExtendedNode> newMapOriginalToGenerated = generateMapWithMappedValues(mapping,mapOriginalToGenerated); generatedTree = newTree; affectedNodes = newAffectedNodes; mapOriginalToGenerated = newMapOriginalToGenerated; }
protected void updateAntecedentInformation() { for (ExtendedNode newNode : mapOrigToCopy.rightSet()) { ExtendedNode originalNode = mapOrigToCopy.rightGet(newNode); if (originalNode.getAntecedent()!=null) { ExtendedNode originalNodeAntecedent = originalNode.getAntecedent(); ExtendedNode newNodeAntecedent = mapOrigToCopy.leftGet(originalNodeAntecedent); newNode.setAntecedent(newNodeAntecedent); } } }
@Override protected void generateTheTree() throws OperationException { affectedNodes = new LinkedHashSet<ExtendedNode>(); //BidirectionalMap<AbstractNode<Info,?>, EnglishNode> copyMap = AbstractNodeUtils.copyTreeReturnMap(textTree.getTree(), new EnglishNodeConstructor()); BidirectionalMap<ExtendedNode, ExtendedNode> copyMap = AbstractNodeUtils.strictTypeCopyTree(textTree.getTree(), new ExtendedNodeConstructor()); ExtendedNode duplicatedNode = new ExtendedNode(new ExtendedInfo(new DefaultInfo(nodeToMove.getInfo().getId(), nodeToMove.getInfo().getNodeInfo(), this.newEdgeInfo), nodeToMove.getInfo().getAdditionalNodeInformation()) ); ExtendedNode parentInGeneratedTree = copyMap.leftGet(newParent); parentInGeneratedTree.addChild(duplicatedNode); if (nodeToMove.getAntecedent()!=null) { ExtendedNode antecedentInOriginal = nodeToMove.getAntecedent(); ExtendedNode antecedentInGenerated = copyMap.leftGet(antecedentInOriginal); duplicatedNode.setAntecedent(antecedentInGenerated); } affectedNodes.add(duplicatedNode); this.generatedTree = copyMap.leftGet(textTree.getTree()); this.mapOriginalToGenerated = new SimpleValueSetMap<ExtendedNode, ExtendedNode>(); for (ExtendedNode originalNode : copyMap.leftSet()) { mapOriginalToGenerated.put(originalNode,copyMap.leftGet(originalNode)); } mapOriginalToGenerated.put(nodeToMove,duplicatedNode); }
@Override protected void generateTheTree() throws OperationException { bidiMapOrigToGenerated = new SimpleBidirectionalMap<TN, TN>(); this.rootOfLhsInTree = mapLhsToTree.leftGet(rule.getLeftHandSide()); if (null==rootOfLhsInTree) { throw new OperationException("LHS root not mapped to any node in the original tree.\n" + "The map from LHS to the tree is:\n"+debug_print_mapLhsToTree()); } try { generatedTree = copySubTree(this.textTree.getTree()); } catch(TeEngineMlException e) { throw new OperationException("Rule application failed. See nested exception.",e); } }
public void generate() { TreeCopier<I, S, I, S> copier = new TreeCopier<I, S, I, S>( originalTree, new TreeCopier.InfoConverter<S, I>() { @Override public I convert(S os) { return os.getInfo(); } }, nodeConstructor ); copier.copy(); generatedTree = copier.getGeneratedTree(); generatedTree.addChild(treeToPatch); BidirectionalMap<S, S> copierMap = copier.getNodesMap(); mapOriginalToGenerated = new SimpleValueSetMap<>(); for (S node : copierMap.leftSet()) { mapOriginalToGenerated.put(node, copierMap.leftGet(node)); } }