I'm defining a noun phrase using grammar in nltk. The example provided by nltk is:
grammar = "NP: {<DT>?<NNP>*<NN>}"
Then if I have a sentence like: show me the Paris hospitals, the library can detect the noun phrase:
>>> s
'show me the Paris hospitals'
>>> grammar = "NP: {<DT>?<NNP>*<NNS>}"
>>> nltk.RegexpParser(grammar).parse(nltk.pos_tag(nltk.word_tokenize(s)))
Tree('S', [('show', 'VB'), ('me', 'PRP'), Tree('NP', [('the', 'DT'), ('Paris', 'NNP'), ('hospitals', 'NNS')])])
Now, the sentence can be written in another way: show me the hospitals of Paris, and hence I need to change the grammar to:
>>> grammar = "NP: {<DT>?<NNS><IN><NNP>}"
>>> s = "show me the hospitals in Paris"
>>> nltk.RegexpParser(grammar).parse(nltk.pos_tag(nltk.word_tokenize(s)))
Tree('S', [('show', 'VB'), ('me', 'PRP'), Tree('NP', [('the', 'DT'), ('hospitals', 'NNS'), ('in', 'IN'), ('Paris', 'NNP')])])
How do I combine the two grammars in a unique one? I couldn't figure out the OR condition for the two grammars.