本文整理汇总了Python中nltk.tree.Tree方法的典型用法代码示例。如果您正苦于以下问题:Python tree.Tree方法的具体用法?Python tree.Tree怎么用?Python tree.Tree使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nltk.tree
的用法示例。
在下文中一共展示了tree.Tree方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _backtrack
# 需要导入模块: from nltk import tree [as 别名]
# 或者: from nltk.tree import Tree [as 别名]
def _backtrack(self, *e):
if self._animating_lock: return
if self._parser.backtrack():
elt = self._parser.tree()
for i in self._parser.frontier()[0]:
elt = elt[i]
self._lastoper1['text'] = 'Backtrack'
self._lastoper2['text'] = ''
if isinstance(elt, Tree):
self._animate_backtrack(self._parser.frontier()[0])
else:
self._animate_match_backtrack(self._parser.frontier()[0])
return True
else:
self._autostep = 0
self._lastoper1['text'] = 'Finished'
self._lastoper2['text'] = ''
return False
示例2: _chunk_parse
# 需要导入模块: from nltk import tree [as 别名]
# 或者: from nltk.tree import Tree [as 别名]
def _chunk_parse(self, grammar=None, root_label='record', trace=0, **kwargs):
"""
Returns an element tree structure corresponding to a toolbox data file
parsed according to the chunk grammar.
:type grammar: str
:param grammar: Contains the chunking rules used to parse the
database. See ``chunk.RegExp`` for documentation.
:type root_label: str
:param root_label: The node value that should be used for the
top node of the chunk structure.
:type trace: int
:param trace: The level of tracing that should be used when
parsing a text. ``0`` will generate no tracing output;
``1`` will generate normal tracing output; and ``2`` or
higher will generate verbose tracing output.
:type kwargs: dict
:param kwargs: Keyword arguments passed to ``toolbox.StandardFormat.fields()``
:rtype: ElementTree._ElementInterface
"""
from nltk import chunk
from nltk.tree import Tree
cp = chunk.RegexpParser(grammar, root_label=root_label, trace=trace)
db = self.parse(**kwargs)
tb_etree = Element('toolbox_data')
header = db.find('header')
tb_etree.append(header)
for record in db.findall('record'):
parsed = cp.parse([(elem.text, elem.tag) for elem in record])
tb_etree.append(self._tree2etree(parsed))
return tb_etree
示例3: parse
# 需要导入模块: from nltk import tree [as 别名]
# 或者: from nltk.tree import Tree [as 别名]
def parse(self, chunk_struct, trace=None):
"""
Apply the chunk parser to this input.
:type chunk_struct: Tree
:param chunk_struct: the chunk structure to be (further) chunked
(this tree is modified, and is also returned)
:type trace: int
:param trace: The level of tracing that should be used when
parsing a text. ``0`` will generate no tracing output;
``1`` will generate normal tracing output; and ``2`` or
highter will generate verbose tracing output. This value
overrides the trace level value that was given to the
constructor.
:return: the chunked output.
:rtype: Tree
"""
if trace is None: trace = self._trace
for i in range(self._loop):
for parser in self._stages:
chunk_struct = parser.parse(chunk_struct, trace=trace)
return chunk_struct
示例4: tree2conlltags
# 需要导入模块: from nltk import tree [as 别名]
# 或者: from nltk.tree import Tree [as 别名]
def tree2conlltags(t):
"""
Return a list of 3-tuples containing ``(word, tag, IOB-tag)``.
Convert a tree to the CoNLL IOB tag format.
:param t: The tree to be converted.
:type t: Tree
:rtype: list(tuple)
"""
tags = []
for child in t:
try:
category = child.label()
prefix = "B-"
for contents in child:
if isinstance(contents, Tree):
raise ValueError("Tree is too deeply nested to be printed in CoNLL format")
tags.append((contents[0], contents[1], prefix+category))
prefix = "I-"
except AttributeError:
tags.append((child[0], child[1], "O"))
return tags
示例5: _tagged_to_parse
# 需要导入模块: from nltk import tree [as 别名]
# 或者: from nltk.tree import Tree [as 别名]
def _tagged_to_parse(self, tagged_tokens):
"""
Convert a list of tagged tokens to a chunk-parse tree.
"""
sent = Tree('S', [])
for (tok,tag) in tagged_tokens:
if tag == 'O':
sent.append(tok)
elif tag.startswith('B-'):
sent.append(Tree(tag[2:], [tok]))
elif tag.startswith('I-'):
if (sent and isinstance(sent[-1], Tree) and
sent[-1].label() == tag[2:]):
sent[-1].append(tok)
else:
sent.append(Tree(tag[2:], [tok]))
return sent
示例6: _parse_to_tagged
# 需要导入模块: from nltk import tree [as 别名]
# 或者: from nltk.tree import Tree [as 别名]
def _parse_to_tagged(sent):
"""
Convert a chunk-parse tree to a list of tagged tokens.
"""
toks = []
for child in sent:
if isinstance(child, Tree):
if len(child) == 0:
print("Warning -- empty chunk in sentence")
continue
toks.append((child[0], 'B-%s' % child.label()))
for tok in child[1:]:
toks.append((tok, 'I-%s' % child.label()))
else:
toks.append((child, 'O'))
return toks
示例7: trees
# 需要导入模块: from nltk import tree [as 别名]
# 或者: from nltk.tree import Tree [as 别名]
def trees(self, edge, tree_class=Tree, complete=False):
"""
Return an iterator of the tree structures that are associated
with ``edge``.
If ``edge`` is incomplete, then the unexpanded children will be
encoded as childless subtrees, whose node value is the
corresponding terminal or nonterminal.
:rtype: list(Tree)
:note: If two trees share a common subtree, then the same
Tree may be used to encode that subtree in
both trees. If you need to eliminate this subtree
sharing, then create a deep copy of each tree.
"""
return iter(self._trees(edge, complete, memo={}, tree_class=tree_class))
示例8: _shift
# 需要导入模块: from nltk import tree [as 别名]
# 或者: from nltk.tree import Tree [as 别名]
def _shift(self, stack, remaining_text):
"""
Move a token from the beginning of ``remaining_text`` to the
end of ``stack``.
:type stack: list(str and Tree)
:param stack: A list of strings and Trees, encoding
the structure of the text that has been parsed so far.
:type remaining_text: list(str)
:param remaining_text: The portion of the text that is not yet
covered by ``stack``.
:rtype: None
"""
stack.append(remaining_text[0])
remaining_text.remove(remaining_text[0])
if self._trace: self._trace_shift(stack, remaining_text)
示例9: _trace_stack
# 需要导入模块: from nltk import tree [as 别名]
# 或者: from nltk.tree import Tree [as 别名]
def _trace_stack(self, stack, remaining_text, marker=' '):
"""
Print trace output displaying the given stack and text.
:rtype: None
:param marker: A character that is printed to the left of the
stack. This is used with trace level 2 to print 'S'
before shifted stacks and 'R' before reduced stacks.
"""
s = ' '+marker+' [ '
for elt in stack:
if isinstance(elt, Tree):
s += unicode_repr(Nonterminal(elt.label())) + ' '
else:
s += unicode_repr(elt) + ' '
s += '* ' + ' '.join(remaining_text) + ']'
print(s)
示例10: _setprob
# 需要导入模块: from nltk import tree [as 别名]
# 或者: from nltk.tree import Tree [as 别名]
def _setprob(self, tree, prod_probs):
if tree.prob() is not None: return
# Get the prob of the CFG production.
lhs = Nonterminal(tree.label())
rhs = []
for child in tree:
if isinstance(child, Tree):
rhs.append(Nonterminal(child.label()))
else:
rhs.append(child)
prob = prod_probs[lhs, tuple(rhs)]
# Get the probs of children.
for child in tree:
if isinstance(child, Tree):
self._setprob(child, prod_probs)
prob *= child.prob()
tree.set_prob(prob)
示例11: _production_to_tree
# 需要导入模块: from nltk import tree [as 别名]
# 或者: from nltk.tree import Tree [as 别名]
def _production_to_tree(self, production):
"""
:rtype: Tree
:return: The Tree that is licensed by ``production``.
In particular, given the production ``[lhs -> elt[1] ... elt[n]]``
return a tree that has a node ``lhs.symbol``, and
``n`` children. For each nonterminal element
``elt[i]`` in the production, the tree token has a
childless subtree with node value ``elt[i].symbol``; and
for each terminal element ``elt[j]``, the tree token has
a leaf token with type ``elt[j]``.
:param production: The CFG production that licenses the tree
token that should be returned.
:type production: Production
"""
children = []
for elt in production.rhs():
if isinstance(elt, Nonterminal):
children.append(Tree(elt.symbol(), []))
else:
# This will be matched.
children.append(elt)
return Tree(production.lhs().symbol(), children)
示例12: _trace_fringe
# 需要导入模块: from nltk import tree [as 别名]
# 或者: from nltk.tree import Tree [as 别名]
def _trace_fringe(self, tree, treeloc=None):
"""
Print trace output displaying the fringe of ``tree``. The
fringe of ``tree`` consists of all of its leaves and all of
its childless subtrees.
:rtype: None
"""
if treeloc == (): print("*", end=' ')
if isinstance(tree, Tree):
if len(tree) == 0:
print(unicode_repr(Nonterminal(tree.label())), end=' ')
for i in range(len(tree)):
if treeloc is not None and i == treeloc[0]:
self._trace_fringe(tree[i], treeloc[1:])
else:
self._trace_fringe(tree[i])
else:
print(unicode_repr(tree), end=' ')
示例13: initialize
# 需要导入模块: from nltk import tree [as 别名]
# 或者: from nltk.tree import Tree [as 别名]
def initialize(self, tokens):
"""
Start parsing a given text. This sets the parser's tree to
the start symbol, its frontier to the root node, and its
remaining text to ``token['SUBTOKENS']``.
"""
self._rtext = tokens
start = self._grammar.start().symbol()
self._tree = Tree(start, [])
self._frontier = [()]
self._tried_e = {}
self._tried_m = {}
self._history = []
self._parses = []
if self._trace:
self._trace_start(self._tree, self._frontier, self._rtext)
示例14: _tree2conll
# 需要导入模块: from nltk import tree [as 别名]
# 或者: from nltk.tree import Tree [as 别名]
def _tree2conll(self, tree, wordnum, words, pos, synt):
assert isinstance(tree, Tree)
if len(tree) == 1 and isinstance(tree[0], compat.string_types):
pos[wordnum] = tree.label()
assert words[wordnum] == tree[0]
return wordnum+1
elif len(tree) == 1 and isinstance(tree[0], tuple):
assert len(tree[0]) == 2
pos[wordnum], pos[wordnum] = tree[0]
return wordnum+1
else:
synt[wordnum] = '(%s%s' % (tree.label(), synt[wordnum])
for child in tree:
wordnum = self._tree2conll(child, wordnum, words,
pos, synt)
synt[wordnum-1] += ')'
return wordnum
示例15: ieer_headlines
# 需要导入模块: from nltk import tree [as 别名]
# 或者: from nltk.tree import Tree [as 别名]
def ieer_headlines():
from nltk.corpus import ieer
from nltk.tree import Tree
print("IEER: First 20 Headlines")
print("=" * 45)
trees = [(doc.docno, doc.headline) for file in ieer.fileids() for doc in ieer.parsed_docs(file)]
for tree in trees[:20]:
print()
print("%s:\n%s" % tree)
#############################################
## Dutch CONLL2002: take_on_role(PER, ORG
#############################################