|
| 1 | +package edu.stanford.nlp.semgraph.semgrex; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.HashSet; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Set; |
| 9 | + |
| 10 | +import edu.stanford.nlp.ling.IndexedWord; |
| 11 | +import edu.stanford.nlp.semgraph.SemanticGraph; |
| 12 | +import edu.stanford.nlp.semgraph.SemanticGraphEdge; |
| 13 | +import edu.stanford.nlp.util.CoreMap; |
| 14 | +import edu.stanford.nlp.util.Pair; |
| 15 | +import edu.stanford.nlp.util.VariableStrings; |
| 16 | + |
| 17 | +/** |
| 18 | + * At semgrex creation time, this takes a list of nodes or attributes. |
| 19 | + *<br> |
| 20 | + * At batch processing time, this pares a list of matches down to |
| 21 | + * one match for each matching attributes. |
| 22 | + */ |
| 23 | +public class UniqPattern extends SemgrexPattern { |
| 24 | + private static final long serialVersionUID = -38315768154569L; |
| 25 | + |
| 26 | + private final SemgrexPattern child; |
| 27 | + private final List<String> keys; |
| 28 | + |
| 29 | + public UniqPattern(SemgrexPattern child, List<String> keys) { |
| 30 | + this.child = child; |
| 31 | + this.keys = new ArrayList<>(keys); |
| 32 | + } |
| 33 | + |
| 34 | + private String getKey(SemgrexMatch match, String key) { |
| 35 | + // TODO: could also do edge names or variable groups (once those exist) |
| 36 | + IndexedWord node = match.getNode(key); |
| 37 | + if (node == null) { |
| 38 | + return null; |
| 39 | + } |
| 40 | + return node.value(); |
| 41 | + } |
| 42 | + |
| 43 | + public List<Pair<CoreMap, List<SemgrexMatch>>> postprocessMatches(List<Pair<CoreMap, List<SemgrexMatch>>> matches) { |
| 44 | + // hashing lists should be okay here since the lists will not change |
| 45 | + // while the postprocessing is happening |
| 46 | + Set<List<String>> seenKeys = new HashSet<>(); |
| 47 | + |
| 48 | + List<Pair<CoreMap, List<SemgrexMatch>>> newMatches = new ArrayList<>(); |
| 49 | + for (Pair<CoreMap, List<SemgrexMatch>> sentence : matches) { |
| 50 | + List<SemgrexMatch> newSentenceMatches = new ArrayList<>(); |
| 51 | + for (SemgrexMatch match : sentence.second()) { |
| 52 | + List<String> matchKey = new ArrayList<>(); |
| 53 | + for (String key : keys) { |
| 54 | + matchKey.add(getKey(match, key)); |
| 55 | + } |
| 56 | + if (seenKeys.contains(matchKey)) { |
| 57 | + continue; |
| 58 | + } |
| 59 | + seenKeys.add(matchKey); |
| 60 | + newSentenceMatches.add(match); |
| 61 | + } |
| 62 | + if (newSentenceMatches.size() > 0) { |
| 63 | + newMatches.add(new Pair<>(sentence.first(), newSentenceMatches)); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return newMatches; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public String localString() { |
| 72 | + return toString(true, false); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public String toString() { |
| 77 | + return toString(true, true); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public String toString(boolean hasPrecedence) { |
| 82 | + return toString(hasPrecedence, true); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void setChild(SemgrexPattern n) { |
| 87 | + throw new UnsupportedOperationException("Child should only be set on a UniqPattern at creation time"); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public List<SemgrexPattern> getChildren() { |
| 92 | + if (child == null) { |
| 93 | + return Collections.emptyList(); |
| 94 | + } else { |
| 95 | + return Collections.singletonList(child); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public String toString(boolean hasPrecedence, boolean addChild) { |
| 100 | + StringBuilder sb = new StringBuilder(); |
| 101 | + if (addChild) { |
| 102 | + sb.append(child.toString(true)); |
| 103 | + } |
| 104 | + sb.append(" :: uniq"); |
| 105 | + for (String key : keys) { |
| 106 | + sb.append(" "); |
| 107 | + sb.append(key); |
| 108 | + } |
| 109 | + return sb.toString(); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public SemgrexMatcher matcher(SemanticGraph sg, IndexedWord node, |
| 114 | + Map<String, IndexedWord> namesToNodes, |
| 115 | + Map<String, String> namesToRelations, |
| 116 | + Map<String, SemanticGraphEdge> namesToEdges, |
| 117 | + VariableStrings variableStrings, |
| 118 | + boolean ignoreCase) { |
| 119 | + return child.matcher(sg, node, namesToNodes, namesToRelations, namesToEdges, variableStrings, ignoreCase); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public SemgrexMatcher matcher(SemanticGraph sg, |
| 124 | + Alignment alignment, SemanticGraph sg_align, |
| 125 | + boolean hyp, IndexedWord node, |
| 126 | + Map<String, IndexedWord> namesToNodes, |
| 127 | + Map<String, String> namesToRelations, |
| 128 | + Map<String, SemanticGraphEdge> namesToEdges, |
| 129 | + VariableStrings variableStrings, |
| 130 | + boolean ignoreCase) { |
| 131 | + return child.matcher(sg, alignment, sg_align, hyp, node, namesToNodes, namesToRelations, namesToEdges, variableStrings, ignoreCase); |
| 132 | + } |
| 133 | +} |
0 commit comments