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


Python Grammar.get_possible_parent_rules方法代码示例

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


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

示例1: __init__

# 需要导入模块: from grammar import Grammar [as 别名]
# 或者: from grammar.Grammar import get_possible_parent_rules [as 别名]

#.........这里部分代码省略.........
                    # (0 is start of sentence)
        for token in tokens:
            node += 1
            rule = ProductionRule(token, [], 1.0)
            edge = Edge(node, node+1, rule, 0, [])
            self.queue.add_edge(edge)

    def enough_parses_found(self, number_of_parses):
        '''
        Check if enough parses have been found for the input sentence

        Return True if the number of complete S edges that the chart
        contains is >= the number of parses that the user wants, else
        False
        '''
        return False if (number_of_parses == -1 or len(self.chart.get_s_edges()) < number_of_parses) else True

    def predict_rule(self, complete_edge):
        '''
        If the LHS of a complete edge can be the first RHS element of
        a production rule, create a self-loop edge with that rule and
        push it to the queue

        Input: Complete edge
        Push to queue: Incomplete self-loop edges

        Formal definition:
            For each complete edge [A -> alpha . , (i, j)]
            and each production rule  B -> A beta,
            add the self-loop edge [B -> . A beta , (i, i)]
        '''
        start = complete_edge.get_start()
        lhs = complete_edge.get_prod_rule().get_lhs()
        parent_rules = self.grammar.get_possible_parent_rules(lhs)

        for parent_rule in parent_rules:
            new_edge = Edge(start, start, parent_rule, 0, [])
            if not self.queue.has_edge(new_edge) and not self.chart.has_edge(new_edge):
                self.queue.add_edge(new_edge)

    def fundamental_rule(self, input_edge):
        '''
        If an incomplete edge can be advanced by a complete edge,
        create a new edge with the advanced dot.

        Create new edges (which can be complete or incomplete) by
        "advancing the dot", i.e. by matching incomplete edges with
        appropriate complete ones:

        (1) If the input edge is incomplete, find all complete edges
            - whose start node equals the end node of the input edge
            - whose LHS matches the RHS element
              that the input edge is currently looking for.
            If the input edge is complete, find all incomplete edges
            - whose end node equals the start node of the input edge
            - whose dot can be advanced by pairing them with the input
              edge.
        (2) From every pairing, create a new edge with the dot
            advanced over the RHS element that has just been found.
        (3) Push that edge to the queue IFF it does not exist already,
            i.e. if it has not been added to the chart or the queue
            before. This constraint keeps the parser from entering an
            infinite loop when using left-recursive grammar rules.

        Input: Single edge
        Push to queue: Complete and incomplete edges
开发者ID:itsjeyd,项目名称:BOP,代码行数:70,代码来源:bottom_up_chart_parser.py


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