本文整理匯總了Python中nltk.parse.dependencygraph.DependencyGraph.nodelist[0]['deps']方法的典型用法代碼示例。如果您正苦於以下問題:Python DependencyGraph.nodelist[0]['deps']方法的具體用法?Python DependencyGraph.nodelist[0]['deps']怎麽用?Python DependencyGraph.nodelist[0]['deps']使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類nltk.parse.dependencygraph.DependencyGraph
的用法示例。
在下文中一共展示了DependencyGraph.nodelist[0]['deps']方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: parse
# 需要導入模塊: from nltk.parse.dependencygraph import DependencyGraph [as 別名]
# 或者: from nltk.parse.dependencygraph.DependencyGraph import nodelist[0]['deps'] [as 別名]
#.........這裏部分代碼省略.........
param tokens: A list of tokens to parse.
type tokens: list(str)
return: A set of non-projective parses.
rtype: list(DependencyGraph)
"""
# Create graph representation of tokens
self._graph = DependencyGraph()
self._graph.nodelist = [] # Remove the default root
for index, token in enumerate(tokens):
self._graph.nodelist.append({'word':token, 'deps':[], 'rel':'NTOP', 'address':index})
for head_node in self._graph.nodelist:
deps = []
for dep_node in self._graph.nodelist:
if self._grammar.contains(head_node['word'], dep_node['word']) and not head_node['word'] == dep_node['word']:
deps.append(dep_node['address'])
head_node['deps'] = deps
# Create lattice of possible heads
roots = []
possible_heads = []
for i, word in enumerate(tokens):
heads = []
for j, head in enumerate(tokens):
if (i != j) and self._grammar.contains(head, word):
heads.append(j)
if len(heads) == 0:
roots.append(i)
possible_heads.append(heads)
# Set roots to attempt
if len(roots) > 1:
print("No parses found.")
return False
elif len(roots) == 0:
for i in range(len(tokens)):
roots.append(i)
# Traverse lattice
analyses = []
for root in roots:
stack = []
analysis = [[] for i in range(len(possible_heads))]
i = 0
forward = True
while(i >= 0):
if forward:
if len(possible_heads[i]) == 1:
analysis[i] = possible_heads[i][0]
elif len(possible_heads[i]) == 0:
analysis[i] = -1
else:
head = possible_heads[i].pop()
analysis[i] = head
stack.append([i, head])
if not forward:
index_on_stack = False
for stack_item in stack:
# print stack_item
if stack_item[0] == i:
index_on_stack = True
orig_length = len(possible_heads[i])
# print len(possible_heads[i])
if index_on_stack and orig_length == 0:
for j in xrange(len(stack) -1, -1, -1):
stack_item = stack[j]
if stack_item[0] == i:
possible_heads[i].append(stack.pop(j)[1])
# print stack
elif index_on_stack and orig_length > 0:
head = possible_heads[i].pop()
analysis[i] = head
stack.append([i, head])
forward = True
# print 'Index on stack:', i, index_on_stack
if i + 1 == len(possible_heads):
analyses.append(analysis[:])
forward = False
if forward:
i += 1
else:
i -= 1
# Filter parses
graphs = []
#ensure 1 root, every thing has 1 head
for analysis in analyses:
root_count = 0
root = []
for i, cell in enumerate(analysis):
if cell == -1:
root_count += 1
root = i
if root_count == 1:
graph = DependencyGraph()
graph.nodelist[0]['deps'] = root + 1
for i in range(len(tokens)):
node = {'word':tokens[i], 'address':i+1}
node['deps'] = [j+1 for j in range(len(tokens)) if analysis[j] == i]
graph.nodelist.append(node)
# cycle = graph.contains_cycle()
# if not cycle:
graphs.append(graph)
return graphs