本文整理汇总了Python中nltk.Tree.fromstring方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.fromstring方法的具体用法?Python Tree.fromstring怎么用?Python Tree.fromstring使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nltk.Tree
的用法示例。
在下文中一共展示了Tree.fromstring方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: match
# 需要导入模块: from nltk import Tree [as 别名]
# 或者: from nltk.Tree import fromstring [as 别名]
def match(self, tree):
try:
if tree.label() != 'ROOT':
raise IndexError
if tree[0].label() != 'SBARQ':
raise IndexError
if tree[0][0][0].label() != 'WRB':
raise IndexError
if tree[0][0][0][0].lower() != 'when':
raise IndexError
if tree[0][1].label() != 'SQ':
raise IndexError
if tree[0][1][0].label() != 'VBD':
raise IndexError
if tree[0][1][1].label() != 'NP':
raise IndexError
if tree[0][1][2].label() != 'VP':
raise IndexError
part = Pattern.Part()
part.object = ParentedTree.fromstring(str(tree[0][1][1]))
part.property = ParentedTree.fromstring(str(Tree('VP', [
Tree.fromstring(str(tree[0][0][0])),
Tree.fromstring(str(tree[0][1][0])),
Tree.fromstring(str(tree[0][1][2]))
])))
return [part]
except IndexError:
return []
示例2: read_treefile
# 需要导入模块: from nltk import Tree [as 别名]
# 或者: from nltk.Tree import fromstring [as 别名]
def read_treefile(hyptreefile,reftreefile):
hfile = codecs.open(hyptreefile,"r",encoding='utf-8')
rfile = codecs.open(reftreefile,"r",encoding='utf-8')
scoredic = {}
#store rtree into rtreelist suppose there are more than one reference
rtreel = []
for i in rfile:
refl = []
if i.strip() != "":
refl.append(i.strip())
rstr = " ".join(refl)
rtree = Tree.fromstring(rstr)
rtreel.append(rtree)
#store hyptree into hyplist
htreel = []
senl = []
for i in hfile:
if i.strip() != "":
senl.append(i.strip())
else:
htreel.append(Tree.fromstring(" ".join(senl)))
senl = []
#loop and score
for r in rtreel:
for h in htreel:
score,hword,rword= score_similarity(h,r)
scoredic[" ".join(hword)] = score
return scoredic
示例3: parser_output_to_parse_deriv_trees
# 需要导入模块: from nltk import Tree [as 别名]
# 或者: from nltk.Tree import fromstring [as 别名]
def parser_output_to_parse_deriv_trees(output):
lines = output.strip().split("\n")
deriv_tree_lines = lines[::2]
parse_tree_lines = lines[1::2]
parse_trees = [Tree.fromstring(line.replace('\x06', 'epsilon_')) for line in parse_tree_lines if line != '']
deriv_trees = [Tree.fromstring(line) for line in deriv_tree_lines if line != '']
return parse_trees, deriv_trees
示例4: test_evalb_correctly_scores_identical_trees
# 需要导入模块: from nltk import Tree [as 别名]
# 或者: from nltk.Tree import fromstring [as 别名]
def test_evalb_correctly_scores_identical_trees(self):
tree1 = Tree.fromstring("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))")
tree2 = Tree.fromstring("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))")
evalb_scorer = EvalbBracketingScorer()
evalb_scorer([tree1], [tree2])
metrics = evalb_scorer.get_metric()
assert metrics["evalb_recall"] == 1.0
assert metrics["evalb_precision"] == 1.0
assert metrics["evalb_f1_measure"] == 1.0
示例5: test_evalb_correctly_calculates_bracketing_metrics_over_multiple_trees
# 需要导入模块: from nltk import Tree [as 别名]
# 或者: from nltk.Tree import fromstring [as 别名]
def test_evalb_correctly_calculates_bracketing_metrics_over_multiple_trees(self):
tree1 = Tree.fromstring("(S (VP (D the) (NP dog)) (VP (V chased) (NP (D the) (N cat))))")
tree2 = Tree.fromstring("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))")
evalb_scorer = EvalbBracketingScorer()
evalb_scorer([tree1, tree2], [tree2, tree2])
metrics = evalb_scorer.get_metric()
assert metrics["evalb_recall"] == 0.875
assert metrics["evalb_precision"] == 0.875
assert metrics["evalb_f1_measure"] == 0.875
示例6: test_evalb_correctly_scores_imperfect_trees
# 需要导入模块: from nltk import Tree [as 别名]
# 或者: from nltk.Tree import fromstring [as 别名]
def test_evalb_correctly_scores_imperfect_trees(self):
# Change to constiutency label (VP ... )should effect scores, but change to POS
# tag (NP dog) should have no effect.
tree1 = Tree.fromstring("(S (VP (D the) (NP dog)) (VP (V chased) (NP (D the) (N cat))))")
tree2 = Tree.fromstring("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))")
evalb_scorer = EvalbBracketingScorer()
evalb_scorer([tree1], [tree2])
metrics = evalb_scorer.get_metric()
assert metrics["evalb_recall"] == 0.75
assert metrics["evalb_precision"] == 0.75
assert metrics["evalb_f1_measure"] == 0.75
示例7: test_evalb_with_terrible_trees_handles_nan_f1
# 需要导入模块: from nltk import Tree [as 别名]
# 或者: from nltk.Tree import fromstring [as 别名]
def test_evalb_with_terrible_trees_handles_nan_f1(self):
# If precision and recall are zero, evalb returns nan f1.
# This checks that we handle the zero division.
tree1 = Tree.fromstring("(PP (VROOT (PP That) (VROOT (PP could) "
"(VROOT (PP cost) (VROOT (PP him))))) (PP .))")
tree2 = Tree.fromstring("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))")
evalb_scorer = EvalbBracketingScorer()
evalb_scorer([tree1], [tree2])
metrics = evalb_scorer.get_metric()
assert metrics["evalb_recall"] == 0.0
assert metrics["evalb_precision"] == 0.0
assert metrics["evalb_f1_measure"] == 0.0
示例8: pprint
# 需要导入模块: from nltk import Tree [as 别名]
# 或者: from nltk.Tree import fromstring [as 别名]
def pprint(self, **kwargs):
"""Returns a representation of the tree compatible with the LaTeX
qtree package. Requires the nltk module. See
http://www.nltk.org/_modules/nltk/tree.html."""
from nltk import Tree as NLTKTree
tree = NLTKTree.fromstring(self.ptb())
return tree.pprint(**kwargs)
示例9: extract_trees
# 需要导入模块: from nltk import Tree [as 别名]
# 或者: from nltk.Tree import fromstring [as 别名]
def extract_trees(filename="./out/toy_pcfg2.gen"):
trees = []
with open(filename) as fh:
for line in fh:
trees.append(Tree.fromstring(line))
return trees
示例10: __render_tree
# 需要导入模块: from nltk import Tree [as 别名]
# 或者: from nltk.Tree import fromstring [as 别名]
def __render_tree(self):
string = self.output_text_area.get("1.0", END)
string = string.replace("\n", "")
tree = Tree.fromstring(string)
tree.draw()
示例11: testConvert
# 需要导入模块: from nltk import Tree [as 别名]
# 或者: from nltk.Tree import fromstring [as 别名]
def testConvert(self):
sample_tree = Tree.fromstring("(S (NP I) (VP (V saw) (NP him)))")
converter = DotLanguageConverter()
str = converter.convert(sample_tree)
expected_tree_string = ("digraph parse_tree {\n"
"\t\"S\" [label=\"S\"];\n"
"\t\"NP\" [label=\"NP\"];\n"
"\t\"S\"-> \"NP\";\n"
"\t\"I\" [label=\"I\"];\n"
"\t\"NP\"-> \"I\";\n"
"\t\"VP\" [label=\"VP\"];\n"
"\t\"S\"-> \"VP\";\n"
"\t\"V\" [label=\"V\"];\n"
"\t\"VP\"-> \"V\";\n"
"\t\"saw\" [label=\"saw\"];\n"
"\t\"V\"-> \"saw\";\n"
"\t\"NP_1\" [label=\"NP\"];\n"
"\t\"VP\"-> \"NP_1\";\n"
"\t\"him\" [label=\"him\"];\n"
"\t\"NP_1\"-> \"him\";\n"
"}")
self.assertEqual(str, expected_tree_string)
示例12: calc
# 需要导入模块: from nltk import Tree [as 别名]
# 或者: from nltk.Tree import fromstring [as 别名]
def calc(param):
p = ["He", "he", "Him", "him", "She", "she", "Her",
"her", "It", "it", "They", "they"]
r = ["Himself", "himself", "Herself", "herself",
"Itself", "itself", "Themselves", "themselves"]
fname = param[1]
pro = param[2]
with open(fname) as f:
sents = f.readlines()
trees = [Tree.fromstring(s) for s in sents]
pos = get_pos(trees[-1], pro)
pos = pos[:-1]
if pro in p:
tree, pos = hobbs(trees, pos)
#for t in trees:
# print t, '\n'
#print "Proposed antecedent for '"+pro+"':", tree[pos]
return tree, tree[pos]
elif pro in r:
tree, pos = resolve_reflexive(trees, pos)
#for t in trees:
# print t, '\n'
#print "Proposed antecedent for '"+pro+"':", tree[pos]
return tree, tree[pos]
示例13: main
# 需要导入模块: from nltk import Tree [as 别名]
# 或者: from nltk.Tree import fromstring [as 别名]
def main(argv):
if len(sys.argv) == 2 and argv[1] == "demo":
demo()
else:
if len(sys.argv) > 3 or len(sys.argv) < 2:
print "Enter the file and the pronoun to resolve."
elif len(sys.argv) == 3:
p = ["He", "he", "Him", "him", "She", "she", "Her",
"her", "It", "it", "They", "they"]
r = ["Himself", "himself", "Herself", "herself",
"Itself", "itself", "Themselves", "themselves"]
fname = sys.argv[1]
pro = sys.argv[2]
with open(fname) as f:
sents = f.readlines()
trees = [Tree.fromstring(s) for s in sents]
pos = get_pos(trees[-1], pro)
pos = pos[:-1]
if pro in p:
tree, pos = hobbs(trees, pos)
for t in trees:
print t, '\n'
print "Proposed antecedent for '"+pro+"':", tree[pos]
elif pro in r:
tree, pos = resolve_reflexive(trees, pos)
for t in trees:
print t, '\n'
print "Proposed antecedent for '"+pro+"':", tree[pos]
示例14: syntactic_parse_features
# 需要导入模块: from nltk import Tree [as 别名]
# 或者: from nltk.Tree import fromstring [as 别名]
def syntactic_parse_features(paragraph, parse):
""" Returns the count for the usage of S, SBAR units in the syntactic parse,
plus statistics about the height of the trees """
KEPT_FEATURES = ['S', 'SBAR']
# Increment the count for the part-of-speech of each head of phrase
counts_of_heads = Counter()
tree_heights = []
for t_string in parse:
t = Tree.fromstring(t_string)
for st in t.subtrees():
counts_of_heads[st.label()] += 1
tree_heights.append(t.height())
# Keep only the head parts-of-speech that appear in KEPT_FEATURES
features = dict(("syntactic_head_"+key, counts_of_heads[key]) for
key in counts_of_heads if key in KEPT_FEATURES)
features = Counter(features)
# Add in the features related to tree height
features["tree_height_mean"] = np.mean(tree_heights)
features["tree_height_median"] = np.median(tree_heights)
features["tree_height_max"] = np.max(tree_heights)
features["tree_height_min"] = np.min(tree_heights)
features["tree_height_spread"] = np.max(tree_heights) - np.min(tree_heights)
return Counter(features)
示例15: treebank_bracket_parse
# 需要导入模块: from nltk import Tree [as 别名]
# 或者: from nltk.Tree import fromstring [as 别名]
def treebank_bracket_parse(t):
try:
return Tree.fromstring(t, remove_empty_top_bracketing=True)
except IndexError:
# in case it's the real treebank format,
# strip first and last brackets before parsing
return tree.bracket_parse(t.strip()[1:-1])