3
3
import java .util .ArrayList ;
4
4
import java .util .Collections ;
5
5
import java .util .Iterator ;
6
- import java .util .LinkedHashMap ;
7
6
import java .util .List ;
8
7
import java .util .Map ;
9
8
import java .util .regex .Matcher ;
13
12
import edu .stanford .nlp .semgraph .SemanticGraph ;
14
13
import edu .stanford .nlp .semgraph .SemanticGraphEdge ;
15
14
import edu .stanford .nlp .util .Pair ;
15
+ import edu .stanford .nlp .util .Triple ;
16
16
import edu .stanford .nlp .util .logging .Redwood ;
17
17
18
18
public class NodePattern extends SemgrexPattern {
@@ -31,7 +31,7 @@ public class NodePattern extends SemgrexPattern {
31
31
* value.
32
32
* Otherwise, the type will be a Pattern, and you must use Pattern.matches().
33
33
*/
34
- private final Map < String , Pair < Object , Object > > attributes ;
34
+ private final List < Attribute > attributes ;
35
35
private final boolean isRoot ;
36
36
private final boolean isLink ;
37
37
private final boolean isEmpty ;
@@ -43,33 +43,34 @@ public class NodePattern extends SemgrexPattern {
43
43
private List <Pair <Integer , String >> variableGroups ;
44
44
45
45
public NodePattern (GraphRelation r , boolean negDesc ,
46
- Map < String , String > attrs ,
46
+ List < Triple < String , String , Boolean > > attrs ,
47
47
boolean root , boolean empty , boolean isLink , String name ) {
48
48
this (r , negDesc , attrs , root , empty , isLink , name ,
49
49
new ArrayList <>(0 ));
50
50
}
51
51
52
52
// TODO: there is no capacity for named variable groups in the parser right now
53
53
public NodePattern (GraphRelation r , boolean negDesc ,
54
- Map < String , String > attrs ,
54
+ List < Triple < String , String , Boolean > > attrs ,
55
55
boolean root , boolean empty , boolean isLink , String name ,
56
56
List <Pair <Integer , String >> variableGroups ) {
57
57
this .reln = r ;
58
58
this .negDesc = negDesc ;
59
59
this .isLink = isLink ;
60
60
// order the attributes so that the pattern stays the same when
61
61
// printing a compiled pattern
62
- attributes = new LinkedHashMap <>();
62
+ attributes = new ArrayList <>();
63
63
descString = "{" ;
64
- for (Map . Entry <String , String > entry : attrs . entrySet () ) {
64
+ for (Triple <String , String , Boolean > entry : attrs ) {
65
65
if (!descString .equals ("{" ))
66
66
descString += ";" ;
67
- String key = entry .getKey ();
68
- String value = entry .getValue ();
67
+ String key = entry .first ();
68
+ String value = entry .second ();
69
+ boolean negated = entry .third ();
69
70
70
71
// Add the attributes for this key
71
72
if (value .equals ("__" )) {
72
- attributes .put ( key , Pair . makePair ( true , true ));
73
+ attributes .add ( new Attribute ( key , true , true , negated ));
73
74
} else if (value .matches ("/.*/" )) {
74
75
boolean isRegexp = false ;
75
76
for (int i = 1 ; i < value .length () - 1 ; ++i ) {
@@ -81,34 +82,24 @@ public NodePattern(GraphRelation r, boolean negDesc,
81
82
}
82
83
String patternContent = value .substring (1 , value .length () - 1 );
83
84
if (isRegexp ) {
84
- attributes .put ( key , Pair . makePair (
85
- Pattern .compile (patternContent ),
86
- Pattern .compile (patternContent , Pattern .CASE_INSENSITIVE |Pattern .UNICODE_CASE ))
87
- );
85
+ attributes .add ( new Attribute ( key ,
86
+ Pattern .compile (patternContent ),
87
+ Pattern .compile (patternContent , Pattern .CASE_INSENSITIVE |Pattern .UNICODE_CASE ),
88
+ negated ) );
88
89
} else {
89
- attributes .put ( key , Pair . makePair ( patternContent , patternContent ));
90
+ attributes .add ( new Attribute ( key , patternContent , patternContent , negated ));
90
91
}
91
92
} else { // raw description
92
- attributes .put ( key , Pair . makePair ( value , value ));
93
+ attributes .add ( new Attribute ( key , value , value , negated ));
93
94
}
94
95
95
-
96
-
97
- // if (value.equals("__")) {
98
- // attributes.put(key, Pair.makePair(Pattern.compile(".*"), Pattern.compile(".*", Pattern.CASE_INSENSITIVE)));
99
- // } else if (value.matches("/.*/")) {
100
- // attributes.put(key, Pair.makePair(
101
- // Pattern.compile(value.substring(1, value.length() - 1)),
102
- // Pattern.compile(value.substring(1, value.length() - 1), Pattern.CASE_INSENSITIVE))
103
- // );
104
- // } else { // raw description
105
- // attributes.put(key, Pair.makePair(
106
- // Pattern.compile("^(" + value + ")$"),
107
- // Pattern.compile("^(" + value + ")$", Pattern.CASE_INSENSITIVE))
108
- // );
109
- // }
110
- descString += (key + ':' + value );
96
+ if (negated ) {
97
+ descString += (key + "!:" + value );
98
+ } else {
99
+ descString += (key + ':' + value );
100
+ }
111
101
}
102
+
112
103
if (root ) {
113
104
if (!descString .equals ("{" ))
114
105
descString += ";" ;
@@ -145,8 +136,8 @@ public boolean nodeAttrMatch(IndexedWord node, final SemanticGraph sg, boolean i
145
136
return (negDesc ? !node .equals (IndexedWord .NO_WORD ) : node .equals (IndexedWord .NO_WORD ));
146
137
147
138
// log.info("Attributes are: " + attributes);
148
- for (Map . Entry < String , Pair < Object , Object >> attr : attributes . entrySet () ) {
149
- String key = attr .getKey () ;
139
+ for (Attribute attr : attributes ) {
140
+ String key = attr .key ;
150
141
// System.out.println(key);
151
142
String nodeValue ;
152
143
// if (key.equals("idx"))
@@ -167,7 +158,7 @@ public boolean nodeAttrMatch(IndexedWord node, final SemanticGraph sg, boolean i
167
158
return negDesc ;
168
159
169
160
// Get the node pattern
170
- Object toMatch = ignoreCase ? attr .getValue (). second : attr .getValue (). first ;
161
+ Object toMatch = ignoreCase ? attr .caseless : attr .cased ;
171
162
boolean matches ;
172
163
if (toMatch instanceof Boolean ) {
173
164
matches = ((Boolean ) toMatch );
@@ -182,6 +173,9 @@ public boolean nodeAttrMatch(IndexedWord node, final SemanticGraph sg, boolean i
182
173
} else {
183
174
throw new IllegalStateException ("Unknown matcher type: " + toMatch + " (of class + " + toMatch .getClass () + ")" );
184
175
}
176
+ if (attr .negated ) {
177
+ matches = !matches ;
178
+ }
185
179
186
180
if (!matches ) {
187
181
// System.out.println("doesn't match");
0 commit comments