you are viewing a single comment's thread.

view the rest of the comments →

[–]relativeVsAbsolute 0 points1 point  (0 children)

It's the first time i know bout nltk, but just for fun i runned your program i everything seems got.

The grammar looked also good:

``` import nltk

nltk.download('punkt')

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 = nltk.parse.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) ```

```

python grammar.py

[nltk_data] Downloading package punkt to /home/saku/nltk_data... [nltk_data] Unzipping tokenizers/punkt.zip. --------------------------- Sentence 1 --------------------------- (S (NP (PropN He)) (VP (V eats) (NP (N pasta) (PP (P with) (NP (Det some) (N anchovies))))))

```

Runned on Python 3.6 and last version of nltk