def intialize_model(): '''Feed the data to a markov chain and return the object of it''' poems = fetch_poem() mc = MarkovChain() for poem in poems: mc.add_string(poems[poem]) return mc
def main(args): mc = MarkovChain() for a in args[1::]: fd = FetchData(a) mc.add_string(fd.fetch_data()) chain = mc.generate_text() out = (" ").join(chain) print out
def write_song(doc1, doc2): mc = MarkovChain() verse = 1 mc.add_file(doc1) with open(doc2, "w") as f: while verse < 5: f.write(("Verse %s" + "\n") % (verse)) for x in range(4): line = mc.generate_text() join_line = " ".join(line) f.write(join_line.capitalize() + "\n") verse += 1
def write_song(doc1, doc2): mc = MarkovChain() verse = 1 mc.add_file(doc1) with open(doc2, 'w') as f: while verse < 5: f.write(('Verse %s' + '\n') % (verse)) for x in range(4): line = mc.generate_text() join_line = ' '.join(line) f.write(join_line.capitalize() + '\n') f.write('\n') verse += 1
def get_chain(text, num_words): #grab the text from the web. Data shouldn't contain HTML. #initialize the markov chain with text chain = MarkovChain() chain.add_string(text) #ask user for the number of words #generate the markov chain using the number of words provided #by the user and the text from the website entered. chain_out = chain.generate_text(num_words) stripped_chain = format_chain(chain_out) return add_punct(stripped_chain)
from markov_python.cc_markov import MarkovChain mc = MarkovChain() mc.add_file('/Users/mobpro/desktop/winereviews.txt') mc.add_string("red") print str(mc.generate_text(10))
def generate_data(): raw_text = read_data(data_file) mc = MarkovChain() mc.add_string(raw_text) mcg = mc.generate_text() return mcg
from markov_python.cc_markov import MarkovChain import sqlite3 from datetime import datetime mk = MarkovChain() conn = sqlite3.connect('tweets.db') c = conn.cursor() c.execute("SELECT COUNT(DISTINCT tweet) from home_tweets") num_rows = c.fetchone()[0] c.execute("SELECT DISTINCT tweet from home_tweets") tweets = c.fetchmany(num_rows) for tweet in tweets: mk.add_string(list(tweet)[0]) all_status = [] for i in range(int(num_rows / 50)): all_status.append(" ".join(mk.generate_text(20))) with open("data/update_status_" + str(datetime.today()) + ".txt", 'wt', encoding='utf-8') as f: for status in all_status: f.write(status) f.write("\n") pass
from markov_python.cc_markov import MarkovChain import random mc = MarkovChain(3) mc.add_file('phrase_bank.txt') generated_song = "" def generate_verse(): new_verse = "" for lyric in mc.generate_text(random.randint(5, 16)): if lyric == 'i' or lyric == "i'm" or lyric == "i'ma" or lyric == "i'mma": lyric = lyric.capitalize() if new_verse == "": lyric = lyric.capitalize() new_verse += lyric + ' ' return new_verse def generate_song(): generated_song = "" for i in range(0, random.randint(45, 55)): generated_song += (generate_verse() + ('\n')) return generated_song print generate_song()
#runs the project and outputs the text from markov_python.cc_markov import MarkovChain import fetch_data mc = MarkovChain() mc.add_file("sermon_output.txt") initial = mc.generate_text() def convert_to_file(initial): sermon_string = "" for n in initial: sermon_string += (n + " ") with open("Markov_Sermon.txt", "w") as f: f.write(sermon_string) convert_to_file(initial)
from markov_python.cc_markov import MarkovChain import requests from bs4 import BeautifulSoup, Tag r = requests.get('https://www.familyfriendpoems.com/print/poem/NDA3ODk=') response = r.content soup = BeautifulSoup(response, 'html.parser') tag = soup.find_all('p') + soup.find_all('script') + soup.find_all( 'b') + soup.find_all('title') + soup.find_all('a') for i in tag: i.decompose() markov_input = soup.get_text() mc = MarkovChain() mc.add_string(markov_input) a = mc.generate_text() b = mc.generate_text() c = mc.generate_text() d = mc.generate_text() print(' '.join(a)) print(' '.join(b)) print(' '.join(c)) print(' '.join(d)) print(markov_input)
''' Created on June 2, 2018 @author: vanessa ''' from markov_python.cc_markov import MarkovChain mc = MarkovChain() mc.add_file('songtexte.txt') lyric = mc.generate_text() lyric = ' '.join(lyric)
soup = BeautifulSoup(r2.content, "html.parser") # This section pulls out the lyrics and puts into a list rough = soup.find_all(itemprop='description') results2 = list(map(str, rough[0].contents)) stripped_results2 = [] for line in results2[:-5]: if line != "<br/>": if line: stripped_results2.append(line.strip()) song2 = ''.join(stripped_results2) print("*** New song with more Cowbell!", ) #print (stripped_results2) mc = MarkovChain() mc.add_string(song1) mc.add_string(song2) x = mc.generate_text() x = ' '.join(x) print(x) # This section pulls out the breaks # new_rough = rough.replace("<br/>", "") # print(new_rough) # This printed the lyrics but included div class and <br> print(soup.find_all(itemprop='description')) # This found null print(soup.find_all('lyrics')) # from jerry. Said worked on 2.7 -- print ("".join(map(str, div.contents))) #div = soup.find('div', id='lyrics') #soup = BeautifulSoup(r.content, builder=HTMLParserTreeBuilder())
from markov_python.cc_markov import MarkovChain mc = MarkovChain() mc.add_file("text.txt") print(mc.generate_text(15))
from markov_python.cc_markov import MarkovChain mc = MarkovChain() mc.add_file("story.txt") print mc.generate_text()
from fetch_data import train from markov_python.cc_markov import MarkovChain # The above comes from https://github.com/ReagentX/markov_python import re # Get the text from a webpage data = train('http://www.gutenberg.org/files/135/135-h/135-h.htm') # Clean the page to remove any pesky bits data = re.sub('<.*?>', '', data) data = re.sub('&rsquo', '\'', data) data = re.sub('&rdquo|&ldquo', '\"', data) # Make a markov chain object mc = MarkovChain(4) # Add the data mc.add_string(data) text = mc.generate_text(30) result = '' for word in text: result += word + ' ' print(result)
import os from markov_python.cc_markov import MarkovChain mc = MarkovChain() dirname = os.path.dirname(os.path.abspath(__file__)) book = os.path.join(dirname, 'Monologo_do_Vaqueiro_Gil_Vicente.txt') mc.add_file(book) sentence = mc.generate_text() print(sentence)
from markov_python.cc_markov import MarkovChain q = MarkovChain() a = MarkovChain() q.add_file('/users/agaro/desktop/Talk_bot/questions.txt') a.add_file('/users/agaro/desktop/Talk_bot/answers.txt') counter = 0 while counter <= 10: question = q.generate_text(13) question = ' '.join(question) print('Bot A: ') print question answer = a.generate_text(14) print('Bot B: ') answer = ' '.join(answer) print answer counter += 1
from markov_python.cc_markov import MarkovChain mc = MarkovChain() mc.add_file('C:/Users/dell/PycharmProjects/markov_chain/Wine Reviews.txt') mc.add_string("red") word_lst_1 = mc.generate_text(10) print (word_lst_1) word_lst_2 = [] for word in word_lst_1: if word.isdigit() == False: word_lst_2.append(word) print(word_lst_2) not_allowed = ["cases", "made", "has", "while"] not_all_end = ["with", "and", "or", "on", "direct", "that", "are", "now", "through", "the", "supports", "hard", "a"] word_lst_3 = [] for word in word_lst_2: if word not in not_allowed: word_lst_3.append(word) print(word_lst_3) while word_lst_3[len(word_lst_3) - 1] in not_all_end: del word_lst_3[len(word_lst_3) - 1] print(word_lst_3) str1 = ' '.join(word_lst_3)
''' Created on 17/02/2019 @author: dinis ''' from markov_python.cc_markov import MarkovChain mc = MarkovChain() mc.add_file("texto.txt") words_list = mc.generate_text() words_list = " ".join(words_list) print (words_list)
from markov_python.cc_markov import MarkovChain from fetch_data import fetch_data lyrics = fetch_data() mc = MarkovChain() for song in lyrics: mc.add_string(song) print mc """text = mc.generate_text(400) for i in range(0,400,5): for j in text[i:i+5]: print j, print "" if i % 4 == 0: print "\n"""
""" Script for running the project and outputting data """ from markov_python.cc_markov import MarkovChain import fetch_data """ """ # fetch_data.get_text() open_text = open('concatenated.txt') base_text = open_text.read() # base_text = fetch_data.set_text() # base_text = open('concatenated.txt') # print (base_text) # base_text.close() mc = MarkovChain() mc.add_string(base_text) mctext = mc.generate_text() print(mctext)
from markov_python.cc_markov import MarkovChain import scrapy mc = MarkovChain() #add filepath into mc.add_file(filepath) for files created by scrapy mc.add_file('fetched_text') #.generate_text() should generate a list of words listofwords = [] listofwords = mc.generate_text(5) listofwords = " ".join(listofwords) print listofwords
from markov_python.cc_markov import MarkovChain import fetch_data link = 'http://www.e-reading.club/bookreader.php/1020088/Fomina_-_Pritchi._Daosskie%2C_kitayskie%2C_dzenskie.html' #link='http://www.krotov.info/acts/01/joseph/filon_02.htm' vocabul = fetch_data.load_web_to_text(link, 'voc.txt') print type(vocabul) fetch_data.show_param(str(vocabul)) out_fileS = 'out_text_string.txt' out_file = 'out_text.txt' text_object = MarkovChain(3) text_object.add_file('voc.txt') out_text = text_object.generate_text(200) for i in out_text: try: print type(i), i.decode('utf8').encode('cp866') except: print 'Error' out_text_j = ' '.join(out_text) #with open(out_fileS,'w+') as ofile: # for i in out_text: ofile.write(i+'\n') with open(out_file, 'w+') as ofile: for i in out_text: ofile.write(i + ' ')
""" This is the launcher to run the New Shakespeare Sonnet Generator """ $ git init $ git status $ git add . $ git status $ git commit -m "Initial commit" from fetchshakespeare import fetch_data from markov_python.cc_markov import MarkovChain import textwrap #creates and adds the randomly generated data to the string def get_words(): mc.add_string(fetch_data()) #generator mc = MarkovChain() get_words() poem_length = 100 output = mc.generate_text(poem_length) output = " ".join(output) print textwrap.fill(output,width=35).capitalize()
""" This program prompts you to insert lyric urls from azlyrics. It mixes up the lyrics and gives you a new song in the same words by using a Markov Chain Generator """ from fetch_data import get_data from markov_python.cc_markov import MarkovChain import textwrap num = int(raw_input("How many songs do you want to mash up? ")) def get_songs(): for i in range(num): mc.add_string(get_data()) mc = MarkovChain() get_songs() song_length = int(raw_input('How many words do you want in this song? ')) output = mc.generate_text(song_length) output = " ".join(output) print textwrap.fill(output,20)
import fetch_data from markov_python.cc_markov import MarkovChain print "Welcom to MarkovChain" url = raw_input("Provide an URL:") text1 = fetch_data.getURL(url) mc = MarkovChain() mc.add_string(text1) output = mc.generate_text() print output
Learn Python Final Project Markov Chain Text Generator 28 October 2018 """ """ import necessary code (including fetch_data module and a slightly modified Codecademy Markov Chain generator module) """ import fetch_data from markov_python.cc_markov import MarkovChain from random import randint from time import sleep # prepare Markov Chain generator mc = MarkovChain() # links to relevant works wagnerian = { "https://www.gutenberg.org/files/51356/51356-0.txt": "The Birth of Tragedy", "http://www.gutenberg.org/cache/epub/5652/pg5652.txt": "Untimely Meditations(I)", "http://www.gutenberg.org/cache/epub/38226/pg38226.txt": "Untimely Meditations(II)" } positivistic = { "https://www.gutenberg.org/files/51935/51935-0.txt": "Human, All-Too-Human (I)", "https://www.gutenberg.org/files/37841/37841-0.txt":
#test markov chain text gnerator from markov_python.cc_markov import MarkovChain import urllib2 from bs4 import BeautifulSoup import os #testing markov chain from file path text = urllib2.urlopen('http://www.challies.com/') html = text.read() html_soup = BeautifulSoup(html, 'html.parser') html_soup = (html_soup.get_text()) print html_soup text_file = open("test_2", "w+") text_file.write(html_soup) mc = MarkovChain() file_path = os.getcwd() #print file_path #mc.add_file("%s/test_2" % (file_path)) #final = mc.generate_text() #print final text_file.close() #testing markov chain from string
response = urllib2.urlopen(url_path) return response.read() def display_on_console(output): print "\nHere's " + str( len(output)) + " significant words we found in your lyrics:" for word in output: print word, print "\n\n" def write_to_file(output): filename = "lyrics_on_" + str(datetime.now()) file = open(filename, "w") wrdCnt = 0 for word in output: if wrdCnt % WORD_BREAK == 0: file.write("\n") file.write(word + " ") wrdCnt += 1 print "Done. You can see the output at " + filename + "\n\n" generateText = MarkovChain() def run_markov_chain(input): generateText.add_string(input) return generateText.generate_text(len(input))