diff options
author | RossTheRoss <msattr@gmail.com> | 2019-10-19 19:01:18 -0500 |
---|---|---|
committer | RossTheRoss <msattr@gmail.com> | 2019-10-19 19:01:18 -0500 |
commit | 9b390d4388c669e0513ca27ace5eefcc2f123847 (patch) | |
tree | e3d37c158cd7e446bb289bab7dee54cbf61aa76b /csci1913/Python | |
parent | Add new stuff to launch (diff) | |
download | homework-9b390d4388c669e0513ca27ace5eefcc2f123847.tar homework-9b390d4388c669e0513ca27ace5eefcc2f123847.tar.gz homework-9b390d4388c669e0513ca27ace5eefcc2f123847.tar.bz2 homework-9b390d4388c669e0513ca27ace5eefcc2f123847.tar.lz homework-9b390d4388c669e0513ca27ace5eefcc2f123847.tar.xz homework-9b390d4388c669e0513ca27ace5eefcc2f123847.tar.zst homework-9b390d4388c669e0513ca27ace5eefcc2f123847.zip |
Start project (halp)
Diffstat (limited to '')
-rw-r--r-- | csci1913/Python/project1_strap012.py | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/csci1913/Python/project1_strap012.py b/csci1913/Python/project1_strap012.py new file mode 100644 index 0000000..af39315 --- /dev/null +++ b/csci1913/Python/project1_strap012.py @@ -0,0 +1,76 @@ +class Random: + def __init__(self, seed): + 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 += " " + string += str(self.left) + string += " -> " + for n in range (0, len(self.right)): + string += self.right[n] + 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]) + else: + result += strings[n] + result += " " + return result + def select(self, left): + rules=self.dictionary[left] + index=self.r.choose(len(left)) + + + +G = Grammar(101) +G.rule('Noun', ('cat',)) # 01 +G.rule('Noun', ('boy',)) # 02 +G.rule('Noun', ('dog',)) # 03 +G.rule('Noun', ('girl',)) # 04 +G.rule('Verb', ('bit',)) # 05 +G.rule('Verb', ('chased',)) # 06 +G.rule('Verb', ('kissed',)) # 07 +G.rule('Phrase', ('the', 'Noun', 'Verb', 'the', 'Noun')) # 08 +G.rule('Story', ('Phrase',)) # 09 +G.rule('Story', ('Phrase', 'and', 'Story')) # 10 +G.rule('Story', ('Phrase', 'but', 'Story')) # 11 +G.rule('Start', ('Story', '.')) # 12 |