NLTK (Natural Language Toolkit) lets you parse a FCFG grammar using nltk.FCFG.fromstring([grammar string here]).
Where is the FCFG grammar format specification*? I googled it to death, but all I could find was this .
*ie grammar language specification
The question was asking for FCFG (Feature Grammars) not plain CFG.
I think you can just add square brackets to the nonterminals and have a feature name, an equal sign and a value in the brackets. The value can either be a variable (starting with a question mark), a terminal symbol (for simple values) or a new feature structure. I found this example on the internet ( http://www.nltk.org/howto/featgram.html ) and it is working at my laptop.
from nltk import grammar, parse
g = """
% start DP
DP[AGR=?a] -> D[AGR=?a] N[AGR=?a]
D[AGR=[NUM='sg', PERS=3]] -> 'this' | 'that'
D[AGR=[NUM='pl', PERS=3]] -> 'these' | 'those'
D[AGR=[NUM='pl', PERS=1]] -> 'we'
D[AGR=[PERS=2]] -> 'you'
N[AGR=[NUM='sg', GND='m']] -> 'boy'
N[AGR=[NUM='pl', GND='m']] -> 'boys'
N[AGR=[NUM='sg', GND='f']] -> 'girl'
N[AGR=[NUM='pl', GND='f']] -> 'girls'
N[AGR=[NUM='sg']] -> 'student'
N[AGR=[NUM='pl']] -> 'students'
"""
grammar = grammar.FeatureGrammar.fromstring(g)
tokens = 'these girls'.split()
parser = parse.FeatureEarleyChartParser(grammar)
trees = parser.parse(tokens)
for tree in trees:
tree.draw()
print(tree)
It seems that it doesn't matter whether the feature terminal symbols are quoted or not.
From the demo:
>>> from nltk import CFG
>>> grammar = CFG.fromstring("""
... S -> NP VP
... PP -> P NP
... NP -> Det N | NP PP
... VP -> V NP | VP PP
... Det -> 'a' | 'the'
... N -> 'dog' | 'cat'
... V -> 'chased' | 'sat'
... P -> 'on' | 'in'
... """)
The grammar for writing the grammar from string should work as such:
->
|
Also, see https://en.wikipedia.org/wiki/Terminal_and_nonterminal_symbols and https://en.wikipedia.org/wiki/Context-free_grammar
Wartena is right: the question is indeed FCFG: Feature based Context-Free Gramars. Check this https://nltk.org/book/ch09.html Here is some light into his FCFG:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.