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
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)