本文整理匯總了Python中nltk.tree.Tree.fromstring方法的典型用法代碼示例。如果您正苦於以下問題:Python Tree.fromstring方法的具體用法?Python Tree.fromstring怎麽用?Python Tree.fromstring使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類nltk.tree.Tree
的用法示例。
在下文中一共展示了Tree.fromstring方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _parse
# 需要導入模塊: from nltk.tree import Tree [as 別名]
# 或者: from nltk.tree.Tree import fromstring [as 別名]
def _parse(self, t):
try:
return Tree.fromstring(self._normalize(t))
except ValueError as e:
sys.stderr.write("Bad tree detected; trying to recover...\n")
# Try to recover, if we can:
if e.args == ('mismatched parens',):
for n in range(1, 5):
try:
v = Tree(self._normalize(t+')'*n))
sys.stderr.write(" Recovered by adding %d close "
"paren(s)\n" % n)
return v
except ValueError: pass
# Try something else:
sys.stderr.write(" Recovered by returning a flat parse.\n")
#sys.stderr.write(' '.join(t.split())+'\n')
return Tree('S', self._tag(t))
示例2: parse_tree
# 需要導入模塊: from nltk.tree import Tree [as 別名]
# 或者: from nltk.tree.Tree import fromstring [as 別名]
def parse_tree(data, subtrees=False, fine_grained=False):
# https://github.com/pytorch/text/blob/6476392a801f51794c90378dd23489578896c6f2/torchtext/data/example.py#L56
try:
from nltk.tree import Tree
except ImportError:
print("Please install NLTK. " "See the docs at http://nltk.org for more information.")
raise
tree = Tree.fromstring(data)
if subtrees:
return [{
'text': ' '.join(t.leaves()),
'label': get_label_str(t.label(), fine_grained=fine_grained)
} for t in tree.subtrees()]
return {
'text': ' '.join(tree.leaves()),
'label': get_label_str(tree.label(), fine_grained=fine_grained)
}
示例3: _read
# 需要導入模塊: from nltk.tree import Tree [as 別名]
# 或者: from nltk.tree.Tree import fromstring [as 別名]
def _read(self, file_path):
with open(cached_path(file_path), u"r") as data_file:
logger.info(u"Reading instances from lines in file at: %s", file_path)
for line in data_file.readlines():
line = line.strip(u"\n")
if not line:
continue
parsed_line = Tree.fromstring(line)
if self._use_subtrees:
for subtree in parsed_line.subtrees():
instance = self.text_to_instance(subtree.leaves(), subtree.label())
if instance is not None:
yield instance
else:
instance = self.text_to_instance(parsed_line.leaves(), parsed_line.label())
if instance is not None:
yield instance
#overrides
示例4: test_from_tree
# 需要導入模塊: from nltk.tree import Tree [as 別名]
# 或者: from nltk.tree.Tree import fromstring [as 別名]
def test_from_tree(self):
s = '(S (NP (NNP John)) (VP (VBZ loves) (NP (NNP Mary))))'
expected_actions = [
NT('S'),
NT('NP'),
SHIFT,
REDUCE,
NT('VP'),
SHIFT,
NT('NP'),
SHIFT,
REDUCE,
REDUCE,
REDUCE,
]
expected_pos_tags = ['NNP', 'VBZ', 'NNP']
expected_words = ['John', 'loves', 'Mary']
oracle = DiscOracle.from_tree(Tree.fromstring(s))
assert isinstance(oracle, DiscOracle)
assert oracle.actions == expected_actions
assert oracle.pos_tags == expected_pos_tags
assert oracle.words == expected_words
示例5: _parse
# 需要導入模塊: from nltk.tree import Tree [as 別名]
# 或者: from nltk.tree.Tree import fromstring [as 別名]
def _parse(self, t):
try:
return Tree.fromstring(self._normalize(t))
except ValueError as e:
sys.stderr.write("Bad tree detected; trying to recover...\n")
# Try to recover, if we can:
if e.args == ('mismatched parens',):
for n in range(1, 5):
try:
v = Tree(self._normalize(t + ')' * n))
sys.stderr.write(
" Recovered by adding %d close " "paren(s)\n" % n
)
return v
except ValueError:
pass
# Try something else:
sys.stderr.write(" Recovered by returning a flat parse.\n")
# sys.stderr.write(' '.join(t.split())+'\n')
return Tree('S', self._tag(t))
示例6: initialize_edu_data
# 需要導入模塊: from nltk.tree import Tree [as 別名]
# 或者: from nltk.tree.Tree import fromstring [as 別名]
def initialize_edu_data(edus):
'''
Create a representation of the list of EDUS that make up the input.
'''
wnum = 0 # counter for distance features
res = []
for edu_index, edu in enumerate(edus):
# lowercase all words
edu_words = [x[0].lower() for x in edu]
edu_pos_tags = [x[1] for x in edu]
# make a dictionary for each EDU
new_tree = Tree.fromstring('(text)')
new_tree.append('{}'.format(edu_index))
tmp_item = {"head_idx": wnum,
"start_idx": wnum,
"end_idx": wnum,
"nt": "text",
"head": edu_words,
"hpos": edu_pos_tags,
"tree": new_tree}
wnum += 1
res.append(tmp_item)
return res
示例7: test
# 需要導入模塊: from nltk.tree import Tree [as 別名]
# 或者: from nltk.tree.Tree import fromstring [as 別名]
def test():
"""Do some tree drawing tests."""
def print_tree(n, tree, sentence=None, ansi=True, **xargs):
print()
print('{0}: "{1}"'.format(n, ' '.join(sentence or tree.leaves())))
print(tree)
print()
drawtree = TreePrettyPrinter(tree, sentence)
try:
print(drawtree.text(unicodelines=ansi, ansi=ansi, **xargs))
except (UnicodeDecodeError, UnicodeEncodeError):
print(drawtree.text(unicodelines=False, ansi=False, **xargs))
from nltk.corpus import treebank
for n in [0, 1440, 1591, 2771, 2170]:
tree = treebank.parsed_sents()[n]
print_tree(n, tree, nodedist=2, maxwidth=8)
print()
print('ASCII version:')
print(TreePrettyPrinter(tree).text(nodedist=2))
tree = Tree.fromstring(
'(top (punct 8) (smain (noun 0) (verb 1) (inf (verb 5) (inf (verb 6) '
'(conj (inf (pp (prep 2) (np (det 3) (noun 4))) (verb 7)) (inf (verb 9)) '
'(vg 10) (inf (verb 11)))))) (punct 12))', read_leaf=int)
sentence = ('Ze had met haar moeder kunnen gaan winkelen ,'
' zwemmen of terrassen .'.split())
print_tree('Discontinuous tree', tree, sentence, nodedist=2)
示例8: _make_tree
# 需要導入模塊: from nltk.tree import Tree [as 別名]
# 或者: from nltk.tree.Tree import fromstring [as 別名]
def _make_tree(self, result):
return Tree.fromstring(result)
示例9: _scored_parse_to_nltk_tree
# 需要導入模塊: from nltk.tree import Tree [as 別名]
# 或者: from nltk.tree.Tree import fromstring [as 別名]
def _scored_parse_to_nltk_tree(scored_parse):
return Tree.fromstring(str(scored_parse.ptb_parse))
示例10: fromtree
# 需要導入模塊: from nltk.tree import Tree [as 別名]
# 或者: from nltk.tree.Tree import fromstring [as 別名]
def fromtree(cls, data, fields, subtrees=False):
try:
from nltk.tree import Tree
except ImportError:
print("Please install NLTK. "
"See the docs at http://nltk.org for more information.")
raise
tree = Tree.fromstring(data)
if subtrees:
return [cls.fromlist(
[' '.join(t.leaves()), t.label()], fields) for t in tree.subtrees()]
return cls.fromlist([' '.join(tree.leaves()), tree.label()], fields)
示例11: __process_trees
# 需要導入模塊: from nltk.tree import Tree [as 別名]
# 或者: from nltk.tree.Tree import fromstring [as 別名]
def __process_trees(self, s):
"""
Input
a string representing Penn parsetrees, delimited by |||
Value:
A list of NLTK Tree objects.
"""
if not s:
return []
tree_strs = s.split("|||")
return [Tree.fromstring(s) for s in tree_strs]
######################################################################
示例12: parse_tree_to_sentence
# 需要導入模塊: from nltk.tree import Tree [as 別名]
# 或者: from nltk.tree.Tree import fromstring [as 別名]
def parse_tree_to_sentence(parse_tree:str)-> List[str]:
return Tree.fromstring(parse_tree).leaves()
示例13: test_strip_functional_tags
# 需要導入模塊: from nltk.tree import Tree [as 別名]
# 或者: from nltk.tree.Tree import fromstring [as 別名]
def test_strip_functional_tags(self):
ptb_reader = PennTreeBankConstituencySpanDatasetReader()
# Get gold spans should strip off all the functional tags.
tree = Tree.fromstring(u"(S (NP=PRP (D the) (N dog)) (VP-0 (V chased) (NP|FUN-TAGS (D the) (N cat))))")
ptb_reader._strip_functional_tags(tree)
assert tree == Tree.fromstring(u"(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))")
示例14: test_get_gold_spans_correctly_extracts_spans
# 需要導入模塊: from nltk.tree import Tree [as 別名]
# 或者: from nltk.tree.Tree import fromstring [as 別名]
def test_get_gold_spans_correctly_extracts_spans(self):
ptb_reader = PennTreeBankConstituencySpanDatasetReader()
tree = Tree.fromstring(u"(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))")
span_dict = {}
ptb_reader._get_gold_spans(tree, 0, span_dict)
spans = list(span_dict.items()) # pylint: disable=protected-access
assert spans == [((0, 1), u'NP'), ((3, 4), u'NP'), ((2, 4), u'VP'), ((0, 4), u'S')]
示例15: _load_cparse
# 需要導入模塊: from nltk.tree import Tree [as 別名]
# 或者: from nltk.tree.Tree import fromstring [as 別名]
def _load_cparse(cparse_filename):
'''
load the constituent parse tree
'''
from nltk.tree import Tree
ctree_list = []
with codecs.open(cparse_filename,'r',encoding='utf-8') as cf:
for line in cf:
ctree_list.append(Tree.fromstring(line.strip()))
return ctree_list