本文整理汇总了Python中nltk.draw.util.CanvasFrame.destroy方法的典型用法代码示例。如果您正苦于以下问题:Python CanvasFrame.destroy方法的具体用法?Python CanvasFrame.destroy怎么用?Python CanvasFrame.destroy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nltk.draw.util.CanvasFrame
的用法示例。
在下文中一共展示了CanvasFrame.destroy方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: quicktree
# 需要导入模块: from nltk.draw.util import CanvasFrame [as 别名]
# 或者: from nltk.draw.util.CanvasFrame import destroy [as 别名]
def quicktree(sentence):
"""Parse a sentence and return a visual representation in IPython"""
import os
from nltk import Tree
from nltk.draw.util import CanvasFrame
from nltk.draw import TreeWidget
from stat_parser import Parser
try:
from IPython.display import display
from IPython.display import Image
except:
pass
try:
get_ipython().getoutput()
except TypeError:
have_ipython = True
except NameError:
import subprocess
have_ipython = False
parser = Parser()
parsed = parser.parse(sentence)
cf = CanvasFrame()
tc = TreeWidget(cf.canvas(),parsed)
cf.add_widget(tc,10,10) # (10,10) offsets
cf.print_to_file('tree.ps')
cf.destroy()
if have_ipython:
tregex_command = 'convert tree.ps tree.png'
result = get_ipython().getoutput(tregex_command)
else:
tregex_command = ["convert", "tree.ps", "tree.png"]
result = subprocess.check_output(tregex_command)
os.remove("tree.ps")
return Image(filename='tree.png')
os.remove("tree.png")
示例2: drawrst
# 需要导入模块: from nltk.draw.util import CanvasFrame [as 别名]
# 或者: from nltk.draw.util.CanvasFrame import destroy [as 别名]
def drawrst(strtree, fname):
""" Draw RST tree into a file
"""
if not fname.endswith(".ps"):
fname += ".ps"
cf = CanvasFrame()
t = Tree.fromstring(strtree)
tc = TreeWidget(cf.canvas(), t)
cf.add_widget(tc,10,10) # (10,10) offsets
cf.print_to_file(fname)
cf.destroy()
示例3: display_tree
# 需要导入模块: from nltk.draw.util import CanvasFrame [as 别名]
# 或者: from nltk.draw.util.CanvasFrame import destroy [as 别名]
def display_tree(tree):
if nltk_is_available:
count = 0
for t in tree:
cf = CanvasFrame()
tc = TreeWidget(cf.canvas(), t)
cf.add_widget(tc, 10, 10)
count += 1
fileName = "tree" + repr(count) + ".ps"
cf.print_to_file(fileName)
cf.destroy()
else:
count = 0
for t in tree:
count += 1
fileName = "tree" + repr(count) + ".txt"
pprint.pprint(t, fileName)
示例4: main
# 需要导入模块: from nltk.draw.util import CanvasFrame [as 别名]
# 或者: from nltk.draw.util.CanvasFrame import destroy [as 别名]
def main(args):
"""
Subcommand main.
You shouldn't need to call this yourself if you're using
`config_argparser`
"""
corpus = read_corpus(args)
odir = get_output_dir(args)
for key in corpus:
cframe = CanvasFrame()
widget = TreeWidget(cframe.canvas(), corpus[key])
cframe.add_widget(widget, 10, 10)
ofilename = fp.join(odir, key.doc) + '.ps'
cframe.print_to_file(ofilename)
cframe.destroy()
announce_output_dir(odir)
示例5: demo
# 需要导入模块: from nltk.draw.util import CanvasFrame [as 别名]
# 或者: from nltk.draw.util.CanvasFrame import destroy [as 别名]
#.........这里部分代码省略.........
from nltk.parse import ViterbiParser
from nltk.grammar import toy_pcfg1, toy_pcfg2
from nltk.draw.tree import draw_trees
from nltk import Tree
from nltk.draw.util import CanvasFrame
from nltk.draw import TreeWidget
# Define two demos. Each demo has a sentence and a grammar.
# demos = [('move the green sphere to the bottom left corner', learned_pcfg),
# ('move the green ball over the red block', learned_pcfg),
# ('take the green pyramid and put it in the top left corner', learned_pcfg),
# ('put the green pyramid on the red block', learned_pcfg),
# ('move the red cylinder and place it on top of the blue cylinder that is on top of a green cylinder', learned_pcfg),]
# Ask the user which demo they want to use.
# print()
# for i in range(len(demos)):
# print('%3s: %s' % (i+1, demos[i][0]))
# print(' %r' % demos[i][1])
# print()
# print('Which demo (%d-%d)? ' % (1, len(demos)), end=' ')
# try:
# snum = int(sys.stdin.readline().strip())-1
# sent, grammar = demos[snum]
# except:
# print('Bad sentence number')
# return
max_scene = 1
if max_scene<10: sc = '0000'+str(max_scene)
elif max_scene<100: sc = '000'+str(max_scene)
elif max_scene<1000: sc = '00'+str(max_scene)
elif max_scene<10000: sc = '0'+str(max_scene)
g = 'grammar_'+sc+'.txt'
learned_pcfg = load('/home/omari/Dropbox/robot_modified/AR/grammar/'+g)
grammar = learned_pcfg
file1 = open('/home/omari/Dropbox/robot_modified/AR/hypotheses/matched_commands.txt', 'r')
g1 = [i for i in file1.readlines()]
for line in g1:
line = unicode(line,encoding='utf-8')
sent = line.split('\n')[0].split('-')[-1]
scene = line.split('\n')[0].split('-')[0]
sent_num = line.split('\n')[0].split('-')[1]
print(line)
if scene == '239' and sent_num == '0': continue
# Tokenize the sentence.
tokens = sent.split()
parser = ViterbiParser(grammar)
all_parses = {}
# print('\nsent: %s\nparser: %s\ngrammar: %s' % (sent,parser,grammar))
parser.trace(3)
parses = parser.parse_all(tokens)
average = (reduce(lambda a,b:a+b.prob(), parses, 0)/len(parses)
if parses else 0)
num_parses = len(parses)
for p in parses:
all_parses[p.freeze()] = 1
# Print some summary statistics
# print()
# print('Time (secs) # Parses Average P(parse)')
# print('-----------------------------------------')
# print('%11.4f%11d%19.14f' % (time, num_parses, average))
parses = all_parses.keys()
if parses:
p = reduce(lambda a,b:a+b.prob(), parses, 0)/len(parses)
else: p = 0
# print('------------------------------------------')
# print('%11s%11d%19.14f' % ('n/a', len(parses), p))
# Ask the user if we should draw the parses.
# print()
# print('Draw parses (y/n)? ', end=' ')
# if sys.stdin.readline().strip().lower().startswith('y'):
# print(' please wait...')
# draw_trees(*parses)
cf = CanvasFrame()
# t = Tree(parses)
t = Tree.fromstring('(S (CH_POS_PREPOST move) (PRE_POST (PRE (the the) (_entity (F_HSV green) (F_SHAPE sphere))) (PREPOST_connect (to to) (the the)) (POST (_F_POS (F_POS (_bottom_left (bottom bottom) (left left)))) (corner corner))))')
tc = TreeWidget(cf.canvas(), t, draggable=1,
node_font=('helvetica', -14),
leaf_font=('helvetica', -12),
roof_fill='white', roof_color='black',
leaf_color='green4', node_color='blue4')
cf.add_widget(tc,10,10)
# tc = TreeWidget(cf.canvas(),t)
# cf.add_widget(tc,10,10) # (10,10) offsets
cf.print_to_file('/home/omari/Dropbox/robot_modified/trees/scene-'+scene+'-'+sent_num+'.ps')
cf.destroy()
示例6: len
# 需要导入模块: from nltk.draw.util import CanvasFrame [as 别名]
# 或者: from nltk.draw.util.CanvasFrame import destroy [as 别名]
{<NOUN><IN><ADJ><NOUN>}
{<ADJ><NOUN><IN><NOUN>}
{<NOUN><IN><NOUN>} # 名词+介词+名词
ADV_ADJ:{<RB><ADJ>} # 副词+形容词
ADJ_PREP_NOUN:{<ADJ><IN><NOUN>} # 形容词+介词+名词
"""
cp = nltk.RegexpParser(grammar)
if len(sys.argv) < 2:
sys.exit(0)
for s in sys.argv[1:]:
print('*****************************')
print(s)
tags = nltk.pos_tag(nltk.word_tokenize(s))
tree = nltk.chunk.ne_chunk(tags)
print(str(tree))
cf = CanvasFrame()
tc = TreeWidget(cf.canvas(), tree)
cf.add_widget(tc, 10, 10)
cf.print_to_file(s+'.1.ps')
cf.destroy()
tree = cp.parse(tags)
print(str(tree))
cf = CanvasFrame()
tc = TreeWidget(cf.canvas(), tree)
cf.add_widget(tc, 10, 10)
cf.print_to_file(s+'.2.ps')