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


Python Grammar.read方法代码示例

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


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

示例1: test2

# 需要导入模块: from grammar import Grammar [as 别名]
# 或者: from grammar.Grammar import read [as 别名]
def test2():

    print("\n\n------ Test 2 ---------")

    # Test grammar 2 - read from file

    g = Grammar()
    g.read("Reynir.test.grammar")

    #print("Grammar:")
    #print(str(g))
    #print()

    # s = "Villi leit út eða Anna og köttur komu beint heim og kona eða maður fóru snemma inn"
    s = "kona með kött myrti mann með hálsbindi með hund og Páll fór út"
    # s = "kona með kött myrti mann með hund og Villi fór út"
    # s = "Villi leit út"

    class NameToken(Token):

        NÖFN_NF = ["Villi", "Anna", "Hlín", "Páll"]
        NÖFN_ÞF = ["Villa", "Önnu", "Hlín", "Pál"]
        NÖFN_ÞGF = ["Villa", "Önnu", "Hlín", "Páli"]

        def matches(self, terminal):
            """ Does this token match the given terminal? """
            if not terminal.name().startswith("nafn_"):
                return False
            if terminal.name().endswith("_nf"):
                return self._val in NameToken.NÖFN_NF
            if terminal.name().endswith("_þf"):
                return self._val in NameToken.NÖFN_ÞF
            if terminal.name().endswith("_þgf"):
                return self._val in NameToken.NÖFN_ÞGF
            return False

    def make_token(w):
        if w[0].isupper():
            return NameToken('nafn', w)
        return Token('orð', w)

    toklist = [make_token(w) for w in s.split()]

    p = Parser.for_grammar(g)

    forest = p.go(toklist)

    print("Parse combinations: {0}".format(Parser.num_combinations(forest)))

    Parser.print_parse_forest(forest)
开发者ID:ebergmundur,项目名称:Reynir,代码行数:52,代码来源:ptest.py


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