@JimSkon/

SentenceGenerator1

Python

No description

fork
loading
Files
  • main.py
main.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
import random
nouns=["box","tree","shovel","cat","window","school","blacksmith"]
adjs=["crazy","odd","orange","silly","hungrey",""]
verbs=["sings","runs","falls","spins","is","snacks","loves","rests"]
dets=["a","my","the","that"]
preps=["on","by","under","in"]

def applyRules(phrase):
  print(phrase)
  newStr=""
  for token in phrase.split():
    if token=="S":
      next="NP VP"
    elif token=="NP":
      next="Det N"
    elif token=="VP":
      next="V"
    elif token=="Det":
      next=random.choice(dets)
    elif token=="N":
      next=random.choice(nouns)
    elif token=="V":
      next=random.choice(verbs)
    else:
      next=token
    newStr += next + " "
  return(newStr)

def createSentence(n,start):
  sent=start
  for i in range(n):
    sent=applyRules(sent)
  return(sent)
   
print(createSentence(4,"S"))