From 4e6531c113292de954172c956de3cde3b06d7e6e Mon Sep 17 00:00:00 2001 From: Long Duong Date: Wed, 4 Feb 2015 14:39:46 +1100 Subject: [PATCH] Modify these files to respond for the recents changes in dependency graph ['deps'] field --- nltk/sem/glue.py | 15 ++++++++++----- nltk/sem/lfg.py | 6 ++++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/nltk/sem/glue.py b/nltk/sem/glue.py index 3971ee1774..69166b350e 100644 --- a/nltk/sem/glue.py +++ b/nltk/sem/glue.py @@ -231,11 +231,13 @@ def __str__(self): def to_glueformula_list(self, depgraph, node=None, counter=None, verbose=False): if node is None: top = depgraph.nodes[0] - root = depgraph.nodes[top['deps'][0]] + depList = sum(list(top['deps'].values()), []) + root = depgraph.nodes[depList[0]] + #print (root) return self.to_glueformula_list(depgraph, root, Counter(), verbose) glueformulas = self.lookup(node, depgraph, counter) - for dep_idx in node['deps']: + for dep_idx in sum(list(node['deps'].values()), []): dep = depgraph.nodes[dep_idx] glueformulas.extend(self.to_glueformula_list(depgraph, dep, counter, verbose)) return glueformulas @@ -271,12 +273,15 @@ def add_missing_dependencies(self, node, depgraph): if rel == 'main': headnode = depgraph.nodes[node['head']] subj = self.lookup_unique('subj', headnode, depgraph) - node['deps'].append(subj['address']) + relation = subj['rel'] + node['deps'].setdefault(relation,[]) + node['deps'][relation].append(subj['address']) + #node['deps'].append(subj['address']) def _lookup_semtype_option(self, semtype, node, depgraph): relationships = frozenset( depgraph.nodes[dep]['rel'].lower() - for dep in node['deps'] + for dep in sum(list(node['deps'].values()), []) if depgraph.nodes[dep]['rel'].lower() not in OPTIONAL_RELATIONSHIPS ) @@ -409,7 +414,7 @@ def lookup_unique(self, rel, node, depgraph): """ deps = [ depgraph.nodes[dep] - for dep in node['deps'] + for dep in sum(list(node['deps'].values()), []) if depgraph.nodes[dep]['rel'].lower() == rel.lower() ] diff --git a/nltk/sem/lfg.py b/nltk/sem/lfg.py index 5fc883d375..c8a99b36b5 100644 --- a/nltk/sem/lfg.py +++ b/nltk/sem/lfg.py @@ -46,7 +46,9 @@ def to_depgraph(self, rel=None): for address, node in nodes.items(): for n2 in (n for n in nodes.values() if n['rel'] != 'TOP'): if n2['head'] == address: - node['deps'].append(n2['address']) + relation = n2['rel'] + node['deps'].setdefault(relation,[]) + node['deps'][relation].append(n2['address']) depgraph.root = nodes[1] @@ -115,7 +117,7 @@ def _read_depgraph(node, depgraph, label_counter=None, parent=None): if not fstruct.pred: fstruct.pred = (word, tag) - children = [depgraph.nodes[idx] for idx in node['deps']] + children = [depgraph.nodes[idx] for idx in sum(list(node['deps'].values()), [])] for child in children: fstruct.safeappend(child['rel'], FStructure._read_depgraph(child, depgraph, label_counter, fstruct))