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


Python Tree.from_stream方法代码示例

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


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

示例1: in

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import from_stream [as 别名]
        )
        (VP
            (VB gave)
            (NP
                (DT the)
                (NN lecture)
            )
        )
    )"""
    
    # uncomment to use the above simple trees for debugging:
    #    trees = [TRANSFORM(Tree.from_string(t)) for t in (t0, t1)]
    #    grammar = PCFG.from_trees(trees)

    # let's get some input to build a grammar:
    grammar = PCFG.from_trees(list(TRANSFORM(t) for t in Tree.from_stream(GzipFile('bigger_treebank_2.txt.gz'))))
    print "Read {} rules in grammar.".format(len(grammar))
    trees = list(TRANSFORM(t) for t in Tree.from_stream(open('end_of_wsj.txt')))
    print "Read {} trees.".format(len(trees))
    
    # now try and parse our trees:
    results = []
    
    for idx, tree in enumerate(trees):
        tokens = [(t,) for t in tree.terminals()]        
        # print 'Sentence {}\tTokens: "{}"'.format(idx, ' '.join(tree.terminals()))
        chart = Chart(grammar, tokens)
        chart.pretty_print()
        has_parse = chart.extract_parse()
        if not has_parse:
            print 'Sentence {}\tTokens: "{}" has no parse!'.format(idx, ' '.join(tree.terminals()))            
开发者ID:kylebgorman,项目名称:kylebgorman.github.io,代码行数:33,代码来源:parsing_717f12c16c1a36.py

示例2: Timeout

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import from_stream [as 别名]

class Timeout(object):
    def __init__(self, seconds=1, error_message="Timeout"):
        self.seconds = seconds
        self.error_message = error_message

    def handle_timeout(self, signum, frame):
        raise TimeoutError(self.error_message)

    def __enter__(self):
        signal.signal(signal.SIGALRM, self.handle_timeout)
        signal.alarm(self.seconds)

    def __exit__(self, type, value, traceback):
        signal.alarm(0)


if __name__ == "__main__":
    pcfg = PCFG.load(PCFG_SOURCE)
    print "PCFG loaded."
    with open(TREE_SOURCE, "r") as source:
        for tree in Tree.from_stream(source):
            tokens = [leaf.decode("ASCII") for leaf in tree.leaves()]
            try:
                with Timeout(TIMEOUT):
                    (_, bw_prob) = CYK_chart(pcfg, tokens)
                    print bw_prob
            except TimeoutError:
                pass
开发者ID:kylebgorman,项目名称:kylebgorman.github.io,代码行数:31,代码来源:generate.py

示例3: of

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import from_stream [as 别名]
        Given a `daughter` (a non-terminal) return a corresponding 
        dictionary of (start, BitWeight) pairs.
        """
        return self.starts.get(daughter)

    def nonterminal_rules(self, daughters):
        """
        Given two `daughters`, return a corresponding dictionary of 
        (nonterminal, BitWeight) pairs.
        """
        return self.nonterms.get(daughters)

    def preterminal_rules(self, terminal):
        """
        Given a `terminal`, return a corresponding dictionary of 
        (preterminal, BitWeight) pairs.
        """
        return self.preterms.get(terminal)

    def __len__(self):
        return len(self.starts) + \
               sum(len(inner) for inner in self.nonterms) + \
               sum(len(inner) for inner in self.preterms)


if __name__ == "__main__":
    with open(SOURCE, "r") as source:
        pcfg = PCFG.from_trees(Tree.from_stream(source))
    print "|G| = {}".format(len(pcfg))
    pcfg.dump(SINK)
开发者ID:kylebgorman,项目名称:kylebgorman.github.io,代码行数:32,代码来源:pcfg.py


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