Skip to content

Commit 8be0ef4

Browse files
committed
Remove some redundant stuff in Graph() class
1 parent 66ef016 commit 8be0ef4

File tree

1 file changed

+43
-63
lines changed

1 file changed

+43
-63
lines changed

modules/graphs.py

Lines changed: 43 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -46,84 +46,65 @@ def __init__(self, tokens, mode="sparse", feat_map=None, weight_vector=None):
4646
# added, then every other arc. Every arc, except ones in the full graph, gets a sparse feature vector based
4747
# on feat_map. The completed graphs also get a score per arc (based on the features and the weight vector).
4848

49-
# sparse arc representation
50-
if mode == "sparse":
51-
for token in (token for token in tokens if token.head == 0):
52-
new_arc = Arc("sparse", 0, token.id)
53-
new_arc.feat_vec = [f for f in (feat_map[feature] for feature in
54-
give_features("__ROOT__", "__ROOT__", "__ROOT__", token.form,
55-
token.lemma, token.pos, token.rel) if
56-
feature in feat_map)]
57-
self.heads[0].append(new_arc)
49+
# normal graph
50+
if mode == "sparse" or mode == "full":
5851
for token1 in tokens:
52+
# add arc from root to it's dependent
53+
if token1.head == 0:
54+
if mode == "sparse":
55+
new_arc = Arc("sparse", 0, token1.id)
56+
new_arc.feat_vec = [f for f in (feat_map[feature] for feature in
57+
give_features("__ROOT__", "__ROOT__", "__ROOT__", token1.form,
58+
token1.lemma, token1.pos, token1.rel) if
59+
feature in feat_map)]
60+
else:
61+
new_arc = Arc("full", 0, token1.id, "__ROOT__", token1.form, "__ROOT__", token1.lemma,
62+
"__ROOT__",
63+
token1.pos, token1.rel)
64+
self.heads[0].append(new_arc)
65+
66+
# add every other arc
5967
dependents = []
6068
for token2 in (token2 for token2 in tokens if token2.head == token1.id):
61-
new_arc = Arc("sparse", token1.id, token2.id)
62-
new_arc.feat_vec = [f for f in (feat_map[feature] for feature in
63-
give_features(token1.form, token1.lemma, token1.pos, token2.form,
64-
token2.lemma, token2.pos, token2.rel) if
65-
feature in feat_map)]
66-
dependents.append(new_arc)
67-
if dependents:
68-
self.heads[token1.id] = dependents
69-
70-
# sparse arc representation, completed graph
71-
elif mode == "complete-sparse":
72-
for token in tokens:
73-
new_arc = Arc("sparse", 0, token.id)
74-
new_arc.feat_vec = [f for f in (feat_map[feature] for feature in
75-
give_features("__ROOT__", "__ROOT__", "__ROOT__", token.form,
76-
token.lemma, token.pos, token.rel) if
77-
feature in feat_map)]
78-
for feature in new_arc.feat_vec:
79-
new_arc.score += weight_vector[feature]
80-
self.heads[0].append(new_arc)
81-
for token1 in tokens:
82-
dependents = []
83-
for token2 in (token2 for token2 in tokens if token2.id != token1.id):
84-
new_arc = Arc("sparse", token1.id, token2.id)
85-
new_arc.feat_vec = [f for f in (feat_map[feature] for feature in
86-
give_features(token1.form, token1.lemma, token1.pos, token2.form,
87-
token2.lemma, token2.pos, token2.rel) if
88-
feature in feat_map)]
89-
for feature in new_arc.feat_vec:
90-
new_arc.score += weight_vector[feature]
69+
if mode == "sparse":
70+
new_arc = Arc("sparse", token1.id, token2.id)
71+
new_arc.feat_vec = [f for f in (feat_map[feature] for feature in
72+
give_features(token1.form, token1.lemma, token1.pos,
73+
token2.form, token2.lemma, token2.pos, token2.rel)
74+
if feature in feat_map)]
75+
else:
76+
new_arc = Arc("full", token1.id, token2.id, token1.form, token2.form, token1.lemma,
77+
token2.lemma, token1.pos, token2.pos, token2.rel)
9178
dependents.append(new_arc)
9279
if dependents:
9380
self.heads[token1.id] = dependents
9481

95-
# full arc representation
96-
elif mode == "full":
97-
for token in (token for token in tokens if token.head == 0):
98-
new_arc = Arc("full", 0, token.id, "__ROOT__", token.form, "__ROOT__", token.lemma, "__ROOT__",
99-
token.pos, token.rel)
100-
self.heads[0].append(new_arc)
82+
# completed graph
83+
elif mode == "complete-sparse" or "complete-full":
10184
for token1 in tokens:
102-
dependents = []
103-
for token2 in (token2 for token2 in tokens if token2.head == token1.id):
104-
new_arc = Arc("full", token1.id, token2.id, token1.form, token2.form, token1.lemma, token2.lemma,
105-
token1.pos, token2.pos, token2.rel)
106-
dependents.append(new_arc)
107-
if dependents:
108-
self.heads[token1.id] = dependents
10985

110-
# full arc representation, completed graph
111-
elif mode == "complete-full":
112-
for token in tokens:
113-
new_arc = Arc("full", 0, token.id, "__ROOT__", token.form, "__ROOT__", token.lemma, "__ROOT__",
114-
token.pos, token.rel)
86+
# add arcs from root to every node
87+
if mode == "complete-sparse":
88+
new_arc = Arc("sparse", 0, token1.id)
89+
else:
90+
new_arc = Arc("full", 0, token1.id, "__ROOT__", token1.form, "__ROOT__", token1.lemma, "__ROOT__",
91+
token1.pos, token1.rel)
11592
new_arc.feat_vec = [f for f in (feat_map[feature] for feature in
116-
give_features("__ROOT__", "__ROOT__", "__ROOT__", token.form,
117-
token.lemma, token.pos, token.rel) if
93+
give_features("__ROOT__", "__ROOT__", "__ROOT__", token1.form,
94+
token1.lemma, token1.pos, token1.rel) if
11895
feature in feat_map)]
11996
for feature in new_arc.feat_vec:
12097
new_arc.score += weight_vector[feature]
12198
self.heads[0].append(new_arc)
122-
for token1 in tokens:
99+
100+
# add every other arc
123101
dependents = []
124102
for token2 in (token2 for token2 in tokens if token2.id != token1.id):
125-
new_arc = Arc("full", token1.id, token2.id, token1.form, token2.form, token1.lemma, token2.lemma,
126-
token1.pos, token2.pos, token2.rel)
103+
if mode == "complete-sparse":
104+
new_arc = Arc("sparse", token1.id, token2.id)
105+
else:
106+
new_arc = Arc("full", token1.id, token2.id, token1.form, token2.form, token1.lemma,
107+
token2.lemma, token1.pos, token2.pos, token2.rel)
127108
new_arc.feat_vec = [f for f in (feat_map[feature] for feature in
128109
give_features(token1.form, token1.lemma, token1.pos, token2.form,
129110
token2.lemma, token2.pos, token2.rel) if
@@ -133,7 +114,6 @@ def __init__(self, tokens, mode="sparse", feat_map=None, weight_vector=None):
133114
dependents.append(new_arc)
134115
if dependents:
135116
self.heads[token1.id] = dependents
136-
137117
else:
138118
print "Unknown Graph mode"
139119

0 commit comments

Comments
 (0)