当前位置: 首页>>代码示例>>Python>>正文


Python Tree.pprint_latex_qtree方法代码示例

本文整理汇总了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
#.........这里部分代码省略.........
开发者ID:jrwdunham,项目名称:old-webapp,代码行数:103,代码来源:administer.py


注:本文中的nltk.Tree.pprint_latex_qtree方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。