if (bidiMap2 == null) throw new AnnotatorException("null bidiMap2"); if (bidiMap1.size() != bidiMap2.size()) throw new AnnotatorException("bidiMap1 has " + bidiMap2.size() + " mappings while bidimap2 has "+ bidiMap2.size()); for (O node : bidiMap1.rightSet()) if (!bidiMap2.leftContains(node)) throw new AnnotatorException("These two bidirectional maps do not intersect properly:\n" + bidiMap1 +"\n" + bidiMap2); intersectedBidiMap.put(bidiMap1.rightGet(node), bidiMap2.leftGet(node));
/** * 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)); } } }
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); } } }
if(mapTemplateToId!=null) if (this.mapTemplateToId.leftContains(template)) id = this.mapTemplateToId.leftGet(template);
private static void mapItself(BidirectionalMap<ExtendedNode, ExtendedNode> map, ExtendedNode tree) { for (ExtendedNode node : AbstractNodeUtils.treeToLinkedHashSet(tree)) { map.put(node,node); } }
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; }
for (BasicNode lhsNode : mapLhsToTreeInRuleMatch.leftSet()) BasicNode treeNodeInBasicNodeTree = mapLhsToTreeInRuleMatch.leftGet(lhsNode); if (null==treeNodeInBasicNodeTree) ExtendedNode treeNodeInExtendedTree = mapTextTree.rightGet(treeNodeInBasicNodeTree); mapLhsToTree.put(lhsNode, treeNodeInExtendedTree);
/** * @param lhs * @param mappings * @param helpMap * @return */ private N convertLhsToNNodes( CN lhs, BidirectionalMap<CN, CN> mappings, BidirectionalMap<N, CN> helpMap) { N newLhs = compilationServices.newNode(lhs.getInfo()); if (lhs.getChildren() != null) for (CN child : lhs.getChildren()) { N newChild = convertLhsToNNodes(child, mappings, helpMap); newLhs.addChild(newChild); newChild.setAntecedent(newLhs); } // update the new mapping if necessary if (mappings.leftContains(lhs)) helpMap.put(newLhs, mappings.leftGet(lhs)); return newLhs; } }
/** * @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; }
if (infoServices.isVariableR(ruleRhsSubTree.getInfo())) SR lhsMappedNode = rule.getMapNodes().rightGet(ruleRhsSubTree); if (null==lhsMappedNode) throw new TeEngineMlException("unmapped variable"); ST originalTreeMappedNode = mapRuleLhsToTree.leftGet(lhsMappedNode); if (null==edgeInfo) if (rule.getMapNodes().rightContains(ruleRhsSubTree)) SR lhsMappedNode = rule.getMapNodes().rightGet(ruleRhsSubTree); ST originalTreeNode = mapRuleLhsToTree.leftGet(lhsMappedNode); if (null==originalTreeNode) throw new TeEngineMlException("left-hand-side or rule without a corresponding node in the text tree."); if (null==edgeInfo) if (rule.getMapNodes().rightContains(ruleRhsSubTree)) SR lhs = rule.getMapNodes().rightGet(ruleRhsSubTree); ST originalTreeNode = mapRuleLhsToTree.leftGet(lhs); if (originalTreeNode.getChildren()!=null) if (!mapRuleLhsToTree.rightContains(originalTreeChild)) if (rule.getMapNodes().rightContains(ruleRhsSubTree)) SR lhs = rule.getMapNodes().rightGet(ruleRhsSubTree); ST originalTreeNode = mapRuleLhsToTree.leftGet(lhs); mapOrigToGenerated.put(originalTreeNode, ret);
public void createMapTemplateToId() throws SQLException, RuleBaseException { mapTemplateToId = new SimpleBidirectionalMap<String, Integer>(); String query = "SELECT id,description FROM "+dbParameters.getTemplatesTableName(); Statement statement = connection.createStatement(); try { ResultSet resultSet = statement.executeQuery(query); while(resultSet.next()) { int id = resultSet.getInt("id"); String description = resultSet.getString("description"); if (description.isEmpty()) ; // ignore rows with an empty pattern-description - they represent duplicates of another row that represents their pattern. else { if (mapTemplateToId.leftContains(description)) throw new RuleBaseException("Malformed rule base. Template "+description+" appears more than once."); mapTemplateToId.put(description, id); } } } finally { statement.close(); } }
/** * @param rhs * @param helpMap * @param newMapping * @return */ private N convertRhsToNNodes( CN rhs, BidirectionalMap<N, CN> helpMap, BidirectionalMap<N, N> newMapping) { N newRhs = compilationServices.newNode(rhs.getInfo()); if (rhs.getChildren() != null) for (CN child : rhs.getChildren()) { N newChild = convertRhsToNNodes(child, helpMap, newMapping); newRhs.addChild(newChild); newChild.setAntecedent(newRhs); } // update the new mapping if necessary if (helpMap.rightContains(rhs)) newMapping.put(helpMap.rightGet(rhs), newRhs); return newRhs; }
if (node.getInfo().getNodeInfo().isVariable() && !rule.getMapNodes().rightContains(node)) throw new EntailmentCompilationException("There's an rhs node that is an unmapped variable: " + node); for (S leftNode : rule.getMapNodes().leftSet()) if (!rhsNodesSet.contains(rule.getMapNodes().leftGet(leftNode))) throw new EntailmentCompilationException("This node has an incoming alignment arrow, but it isn't in the RHS: "+ rule.getMapNodes().leftGet(leftNode));
@Override public ImmutableSet<V> values() { ImmutableSet<V> ret = null; if (this.bidirectionalMap!=null) { ret = this.bidirectionalMap.rightSet(); } else { ret = this.valueSetMap.values(); } return ret; }
protected ST copyNonRuleSubTree(ST subtree) { ST ret = nodeConstructor.newNode(subtree.getInfo()); if (subtree.getChildren()!=null) { for (ST child : subtree.getChildren()) { if (!mapRuleLhsToTree.rightContains(child)) { ret.addChild(copyNonRuleSubTree(child)); } } } mapOrigToGenerated.put(subtree, ret); return ret; }
@Override public boolean containsKey(K key) { boolean ret = false; if (this.bidirectionalMap!=null) { ret = this.bidirectionalMap.leftContains(key); } else { ret = this.valueSetMap.containsKey(key); } return ret; }
@Override public ImmutableSet<K> getKeysOf(V value) { ImmutableSet<K> ret = null; if (this.bidirectionalMap!=null) { LinkedHashSet<K> set = new LinkedHashSet<K>(); if (this.bidirectionalMap.rightContains(value)) { K key = this.bidirectionalMap.rightGet(value); set.add(key); } ret = new ImmutableSetWrapper<K>(set); } else { ret = this.valueSetMap.getKeysOf(value); } return ret; }
@Override public ImmutableSet<K> keySet() { ImmutableSet<K> ret = null; if (this.bidirectionalMap!=null) { ret = this.bidirectionalMap.leftSet(); } else { ret = this.valueSetMap.keySet(); } return ret; }