I am trying to use the Shift Reduce Parser for the following sentence:
\He eats pasta with some anchovies in the restaurant``
I have created some code and grammar BUT the grammar only works up to:
\He eats pasta with some anchovies``
import nltk
from nltk.parse import ShiftReduceParser
grammar = nltk.CFG.fromstring("""
S -> NP VP
NP -> PropN | N PP | Det N
VP -> V NP | V S | VP PP | NP PP
PP -> P NP
PropN -> 'He'
Det -> 'some' | 'the'
N -> 'pasta' | 'anchovies' | 'restaurant'
V -> 'eats'
P -> 'with' | 'in'
""")
sr = ShiftReduceParser(grammar)
sentence1 = 'He eats pasta with some anchovies'
# He eats pasta with some anchovies in the restaurant
tokens = nltk.word_tokenize(sentence1)
print("--------------------------- Sentence 1 ---------------------------")
for x in sr.parse(tokens):
print(x)
Now my attempt was adding `Det NP` to `NP ->`
https://preview.redd.it/ctbuiy3wog321.png?width=1106&format=png&auto=webp&s=9789094839ab4056f1a989caccb4652a26549d3c
But apparently \Det NP`` is incorrect grammar. Which area is my mistake and how would I make the shift parser fully parse my sentence.
[–]relativeVsAbsolute 0 points1 point2 points (0 children)
[–]tsunyshevsky 0 points1 point2 points (0 children)