Context Free Grammar using Shift Reduce Parser

512 views Asked by At

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 -> enter image description here

But apparently Det NP is incorrect grammar. Which area is my mistake and how would I make the shift parser fully parse my sentence.

0

There are 0 answers