def joke_with_seed(seed): """ Same as joke(), but takes a seed to feed into the Markov Chain. """ if not os.path.isfile('markovdb'): generate_database() chain = MarkovChain() generated_joke = '' while len(generated_joke) < MIN_LENGTH: generated_joke = chain.generateStringWithSeed(seed) return generated_joke
class MarkovBot(BotPlugin): def __init__(self): self.markov = MarkovChain() @botcmd def talk(self, mess, args): """ Generate a sentence based on database """ return self.markov.generateString() @botcmd def complete(self, mess, args): """ Try to complete a sentence """ return self.markov.generateStringWithSeed(args) @botcmd def gendbfromfile(self, mess, args): """ Generate markov chain word database """ try: with open(args) as txtFile: txt = txtFile.read() except IOError as e: return 'Error: could not open text file' # At this point, we've got the file contents if self.markov.generateDatabase(txt): return 'Done.' else: return 'Error: Could not generate database' @botcmd def gendbfromstring(self, mess, args): if self.markov.generateDatabase(args): return 'Done.' else: return 'Error: Could not generate database from String' @botcmd def gendbfromurl(self, mess, args): req = requests.get(args) if req.ok and self.markov.generateDatabase(req.content): return 'Done.' else: return 'Error: Could not generate database from URL'
for root, dirs, files in os.walk(_dir, topdown=False): random.shuffle(files) for name in files: num_files -= 1 if num_files > 0: _path = "{}/{}".format(_dir,name) train_text += file_get_contents(_path) #scrub train_text train_text = re.sub('<[^<]+?>', '', train_text) #remove html train_text = re.sub(r'[^a-zA-Z. ]+', ' ', train_text).replace('\n', '') #remove non-alphanumeric chars for word in remove_words: train_text = train_text.replace(word,'') #remove words #create markov db mc.generateDatabase(train_text) #tweet for x in range(0,num_tweets): random.shuffle(seed_words) status = (ucfirst(mc.generateStringWithSeed(seed_words[0])) + ". ") if not validate_tweet(status): continue; try: status = api.PostUpdate(status) except: pass time.sleep(wait_time_between_tweets_in_secs)
class MarkovBot(BotPlugin): def __init__(self): super(MarkovBot, self).__init__() self.sentenceSep = None self.markov = MarkovChain(dbFilePath="./markovdb") @botcmd def talk(self, mess, args): """ Generate a sentence based on database """ return self.markov.generateString() @botcmd def complete(self, mess, args): """ Try to complete a sentence """ return self.markov.generateStringWithSeed(args) @botcmd def gendbfromfile(self, mess, args): """ Generate markov chain word database based on local file """ try: with open(args) as txtFile: txt = txtFile.read() except IOError as e: return "Error: could not open text file" # At this point, we've got the file contents if self.sentenceSep: result = self.markov.generateDatabase(txt, self.sentenceSep) else: result = self.markov.generateDatabase(txt) if result: return "Done." else: return "Error: Could not generate database" @botcmd def setsentencesep(self, mess, args): """ Specify how to detect sentence borders """ self.sentenceSep = args @botcmd def gendbfromstring(self, mess, args): """ Generate markov chain word database based on given string """ if self.sentenceSep: result = self.markov.generateDatabase(args, self.sentenceSep) else: result = self.markov.generateDatabase(args) if result: return "Done." else: return "Error: Could not generate database from String" @botcmd def gendbfromurl(self, mess, args): """ Generate markov chain word database based on contents of url """ response, content = httplib2.Http().request(args, "GET") if response["status"] == "200": if self.sentenceSep: result = self.markov.generateDatabase(content.decode("utf-8"), self.sentenceSep) else: result = self.markov.generateDatabase(content.decode("utf-8")) if result: return "Done." else: return "Error: Could not generate database from URL"
from pymarkovchain import MarkovChain # Create an instance of the markov chain. By default, it uses MarkovChain.py's location to # store and load its database files to. You probably want to give it another location, like so: mc = MarkovChain("./markovdb.txt") with open("../formatted.txt", "r") as myfile: data=myfile.read() # To generate the markov chain's language model, in case it's not present mc.generateDatabase(data) # To let the markov chain generate some text, execute #print( mc.generateString() ) print( mc.generateStringWithSeed("the") )
# Clean up lyrics lyrics = lyrics.replace('(', '').replace(')', '') lyrics = lyrics.replace('"', '') lyrics = lyrics.lower() #print(lyrics) # Import lyrics #else: # with open(db_path, 'r') as inputfile: # for line in inputfile: # lyrics += line + '\n' # Create model instance mc = MarkovChain('db/' + db_name) # Generate db mc.generateDatabase(lyrics) mc.dumpdb() #------------------------------------------------------------------------------- # Write lyrics f = open(folder + db_name + '.txt', 'w') for i in range(0, int(number_of_phrases)): seed = str(input('seed: ')) f.write(mc.generateStringWithSeed(seed) + '\n') #f.write(mc.generateString() + '\n') f.close
help="Throw out strings shorter than this.", default=3) PARSER.add_argument('--notags', action="store_true", help="Don't generate tags (legacy database compat behaviour)") ARGS = PARSER.parse_args() FILENAME = ARGS.filename NUMBER = ARGS.number BOT = MarkovChain(FILENAME) VALID_SENTENCES = 0 while VALID_SENTENCES < NUMBER: SENTENCE = BOT.generateString() if len(SENTENCE.split()) < ARGS.minlen: continue VALID_SENTENCES += 1 print(SENTENCE) if not ARGS.notags: try: TAGS=BOT.generateStringWithSeed("#") print(TAGS) print(" --- ") except pymarkovchain.StringContinuationImpossibleError as e: print("[FATAL] Your database does not have tag data.") print("You can still generate posts without tags using --notags") import sys sys.exit(1)
# read in data and clean data = json.load(open("listicles.json")) text = "\n".join([d['title'] for d in data if d is not ""]).lower() regex = re.compile('[%s]' % re.escape(string.punctuation)) text = regex.sub(" b", text) # generate MC data mc = MarkovChain("./markov") mc.generateDatabase(text) f = open("potential_tweets.txt", "a") # generate and evaluate tweets while 1: try: seed = sys.argv[1] except: seed = None if seed is not None: tweet = mc.generateStringWithSeed(seed).title() else: tweet = mc.generateString().title() print tweet answer = raw_input("Tweet this text? (yes|no|edit) ") if answer == "yes": f.write(tweet) break elif answer == "edit": tweet = raw_input("Enter in the edited text: ") f.write(tweet) break
final += ts sd = True seed = ts.split()[-1] seed = seed.translate(string.maketrans("",""), string.punctuation) c += 1 print ("\n" + final + "\n") raw_input("press enter to continue...") if raw_input("press enter to begin.") == "beta": pgraph() sd = False s = "" while True: if not sd: ts = mc.generateString() else: ts = mc.generateStringWithSeed(s) if countString(ts) >= msl: os.system("clear") print ("\n" + ts + "\n") sd = False s = raw_input("\npress enter to generate string. : ") if s == '!pg': pgraph() if (len(s) > 0): if f.find(s) != -1: sd = True else: raw_input('could not find "' + s + '" in database\npress enter to continue') sd = False
def generate_horoscope_with_first_word(chain: MarkovChain, first_word: str): return chain.generateStringWithSeed(first_word)
regex = re.compile('[%s]' % re.escape(string.punctuation)) text = regex.sub(" b", text) # generate MC data mc = MarkovChain("./markov") mc.generateDatabase(text) f = open("potential_tweets.txt", "a") # generate and evaluate tweets while 1: try: seed = sys.argv[1] except: seed = None if seed is not None: tweet = mc.generateStringWithSeed(seed).title() else: tweet = mc.generateString().title() print tweet answer = raw_input("Tweet this text? (yes|no|edit) ") if answer == "yes": f.write(tweet) break elif answer == "edit": tweet = raw_input("Enter in the edited text: ") f.write(tweet) break