Created
August 27, 2013 18:43
-
-
Save Orpheon/6357392 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from __future__ import division, print_function | |
| import random, sys | |
| class Bot(object): | |
| def __init__(self): | |
| self.words = {} | |
| self.LINE_LENGTH = 100 | |
| self.BEGIN = "THIS_IS_A_BEGINNING;_IF_THIS_APPEARS_IN_YOUR_TEXT:FUCK_YOU" | |
| self.beginning_words = [] | |
| def load_from_file(self, filename): | |
| text = open(filename, "r") | |
| textlist = text.read().split() | |
| text.close() | |
| word = textlist[0] | |
| for i in range(len(textlist) -1): | |
| nextword = textlist[i+1] | |
| if word[-1] == "." and i < len(textlist) -1: | |
| self.beginning_words.append(nextword) | |
| if word not in self.words: | |
| self.words[word] = {} | |
| if nextword in self.words[word]: | |
| (self.words[word])[nextword] += 1 | |
| else: | |
| (self.words[word])[nextword] = 1 | |
| word = nextword | |
| print("Finished loading words") | |
| def generate_text(self, num_sentences): | |
| word = random.choice(self.words.keys()).capitalize() | |
| text = word | |
| i = 0 | |
| while i < num_sentences: | |
| choicelist = [] | |
| try: | |
| for nextword, probability in self.words[word].items(): | |
| if nextword not in self.beginning_words: | |
| for j in range(probability): | |
| choicelist.append(nextword) | |
| nextword = random.choice(choicelist) | |
| except: | |
| nextword = random.choice(self.words.keys()) | |
| if text[-1] == ".": | |
| nextword = random.choice(self.beginning_words) | |
| i += 1 | |
| if len(text) - text.rfind("\n") >= self.LINE_LENGTH: | |
| nextword = "\n" + nextword | |
| text += " "+nextword | |
| word = nextword | |
| print("{}% done.".format(int(100*i/num_sentences))) | |
| output = open("output", "w") | |
| output.write(text) | |
| output.close() | |
| bot = Bot() | |
| if sys.argv != '': | |
| bot.load_from_file(sys.argv[0]) | |
| bot.generate_text(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment