Skip to content

Commit cc806f2

Browse files
committed
Replace HashMap with a LinkedHashMap for semgrex. Makes the order of printing attributes deterministic when printing out a previously parsed semgrex expression
1 parent 4aafb84 commit cc806f2

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

src/edu/stanford/nlp/semgraph/semgrex/NodeAttributes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package edu.stanford.nlp.semgraph.semgrex;
22

3-
import java.util.HashMap;
3+
import java.util.LinkedHashMap;
44
import java.util.Map;
55

66
/**
@@ -23,7 +23,7 @@ public class NodeAttributes {
2323
public NodeAttributes() {
2424
root = false;
2525
empty = false;
26-
attributes = new HashMap<>();
26+
attributes = new LinkedHashMap<>();
2727
}
2828

2929
public void setRoot(boolean root) {

src/edu/stanford/nlp/semgraph/semgrex/NodePattern.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import java.util.ArrayList;
44
import java.util.Collections;
55
import java.util.Iterator;
6+
import java.util.LinkedHashMap;
67
import java.util.List;
78
import java.util.Map;
89
import java.util.regex.Matcher;
910
import java.util.regex.Pattern;
1011

1112
import edu.stanford.nlp.ling.IndexedWord;
1213
import edu.stanford.nlp.semgraph.SemanticGraph;
13-
import edu.stanford.nlp.util.Generics;
1414
import edu.stanford.nlp.util.Pair;
1515
import edu.stanford.nlp.util.logging.Redwood;
1616

@@ -55,7 +55,9 @@ public NodePattern(GraphRelation r, boolean negDesc,
5555
List<Pair<Integer, String>> variableGroups) {
5656
this.reln = r;
5757
this.negDesc = negDesc;
58-
attributes = Generics.newHashMap();
58+
// order the attributes so that the pattern stays the same when
59+
// printing a compiled pattern
60+
attributes = new LinkedHashMap<>();
5961
descString = "{";
6062
for (Map.Entry<String, String> entry : attrs.entrySet()) {
6163
if (!descString.equals("{"))

test/src/edu/stanford/nlp/semgraph/semgrex/SemgrexTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,29 @@ public void testSimpleDependency() {
115115
"ate", "ate", "muffins");
116116
}
117117

118+
public void testMultipleAttributes() {
119+
runTest("{} >> {word:Bill}",
120+
"[ate subj>Bill/NNP obj>[muffins compound>blueberry]]",
121+
"ate");
122+
runTest("{} >> {tag:NNP}",
123+
"[ate subj>Bill/NNP obj>[muffins compound>blueberry]]",
124+
"ate");
125+
runTest("{} >> {word:Bill;tag:NNP}",
126+
"[ate subj>Bill/NNP obj>[muffins compound>blueberry]]",
127+
"ate");
128+
runTest("{} >> {word:Bill;tag:NNZ}",
129+
"[ate subj>Bill/NNP obj>[muffins compound>blueberry]]");
130+
runTest("{} >> {word:Ragavaniskillinglegacy;tag:NNP}",
131+
"[ate subj>Bill/NNP obj>[muffins compound>blueberry]]");
132+
runTest("{} >> {tag:NNP;word:Bill}",
133+
"[ate subj>Bill/NNP obj>[muffins compound>blueberry]]",
134+
"ate");
135+
runTest("{} >> {tag:NNZ;word:Bill}",
136+
"[ate subj>Bill/NNP obj>[muffins compound>blueberry]]");
137+
runTest("{} >> {tag:NNP;word:UnbanMoxOpal}",
138+
"[ate subj>Bill/NNP obj>[muffins compound>blueberry]]");
139+
}
140+
118141
public void testNamedDependency() {
119142
runTest("{} << {word:ate}",
120143
"[ate subj>Bill obj>[muffins compound>blueberry]]",

0 commit comments

Comments
 (0)