How to match integers in NLTK CFG?

前端 未结 2 393
醉话见心
醉话见心 2021-01-23 07:13

If I want to define a grammar in which one of the tokens will match an integer, how can i achieve it using nltk\'s string CFG?

For example -

S -> SK S         


        
2条回答
  •  暖寄归人
    2021-01-23 07:34

    A simple solution is to define a function which creates a parser given the sentence and grammar. This works for the integer problem by augmenting the grammar for each function call to include productions for the integers in the sentence. Here is an example function:

    def name_parser(G,sent):
        ints = [i for i in sent if i.isdigit()]
        lproductions = list(G.productions())
        lproduction.extend([nltk.grammar.Production(nltk.grammar.Nonterminal('INT'),[i]) for i in ints])
        lgrammar = nltk.grammar.CFG(G.start(),lproductions)
        parser = nltk.ChartParser(lgrammar)
        for tree in parser.parse(sent):
            print(tree)
    
    

提交回复
热议问题