Skip to content

Commit

Permalink
Merge pull request #26 from bryant1410/patch-1
Browse files Browse the repository at this point in the history
Fix potential infinite recursion error
  • Loading branch information
vacancy committed Oct 13, 2023
2 parents 44f1149 + d24299b commit 2825dba
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions sng_parser/backends/spacy_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,24 @@ def parse(self, sentence, doc=None, return_doc=False):
modifiers=[]
)

visited_nodes = set()

def dfs(node):
for x in node.children:
if x.dep_ == 'det':
ent['modifiers'].append({'dep': x.dep_, 'span': x.text, 'lemma_span': x.lemma_})
elif x.dep_ == 'nummod':
ent['modifiers'].append({'dep': x.dep_, 'span': x.text, 'lemma_span': x.lemma_})
elif x.dep_ == 'amod':
for y in self.__flatten_conjunction(x):
ent['modifiers'].append({'dep': x.dep_, 'span': y.text, 'lemma_span': y.lemma_})
elif x.dep_ == 'compound':
ent['head'] = x.text + ' ' + ent['head']
ent['lemma_head'] = x.lemma_ + ' ' + ent['lemma_head']
dfs(x)
if node not in visited_nodes: # Sometimes, the dependency graph is erroneously cyclic.
visited_nodes.add(node)

for x in node.children:
if x.dep_ == 'det':
ent['modifiers'].append({'dep': x.dep_, 'span': x.text, 'lemma_span': x.lemma_})
elif x.dep_ == 'nummod':
ent['modifiers'].append({'dep': x.dep_, 'span': x.text, 'lemma_span': x.lemma_})
elif x.dep_ == 'amod':
for y in self.__flatten_conjunction(x):
ent['modifiers'].append({'dep': x.dep_, 'span': y.text, 'lemma_span': y.lemma_})
elif x.dep_ == 'compound':
ent['head'] = x.text + ' ' + ent['head']
ent['lemma_head'] = x.lemma_ + ' ' + ent['lemma_head']
dfs(x)

dfs(entity.root)

Expand Down

0 comments on commit 2825dba

Please sign in to comment.