This is my current NLTK python grammar:
cfg_2 = CFG.fromstring("""
S -> ADVP VP P
S -> VP NP P
S -> VP P
ADVP -> RB
VP -> VB NP
VP -> VB PP
VP -> VB
VP -> VP CC ADVP VP
NP -> DT NML
VP -> VB NP PP
VP -> VB CC VB
NP -> NP A NP CC NP
NP -> DT NN
NP -> NN
NP -> PRP
NP -> NML NN
PP -> IN NP
NML -> NN CC NN
NML -> CD
DT -> 'the'|'a'
NN -> 'carrots'|'celery'|'courgette'|'garlic'|'onion'|'leek'|'oregano'|'vegetables'|'bowl'|'ends'|'slices'|'beans'|'cannellini'|'water'|'potato'
VB -> 'chop'|'trim'|'peel'|'add'|'cut'|'wash'|'quarter'|'scrub'|'dice'|'drain'
CC -> 'and'
RB -> 'roughly'|'finely'|'lengthways'|'now'|'then'
JJ -> 'large'
A -> ','
P -> '.'
IN -> 'off'|'under'|'into'|'to'
CD -> '2'
PRP -> 'it'
""".format(noun_string))
As you can see, my list of NN, nouns, is quite long. Therefore, I would like to put all my nouns in a list and then add that list to the CFG. I've tried something like this, but this doesn't work because the CFG itself is a string:
noun_list = ['carrots','celery','courgette'
,'garlic','onion','leek','oregano',
'vegetables','bowl','ends','slices','beans','cannellini','water','potato']
noun_string = "'|'".join(noun_list)
print(noun_string)
cfg_2 = CFG.fromstring("""
S -> ADVP VP P
S -> VP NP P
S -> VP P
ADVP -> RB
VP -> VB NP
VP -> VB PP
VP -> VB
VP -> VP CC ADVP VP
NP -> DT NML
VP -> VB NP PP
VP -> VB CC VB
NP -> NP A NP CC NP
NP -> DT NN
NP -> NN
NP -> PRP
NP -> NML NN
PP -> IN NP
NML -> NN CC NN
NML -> CD
DT -> 'the'|'a'
NN -> {}
VB -> 'chop'|'trim'|'peel'|'add'|'cut'|'wash'|'quarter'|'scrub'|'dice'|'drain'
CC -> 'and'
RB -> 'roughly'|'finely'|'lengthways'|'now'|'then'
JJ -> 'large'
A -> ','
P -> '.'
IN -> 'off'|'under'|'into'|'to'
CD -> '2'
PRP -> 'it'
""".format(noun_string))
Is there any other way to add a list of nouns to my CFG?
Put something like <noun_list_goes_here>
in your big string.
Then do:
noun_string = "'|'".join(noun_list)
big_string = big_string.replace('<noun_list_goes_here>', f"'{noun_string}'")`
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.