forked from dmatsanganis/Advanced_Natural_Language_Understanding_System_with_Lexical_Syntactic_and_Semantic_Analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntactic_analyzer.py
23 lines (23 loc) · 1.14 KB
/
syntactic_analyzer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import nltk
grammar = nltk.CFG.fromstring("""
S -> NP VP
NP -> PN | Det N | N
VP -> IV | IV Adv | AV Adj | TV PN NP | V NP
IV -> 'runs' | 'run' | 'running' | 'hurts' | 'hurt' | 'hurting' | 'walks' | 'walk' | 'walking' | 'jumps' | 'jump' | 'jumping' | 'shoots' | 'shoot' | 'shooting'
AV -> 'is' | 'are' | 'does' | 'do'
TV -> 'gives' | 'give' | 'gave' | 'giving'
PN -> 'mary' | 'john' | 'tomy'
Adv -> 'quickly' | 'slowly' | 'independently'
Det -> 'the' | 'a' | 'an'
N -> 'food' | 'cat' | 'dog' | 'dogs' | 'cat' | 'cats' | 'book' | 'books' | 'feather' | 'feathers' | 'baby' | 'babies' | 'boy' | 'boys' | 'girls' | 'girl' | 'icecream' | 'icecreams'
Adj -> 'scary' | 'tall' | 'short' | 'blonde' | 'slim' | 'fat'
V -> 'chased' | 'chase' | 'needs' | 'hates' | 'hate' | 'has' | 'has' | 'have' | 'loves' | 'love' | 'kicks' | 'kick' | 'jumps' | 'jump'
""")
sentence = "mary gave john a book"
sent=sentence.split()
rd_parser = nltk.RecursiveDescentParser(grammar)
print("Parsing the sentence:"+ " " + sentence)
for tree in rd_parser.parse(sent):
print("The tree for the above sentence is:")
print(tree)
break