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


Python DependencyGraph.tree方法代码示例

本文整理汇总了Python中nltk.parse.dependencygraph.DependencyGraph.tree方法的典型用法代码示例。如果您正苦于以下问题:Python DependencyGraph.tree方法的具体用法?Python DependencyGraph.tree怎么用?Python DependencyGraph.tree使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在nltk.parse.dependencygraph.DependencyGraph的用法示例。


在下文中一共展示了DependencyGraph.tree方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: parse

# 需要导入模块: from nltk.parse.dependencygraph import DependencyGraph [as 别名]
# 或者: from nltk.parse.dependencygraph.DependencyGraph import tree [as 别名]
 def parse(self, tokens):
     """
     Parses the list of tokens subject to the projectivity constraint
     and the productions in the parser's grammar.  This uses a method
     similar to the span-concatenation algorithm defined in Eisner (1996).
     It returns the most probable parse derived from the parser's
     probabilistic dependency grammar.
     """
     self._tokens = list(tokens)
     chart = []
     for i in range(0, len(self._tokens) + 1):
         chart.append([])
         for j in range(0, len(self._tokens) + 1):
             chart[i].append(ChartCell(i, j))
             if i == j + 1:
                 if tokens[i - 1] in self._grammar._tags:
                     for tag in self._grammar._tags[tokens[i - 1]]:
                         chart[i][j].add(DependencySpan(i - 1, i, i - 1, [-1], [tag]))
                 else:
                     print "No tag found for input token '%s', parse is impossible." % tokens[i - 1]
                     return []
     for i in range(1, len(self._tokens) + 1):
         for j in range(i - 2, -1, -1):
             for k in range(i - 1, j, -1):
                 for span1 in chart[k][j]._entries:
                     for span2 in chart[i][k]._entries:
                         for newspan in self.concatenate(span1, span2):
                             chart[i][j].add(newspan)
     graphs = []
     trees = []
     max_parse = None
     max_score = 0
     for parse in chart[len(self._tokens)][0]._entries:
         conll_format = ""
         malt_format = ""
         for i in range(len(tokens)):
             malt_format += "%s\t%s\t%d\t%s\n" % (tokens[i], "null", parse._arcs[i] + 1, "null")
             conll_format += "\t%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t%s\n" % (
                 i + 1,
                 tokens[i],
                 tokens[i],
                 parse._tags[i],
                 parse._tags[i],
                 "null",
                 parse._arcs[i] + 1,
                 "null",
                 "-",
                 "-",
             )
         dg = DependencyGraph(conll_format)
         score = self.compute_prob(dg)
         if score > max_score:
             max_parse = dg.tree()
             max_score = score
     return [max_parse, max_score]
开发者ID:Kuew,项目名称:hashtagify,代码行数:57,代码来源:projectivedependencyparser.py

示例2: parse

# 需要导入模块: from nltk.parse.dependencygraph import DependencyGraph [as 别名]
# 或者: from nltk.parse.dependencygraph.DependencyGraph import tree [as 别名]
    def parse(self, tokens):
        """
        Performs a projective dependency parse on the list of tokens using
        a chart-based, span-concatenation algorithm similar to Eisner (1996).

        :param tokens: The list of input tokens.
        :type tokens: list(str)
        :return: An iterator over parse trees.
        :rtype: iter(Tree)
        """
        self._tokens = list(tokens)
        chart = []
        for i in range(0, len(self._tokens) + 1):
            chart.append([])
            for j in range(0, len(self._tokens) + 1):
                chart[i].append(ChartCell(i, j))
                if i == j + 1:
                    chart[i][j].add(DependencySpan(i - 1, i, i - 1, [-1], ['null']))

        for i in range(1, len(self._tokens) + 1):
            for j in range(i - 2, -1, -1):
                for k in range(i - 1, j, -1):
                    for span1 in chart[k][j]._entries:
                        for span2 in chart[i][k]._entries:
                            for newspan in self.concatenate(span1, span2):
                                chart[i][j].add(newspan)

        for parse in chart[len(self._tokens)][0]._entries:
            conll_format = ""
            #            malt_format = ""
            for i in range(len(tokens)):
                #                malt_format += '%s\t%s\t%d\t%s\n' % (tokens[i], 'null', parse._arcs[i] + 1, 'null')
                # conll_format += '\t%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t%s\n' % (i+1, tokens[i], tokens[i], 'null', 'null', 'null', parse._arcs[i] + 1, 'null', '-', '-')
                # Modify to comply with the new Dependency Graph requirement (at least must have an root elements)
                conll_format += '\t%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t%s\n' % (
                    i + 1,
                    tokens[i],
                    tokens[i],
                    'null',
                    'null',
                    'null',
                    parse._arcs[i] + 1,
                    'ROOT',
                    '-',
                    '-',
                )
            dg = DependencyGraph(conll_format)
            #           if self.meets_arity(dg):
            yield dg.tree()
开发者ID:prz3m,项目名称:kind2anki,代码行数:51,代码来源:projectivedependencyparser.py

示例3: parse

# 需要导入模块: from nltk.parse.dependencygraph import DependencyGraph [as 别名]
# 或者: from nltk.parse.dependencygraph.DependencyGraph import tree [as 别名]
 def parse(self, tokens):
     """
     Parses the list of tokens subject to the projectivity constraint
     and the productions in the parser's grammar.  This uses a method
     similar to the span-concatenation algorithm defined in Eisner (1996).
     It returns the most probable parse derived from the parser's
     probabilistic dependency grammar.
     """
     self._tokens = list(tokens)
     chart = []
     for i in range(0, len(self._tokens) + 1):
         chart.append([])
         for j in range(0, len(self._tokens) + 1):
             chart[i].append(ChartCell(i,j))
             if i==j+1:
                 if tokens[i-1] in self._grammar._tags:
                     for tag in self._grammar._tags[tokens[i-1]]:
                         chart[i][j].add(DependencySpan(i-1,i,i-1,[-1], [tag]))
                 else:
                     chart[i][j].add(DependencySpan(i-1,i,i-1,[-1], [u'NULL']))
                     
     for i in range(1,len(self._tokens)+1):
         for j in range(i-2,-1,-1):
             for k in range(i-1,j,-1):
                 for span1 in chart[k][j]._entries:
                         for span2 in chart[i][k]._entries:
                             for newspan in self.concatenate(span1, span2):
                                 chart[i][j].add(newspan)
     trees = []
     max_parse = None
     max_score = 0
     for parse in chart[len(self._tokens)][0]._entries:
         conll_format = ""
         malt_format = ""
         for i in range(len(tokens)):
             malt_format += '%s\t%s\t%d\t%s\n' % (tokens[i], 'null', parse._arcs[i] + 1, 'null')
             #conll_format += '\t%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t%s\n' % (i+1, tokens[i], tokens[i], parse._tags[i], parse._tags[i], 'null', parse._arcs[i] + 1, 'null', '-', '-')
             # Modify to comply with recent change in dependency graph such that there must be a ROOT element. 
             conll_format += '\t%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t%s\n' % (i+1, tokens[i], tokens[i], parse._tags[i], parse._tags[i], 'null', parse._arcs[i] + 1, 'ROOT', '-', '-')
         dg = DependencyGraph(conll_format)
         score = self.compute_prob(dg)            
         trees.append((score, dg.tree()))
     trees.sort(key=lambda e: -e[0])
     if trees == []:
         trees = [(0.0,Tree(tokens[0],tokens[1:]))]
     return ((score,tree) for (score, tree) in trees)
开发者ID:Tomaat,项目名称:grammarCorrector,代码行数:48,代码来源:parseHack.py


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