diff options
author | RossTheRoss <msattr@gmail.com> | 2019-10-19 22:24:14 -0500 |
---|---|---|
committer | RossTheRoss <msattr@gmail.com> | 2019-10-19 22:24:14 -0500 |
commit | 58c3cb062b8ebbfd951bca3270ebccf27c43bd9a (patch) | |
tree | f9a0f73c227a08a6999985245713c2a32d0dc222 /csci1913 | |
parent | It's still fucked (diff) | |
download | homework-58c3cb062b8ebbfd951bca3270ebccf27c43bd9a.tar homework-58c3cb062b8ebbfd951bca3270ebccf27c43bd9a.tar.gz homework-58c3cb062b8ebbfd951bca3270ebccf27c43bd9a.tar.bz2 homework-58c3cb062b8ebbfd951bca3270ebccf27c43bd9a.tar.lz homework-58c3cb062b8ebbfd951bca3270ebccf27c43bd9a.tar.xz homework-58c3cb062b8ebbfd951bca3270ebccf27c43bd9a.tar.zst homework-58c3cb062b8ebbfd951bca3270ebccf27c43bd9a.zip |
Feature-complete but not perfect
Diffstat (limited to 'csci1913')
-rw-r--r-- | csci1913/Python/project1_strap012.py | 43 |
1 files changed, 24 insertions, 19 deletions
diff --git a/csci1913/Python/project1_strap012.py b/csci1913/Python/project1_strap012.py index ad75334..e0c73bb 100644 --- a/csci1913/Python/project1_strap012.py +++ b/csci1913/Python/project1_strap012.py @@ -9,7 +9,7 @@ class Random: return self.newNum def choose(self, limit): - return next()%limit + return self.next()%limit @@ -37,32 +37,40 @@ class Grammar: self.dictionary = {} def rule(self, left, right): - if left in self.dictionary: - self.dictionary[left]+=(Rule(left,right),) + if left not in self.dictionary: + self.dictionary[left]=(Rule(left,right),) else: - self.dictionary[left]=(Rule(left, right),) + self.dictionary[left]+=(Rule(left, right),) def generate(self): if 'Start' in self.dictionary: - self.generating(self.dictionary['Start']) + return self.generating(('Start',)) else: raise RuntimeError + def select(self, left): + rules=self.dictionary[left] + total=len(rules) + index=self.r.choose(total) + chosen=rules[0] #Debug Kludge + while index > 0: + chosen=rules[index] + index-=rules[index].count + for n in range (0, len(rules)): + if rules[n] is not chosen: + rules[n].count+=1 + return chosen.right + def generating(self,strings): result='' for n in range (0, len(strings)): - if strings[n] in self.dictionary: - newStr = select(strings[n]) - generating(newStr) - else: - result += str(strings[n]) + if strings[n] not in self.dictionary: + result += strings[n] result += " " + else: + result += self.generating(self.select(strings[n])) return result - def select(self, left): - rules=self.dictionary[left] - index=self.r.choose(len(rules)) - @@ -79,8 +87,5 @@ 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() +for n in range (0,5): + print(G.generate())
\ No newline at end of file |