本文整理汇总了Python中nltk.Tree.pprint方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.pprint方法的具体用法?Python Tree.pprint怎么用?Python Tree.pprint使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nltk.Tree
的用法示例。
在下文中一共展示了Tree.pprint方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: evaluate
# 需要导入模块: from nltk import Tree [as 别名]
# 或者: from nltk.Tree import pprint [as 别名]
def evaluate(fragments, sumfunc, condition, normalization, verbose=True, perbook=False, topfragments=False, breakdown=True, conftable=False):
green = "\033[32m"; red = "\033[31m"; gray = "\033[0m" # ANSI codes
names = set(map(getauthor, fragments.values()[0]))
results = {}
# heading
if verbose and not perbook:
print "\n &", 21 * " ",
print "&".join(a.rjust(16) for a in sorted(names)),
print "&\tguess &\t\t\tconfidence\\\\"
prev = "foo.bar"
# loop over texts to be classified
for text in sorted(fragments):
if perbook and getauthor(text) != getauthor(prev):
print "\n &", 21 * " ",
print " &".join("\\rotatebox{45}{%s}" % a.split(" - ")[-1].split(".")[0].replace("&","\\&") for a in sorted(fragments[text])), "\\\\"
if verbose: print text.split(" - ")[-1].split(".")[0][:25].replace("&","\\&").ljust(25),
inter = {}
# loop over possible authors
for author in sorted(fragments[text]):
inter[author] = sum(map(sumfunc, filter(condition, fragments[text][author].items()))) / normalization(text, author)
if verbose:
for author in sorted(inter):
if inter[author] == max(inter.values()): l,r = "\\textbf{","}"
else: l, r = "".ljust(8), " "
if isinstance(inter[author], float): print ("& %s%.2f%s" % (l,inter[author],r)).rjust(16),
elif isinstance(inter[author], int): print ("& %s%d%s" % (l,inter[author],r)).rjust(16),
else: print "& %s%s" % (l,repr(inter[author]).rjust(8),r),
actualauthor = getauthor(text)
guess = max(inter, key=inter.get)
results.setdefault(actualauthor, []).append(guess)
if verbose and not perbook:
print "&",
print green+"correct:" if getauthor(guess) == actualauthor else red+"wrong: ",
print getauthor(guess).ljust(10), gray,
try: confidence = (100 * (max(inter.values()) - sorted(inter.values())[-2]) / float(max(inter.values())))
except ZeroDivisionError: confidence = 0.0
except IndexError: confidence = 0.0
print "& %s%5.2f%s " % ((red if confidence < 50 else green), confidence, gray)
elif verbose: print "\\\\"
prev = text
if verbose: print
if topfragments: print "top fragments"
for name in sorted(names) if topfragments else ():
for text in sorted(fragments):
if not getauthor(text) == name: continue
print text
for label in ("(ROOT", "(S ", "(NP ", "(VP ", "(PP "):
guess = max(fragments[text], key=lambda x: sum(sumfunc(a) for a in fragments[text][x].items() if condition(a)) / norm(x))
try:
frag = max((a[0] for a in fragments[text][guess].iteritems() if condition(a) and a[0].startswith(label)), key=lambda x: (sumfunc((x,fragments[text][guess][x])), fragments[text][guess][x]))
except ValueError: pass
else:
f1 = Tree(frag)
f2 = Tree(frag)
print "%2d" % fragments[text][guess][frag], " ".join(a.replace(" ", "_")[:-1] for a in re.findall(r" \)|[^ )]+\)", frag)),
try: f2.un_chomsky_normal_form()
except: print f1.pprint(margin=9999, parens=("[", " ]"))
else: print f2.pprint(margin=9999, parens=("[", " ]"))
示例2: resolve_corefs
# 需要导入模块: from nltk import Tree [as 别名]
# 或者: from nltk.Tree import pprint [as 别名]
#.........这里部分代码省略.........
for coref in corefs:
# unpack some values
referent = coref[1]
referent_sentence = referent[1]
referent_head = referent[2]
referent_start = referent[3]
referent_end = referent[4]
mention = coref[0]
mention_sentence = mention[1]
mention_head = mention[2]
mention_start = mention[3]
mention_end = mention[4]
mention_sent_list = parsed['sentences'][mention_sentence]['text']
referent_sent_list = parsed['sentences'][referent_sentence]['text']
#make trees
referent_tree = Tree(
parsed['sentences'][referent_sentence]['parsetree'])
mention_tree = Tree(
parsed['sentences'][mention_sentence]['parsetree'])
# handle not found heads
if referent_head == -1:
# if the referent is one word long,
# just use the start index
if (referent_end - referent_start) == 1:
referent_head = referent_start
else:
continue
# if we have a valid head, check to make sure
# non-words aren't making the head index isn't
# mismatched with parsetree index.
referent_head = fix_head_index(
referent_head,
parsed['sentences'][referent_sentence]['text']
)
if mention_head == -1:
if (mention_end - mention_start) == 1:
mention_head = mention_start
else:
continue
# if we have a valid head, check to make sure
# non-words aren't making the head index isn't
# mismatch ed with parsetree index.
mention_head = fix_head_index(
mention_head,
mention_sent_list)
# if head index is out of the tree's range,
# set the head index to be the last leaf in
# the tree.
# try:
# rtp = referent_tree.leaf_treeposition(referent_head)
# except IndexError:
# referent_head = len(referent_tree.leaves())-1
# rtp = referent_tree.leaf_treeposition(referent_head)
# try:
# mtp = mention_tree.leaf_treeposition(mention_head)
# except IndexError:
# mention_head = len(mention_tree.leaves())-1
# mtp = mention_tree.leaf_treeposition(mention_head)
# referent_leaf = get_leaf(rtp, referent_tree)
# mention_leaf = get_leaf(mtp, mention_tree)
rtp, referent_leaf = get_matching_leaf(
referent_head,
referent_sent_list[referent_head],
referent_tree
)
mtp, mention_leaf = get_matching_leaf(
mention_head,
mention_sent_list[mention_head],
mention_tree
)
# ensure that referent_leaf and mention_leaf
# actually match their word.
if mention_sent_list[mention_head] != mention_leaf[-1]:
pass
# print "WARNING: Mention leaf does not match mention."
# consider adding a continue statement here
# ensure referent and mention are unique
if referent_leaf != mention_leaf:
# ensure referents and mentions have the proper tags
if referent_leaf.node in PROP_NOUN_TAGS and (
mention_leaf.node in PROP_NOUN_TAGS
or mention_leaf.node in PRONOUN_TAGS):
set_leaf(mtp, mention_tree, referent_leaf)
parsed['sentences'][mention_sentence]['parsetree'] = \
mention_tree.pprint()
return parsed