/** * reverse a rule. Replace RHS and LHS labels and flip the mapping * <p> * <b>Remember </b>you should only reverse substitution rules! * * @param <I> * @param <S> * @param rule * @return */ private static <I, S extends AbstractNode<I,S>> SyntacticRule<I,S> reverseRule(SyntacticRule<I,S> rule) { return new SyntacticRule<I, S>( rule.getRightHandSide(), rule.getLeftHandSide(), new FlippedBidirectionalMap<S, S>(rule.getMapNodes()), rule.isExtraction()); } }
/** * Return first match between the tree and the rule. Comparing nodes and edges with {@link AnnotationsWithWildcardsMatchCriteria}. Recall that with * this criteria, null annotation values are regarded as "" empty strings. * * @param tree * @param rule * @return * @throws MatcherException */ public static Set<BidirectionalMap<ExtendedNode, ExtendedConstructionNode>> getMatchesOfLhsToTree(ExtendedConstructionNode tree, AnnotationRule<ExtendedNode, BasicRuleAnnotations> rule) throws MatcherException { AllEmbeddedMatcher<ExtendedInfo, ExtendedInfo, ExtendedConstructionNode, ExtendedNode> matcher = new AllEmbeddedMatcher<ExtendedInfo, ExtendedInfo, ExtendedConstructionNode, ExtendedNode>(MATCH_CRITERIA); matcher.setTrees(tree, rule.getLeftHandSide()); matcher.findMatches(); Set<BidirectionalMap<ExtendedConstructionNode, ExtendedNode>> reversedMatches = matcher.getMatches(); Set<BidirectionalMap<ExtendedNode, ExtendedConstructionNode>> matchesOfLhsToTree = new LinkedHashSet<BidirectionalMap<ExtendedNode,ExtendedConstructionNode>>(reversedMatches.size()); for (BidirectionalMap<ExtendedConstructionNode, ExtendedNode> match : reversedMatches) matchesOfLhsToTree.add(new FlippedBidirectionalMap<ExtendedNode, ExtendedConstructionNode>(match)); return matchesOfLhsToTree; }
/** * @param tree * @param rule * @return * @throws MatcherException */ private Set<BidirectionalMap<BasicNode, ExtendedNode>> mapRuleToTree(ExtendedNode tree, SyntacticRule<Info, BasicNode> rule) throws MatcherException { Set<BidirectionalMap<BasicNode, ExtendedNode>> mapsOfLhsToTree = new LinkedHashSet<BidirectionalMap<BasicNode,ExtendedNode>>(); AllEmbeddedMatcher<ExtendedInfo, Info, ExtendedNode, BasicNode> matcher = new AllEmbeddedMatcher<ExtendedInfo, Info, ExtendedNode, BasicNode>(new ExtendedMatchCriteria()); matcher.setTrees(tree, rule.getLeftHandSide()); matcher.findMatches(); Set<BidirectionalMap<ExtendedNode, BasicNode>> matches = matcher.getMatches(); if (matches!=null){if (matches.size()>0) { for (BidirectionalMap<ExtendedNode, BasicNode> singleMatch : matches) { mapsOfLhsToTree.add(new FlippedBidirectionalMap<BasicNode, ExtendedNode>(singleMatch)); } }} return mapsOfLhsToTree; }
RuleSpecification spec = new RuleSpecification(ruleBaseName,rule,new FlippedBidirectionalMap<BasicNode, ExtendedNode>(match),false); specs.add(spec);
matchedLeftHandSides.put(lhs, new FlippedBidirectionalMap<BasicNode, ExtendedNode>(singleMatch));
ret.add(new RuleMatch<Info, BasicNode>( new RuleWithConfidenceAndDescription<Info, BasicNode>(rule,score,leftSimilarity.getLeftElement().toKey() + "->" + leftSimilarity.getRightElement().toKey()), new FlippedBidirectionalMap<BasicNode, BasicNode>(match)));
BidirectionalMap<BasicNode, ExtendedNode> mapLhsToTree = new FlippedBidirectionalMap<BasicNode, ExtendedNode>(singleLhsMatch); boolean introduction = false; if (rule.getRule().isExtraction()!=null)