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


Python Stack.accepts方法代码示例

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


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

示例1: recognize

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import accepts [as 别名]
    def recognize(self, text, token_stream):
        self.results = []
        tokens = morph_parser(token_stream)
        while True:
            stack = Stack(self)
            stack.shift(None, None, 0)
            stack.count_active = 1
            prev_tok = INITIAL_TOKEN
            labels_ok = True

            for token_num, token in enumerate(tokens):
                self.debug("\n\n\nNEW ITERATION. Token:", token[1])
                self.debug(token)
                if len(stack.active) == 0:
                    if not self.error_detected(text, tokens, prev_tok, stack.previously_active):
                        text, tokens = self.without_first_word(text, tokens)
                        break
                    else:
                        continue
                prev_tok = token

                # свертка
                for i, node in stack.enumerate_active():  # S.active may grow
                    state = node.data

                    # raw-слова в кавычках
                    raw_token = "'%s'" % token[1]
                    if raw_token in self.ACTION[state]:
                        for r, rule in ifilter(lambda x: x[0] == 'R', self.ACTION[state][raw_token]):
                            self.debug("- Reduce")
                            self.debug("-- Actions", self.ACTION[state][raw_token])
                            self.debug("-- Raw token", node, rule)
                            labels_ok = self.check_labels(tokens, self.R.labels[rule])
                            if not labels_ok:
                                break
                            stack.reduce(node, rule)

                    # обычные состояния
                    if labels_ok:
                        for r, rule in ifilter(lambda x: x[0] == 'R', self.ACTION[state][token[0]]):
                            self.debug("- Reduce")
                            self.debug("-- Actions", self.ACTION[state])
                            self.debug("-- Normal", node, rule)
                            labels_ok = self.check_labels(tokens, self.R.labels[rule])
                            if not labels_ok:
                                break
                            stack.reduce(node, rule)

                    # имитация конца предложения
                    if labels_ok:
                        for r, rule in ifilter(lambda x: x[0] == 'R', self.ACTION[state]["$"]):
                            self.debug("- Reduce")
                            self.debug("-- Actions", self.ACTION[state])
                            self.debug("-- EOS", node, rule)
                            labels_ok = self.check_labels(tokens, self.R.labels[rule])
                            if not labels_ok:
                                break
                            stack.reduce(node, rule)

                    self.debug("- STACK")
                    if self.debug_mode:
                        stack.dump()

                # последняя свертка не удовлетворила лейблам
                if not labels_ok:
                    self.debug("- Labels not OK")
                    text, tokens = self.without_first_word(text, tokens)
                    break

                # конец?
                if token[0] == '$':
                    acc = stack.accepts()
                    if acc:
                        self.results.append(text)
                        self.debug("- Found new result:", self.results)
                    else:
                        self.error_detected(text, tokens, token, stack.active)
                    return self.results

                # перенос
                stack.count_active = len(stack.active)
                for node in (stack.active[i] for i in xrange(len(stack.active))):
                    # из стека могут удаляться состояния, так что верхний длинный for правда оказался нужен
                    state = node.data

                    # raw-слова в кавычках
                    raw_token = "'%s'" % token[1]
                    if raw_token in self.ACTION[state]:
                        for r, state in ifilter(lambda x: x[0] == 'S',  self.ACTION[state][raw_token]):
                            self.debug("- Shift")
                            self.debug("-- Raw", node, token)
                            stack.shift(node, (token,), state)

                    # обычные состояния
                    for r, state in ifilter(lambda x: x[0] == 'S',  self.ACTION[state][token[0]]):
                        self.debug("- Shift")
                        self.debug("-- Normal", node, token)
                        stack.shift(node, (token,), state)

                    self.debug("- Stack:")
#.........这里部分代码省略.........
开发者ID:A1ik,项目名称:python-glr-parser,代码行数:103,代码来源:automaton.py


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