-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.py
26 lines (22 loc) · 790 Bytes
/
2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import nltk
def Rec_Des_Parser(grammar, sentence):
parser = nltk.parse.RecursiveDescentParser(grammar)
for word in sentence:
temp = nltk.word_tokenize(word)
for tree in parser.parse(temp):
print(tree)
tree.draw()
grammar=nltk.CFG.fromstring("""
S -> NP VP
NP -> Det N |Det Nom | Det N PP
Nom -> Adj N | Adj Nom
VP -> V | V NP | V NP PP
PP -> P NP|P Nom
Det -> 'the' | 'a'
N -> 'dog' |'cat' | 'girl' |'hair'|'dogs'
Adj -> 'fierce' | 'long' | 'little' | 'tall' |'black' |'three'
V -> 'saw' |'chased' | 'slept' | 'barked'
P -> 'with'|'on'
""")
sentence = ["the fierce dog saw a cat","the girl with long hair slept","the three tall black dogs barked"]
Rec_Des_Parser(grammar, sentence)