本文整理汇总了Python中nltk.Tree.pprint_latex_qtree方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.pprint_latex_qtree方法的具体用法?Python Tree.pprint_latex_qtree怎么用?Python Tree.pprint_latex_qtree使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nltk.Tree
的用法示例。
在下文中一共展示了Tree.pprint_latex_qtree方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: drawTreeTest
# 需要导入模块: from nltk import Tree [as 别名]
# 或者: from nltk.Tree import pprint_latex_qtree [as 别名]
def drawTreeTest(self):
"""Draw a tree using NLTK, the Qtree TeX package, and ImageMagick.
This doesn't really work. It is very slow (esp. XeLaTeX typesetting)
and the trees often spill over the page and are cut up.
"""
report = []
def getSubprocess(programName):
try:
return subprocess.Popen([programName], shell=False,
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
except OSError:
return None
def programIsInstalled(command, name, check):
try:
process = getSubprocess(command)
communication = process.communicate()
response = communication[0].decode('utf-8')
if check in response:
result = True
else:
result = False
except AttributeError:
result = False
return result
# See if NLTK is installed
nltkInstalled = nltkModuleIsInstalled()
# See if XeLaTeX is installed
xelatexInstalled = programIsInstalled('xelatex', 'XeLaTeX',
'This is XeTeX')
# See if ImageMagick is installed
imagemagickInstalled = programIsInstalled('convert', 'ImageMagick',
'ImageMagick')
if not nltkInstalled or not xelatexInstalled or not imagemagickInstalled:
session['flash'] = 'Dependencies for Tree drawing are not installed'
session.save()
return render('/derived/administer/index.html')
# Create the Qtree using NLTK's nltk.Tree class
from nltk import Tree
#tree = Tree('(TOP (S (NP-SBJ (PRP It)) (VP (VBZ has) (NP (NP (DT no) (NN bearing)) (PP-DIR (IN on) (NP (NP (PRP$ our) (NN work) (NN force)) (NP-TMP (NN today)))))) (. .)))')
tree = Tree(u"(TP (DP (D the) (NP (NP (N dog)) (CP (DP 0) (C' (C that) (TP (DP I) (T' (T 0_PAST) (VP (V see) (DP who)))))))) (T' (T 0_PAST) (VP (V be) (AP (A black)))))")
tree = tree.pprint_latex_qtree()
# Write the XeLaTeX file
filesPath = config['app_conf']['permanent_store']
treesPath = os.path.join(filesPath, 'trees')
if not os.path.exists(treesPath):
os.makedirs(treesPath)
formID = str(11)
formTreesPath = os.path.join(treesPath, formID)
if not os.path.exists(formTreesPath):
os.makedirs(formTreesPath)
xelatexDoc = u'%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s' % (
'\documentclass{article}',
'\usepackage{fontspec}',
'\setmainfont{Times}',
'\usepackage{qtree}',
'\\begin{document}',
'\pagestyle{empty}',
'\\tiny',
'%s' % tree,
'\end{document}'
)
xelatexDocPath = os.path.join(formTreesPath, 'tree.tex')
pdfDocPath = os.path.join(formTreesPath, 'tree.pdf')
pngDocPath = os.path.join(formTreesPath, 'tree.png')
xelatexDocFile = codecs.open(xelatexDocPath, 'w', 'utf-8')
xelatexDocFile.write(xelatexDoc)
xelatexDocFile.close()
# Typeset the XeLaTeX file
if not os.path.exists(pdfDocPath):
outDirOpt = '-output-directory=%s' % formTreesPath
args = ('xelatex', outDirOpt, xelatexDocPath,
'-interaction=nonstopmode')
s = subprocess.call(args, shell=False, stdout=subprocess.PIPE)
# Convert the PDF to a PNG
if not os.path.exists(pngDocPath):
args = ('convert', '-density', '400', pdfDocPath, '-resize', '75%',
'-flatten', '-trim', pngDocPath)
s = subprocess.call(args, shell=False, stdout=subprocess.PIPE)
fileReference = url('gettree', id=formID)
img = '<img src="%s" />' % fileReference
c.img = img
#.........这里部分代码省略.........