aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--csci1913/Python/project1_strap012.py30
1 files changed, 20 insertions, 10 deletions
diff --git a/csci1913/Python/project1_strap012.py b/csci1913/Python/project1_strap012.py
index af39315..ad75334 100644
--- a/csci1913/Python/project1_strap012.py
+++ b/csci1913/Python/project1_strap012.py
@@ -3,17 +3,22 @@ class Random:
self.sevenFive = 16807
self.twoThirtyone = 2147483647
self.newNum = seed
+
def next(self):
self.newNum = self.sevenFive*self.newNum % self.twoThirtyone
return self.newNum
+
def choose(self, limit):
return next()%limit
+
+
class Rule:
def __init__(self, left, right):
self.left = left
self.right = right
self.count = 1
+
def __repr__(self):
string = str(self.count)
string += " "
@@ -24,40 +29,40 @@ class Rule:
string += " "
return string
-def filter(D, E):
- if len(D) is 0:
- return False
- elif D[1] is E:
- return True
- else:
- return filter(D[1:],E)
+
class Grammar:
def __init__(self, seed):
self.r = Random(seed)
self.dictionary = {}
+
def rule(self, left, right):
if left in self.dictionary:
self.dictionary[left]+=(Rule(left,right),)
else:
self.dictionary[left]=(Rule(left, right),)
+
def generate(self):
if 'Start' in self.dictionary:
self.generating(self.dictionary['Start'])
else:
raise RuntimeError
+
def generating(self,strings):
result=''
for n in range (0, len(strings)):
if strings[n] in self.dictionary:
- result += select(strings[n])
+ newStr = select(strings[n])
+ generating(newStr)
else:
- result += strings[n]
+ result += str(strings[n])
result += " "
return result
+
def select(self, left):
rules=self.dictionary[left]
- index=self.r.choose(len(left))
+ index=self.r.choose(len(rules))
+
@@ -74,3 +79,8 @@ G.rule('Story', ('Phrase',)) # 09
G.rule('Story', ('Phrase', 'and', 'Story')) # 10
G.rule('Story', ('Phrase', 'but', 'Story')) # 11
G.rule('Start', ('Story', '.')) # 12
+G.generate()
+G.generate()
+G.generate()
+G.generate()
+G.generate()