-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ch2-2PAIP.py
50 lines (36 loc) · 1.06 KB
/
Ch2-2PAIP.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
### Sentence = Noun-Phrase + Verb-Phrase
### Noun-Phrase = Article + Noun
### Verb-Phrase = Verb + Noun-Phrase
### Article = The, A
### Noun = Man, ball, woman, table
### Verb = hit, took, saw, liked
# Supposedly called the 'Context-free phrase structure' with generative syntax
import random
def sentence():
return noun_phrase() + verb_phrase()
def verb_phrase():
return verb() + noun_phrase()
def verb():
return one_of('hit took saw liked'.split())
def article():
return one_of('the a'.split())
def noun():
return one_of('man ball woman table'.split())
def noun_phrase():
return article() + adj_maybe() + noun() + PP_star()
def one_of(l):
return [random.choice(l)]
def adj_maybe():
if random.randint(0, 1) == 0:
return adj() + adj_maybe()
return []
def PP_star():
if random.randint(0, 1) == 0:
return PP() + PP_star()
return []
def PP():
return prep() + noun_phrase()
def prep():
return one_of('to in by with on'.split())
def adj():
return one_of('big little blue green adiabatic'.split())