示例#1
0
文件: run.py 项目: ygarg25/Codecademy
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
示例#2
0
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
示例#3
0
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
示例#4
0
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)
示例#5
0
def generateLyrics(artists):
	allLyrics = []

	if USE_DATABASE == True:
		# TODO
		return None
	else:
		for artist in artists:
			allLyrics += [item[1] for item in getAllLyrics(artist)]
		# end for

		#print allLyrics

		mc = MarkovChain(2)
		for song in allLyrics:
			mc.add_string(song)
		# end
		
		newLyrics = mc.generate_text(80)

		print newLyrics
		return newLyrics
示例#6
0
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
示例#7
0
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)
示例#8
0
"""
This module will tie together code from fetch_data.py and cc_markov.py.
"""

from markov_python.cc_markov import MarkovChain

import fetch_data

my_chain = MarkovChain()
my_chain.add_string(fetch_data.get_content('http://wiersze.juniora.pl/tuwim/tuwim_l01.html'))

# 3 years

my_text = my_chain.generate_text()

for word in my_text:
    print u"word: {}".format(word)
示例#9
0
"""
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)
示例#10
0
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)
示例#11
0
Created on Mon Sep 11 21:08:34 2017

@author: ihsan
"""

#import urllib
#from bs4 import BeautifulSoup

import fetch_data
from markov_python.cc_markov import MarkovChain

text = fetch_data.get_forecast()
#print (text)

mc = MarkovChain()
mc.add_string(text)

mctext = mc.generate_text()

#Formatting the text


def formatting(strlist):
    new_strlist = []
    days = [
        'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday',
        'sunday'
    ]
    ampm = ['am', 'pm']
    no_comma = ['to', 'mph', 'percent']
    strlist[0] = strlist[0].capitalize()  #cap first word
示例#12
0
'''Running a Markov Chain to build Robert Frost's first "original" poem in 50 years. May need to rerun until you get a good one!'''

from markov_python.cc_markov import MarkovChain
from fetch_data_f import just_poems

mc = MarkovChain()
mc.add_string(just_poems)

#Goal: Make a three stanza poem of 4 lines, 9 words per line in the phrasing of Robert Frost

print '"Random thoughts" by Robert (Markov) Frost\n'


def poem():
    stanzas = 3
    while stanzas > 0:
        stanza()
        print("\n")
        stanzas = stanzas - 1


def stanza():
    set = 4
    while set > 0:
        raw_list = mc.generate_text()
        #convert list to a string, make the first word uppercase and print
        string = ' '.join(raw_list)
        line = string.capitalize()
        print line
        set = set - 1
    content = request.text
    soup = BeautifulSoup(content, "html.parser")
    comments = soup.find_all(id="mid-song-discussion")
    for comment in comments:
        comment.extract()
    return soup.find_all(id="lyrics-body-text")[0].get_text()

def grab_hitler(url):
    request = requests.get(url)
    content = request.text
    soup = BeautifulSoup(content, "html.parser")
    results = soup.find_all('span','bqQuoteLink')
    for result in results:
        yield result.get_text()

for url in taytay_urls:
    mc.add_string(grab_taytay(url))

output =  mc.generate_text(max_length=20)
print "PRE HITLER LYRICS: "
print " ".join(output)

hitlers = grab_hitler(hitler_urls[0])
for hitler in hitlers:
    mc.add_string(hitler)

h_output =  mc.generate_text(max_length=20)
print "POST HITLER LYRICS: "
print " ".join(h_output)

示例#14
0
    print("\nOK, I've loaded the original lyrics.")
    time.sleep(1)

    #below we generate the markov chain

    #first we assign values for words per song and words per line for later use in the Markov Chain
    num_songs = sum([len(albums[x]) for x in selections])
    num_lines = len(out.splitlines())
    num_words = len(out.split())
    lines_per_song = num_lines / num_songs
    words_per_song = num_words / num_songs
    words_per_line = words_per_song / lines_per_song

    #adds album lyrics to Markov chain data
    mc = MarkovChain()
    mc.add_string(out)

    #prompts the user for the first word of the song, with the option to view available words, and generate the chain
    time.sleep(1)
    print(
        "\nRight, let's move on to your new song. It's time to write the first word!"
    )
    print(
        "Note that the word has to be in your chosen album(s) original lyrics at least once, else I can't generate a new song from it."
    )
    print(
        "You can either view the available words, or just try a common song word like 'love' or 'I' to get started."
    )
    time.sleep(3)
    cont3 = validated_input("\nView available words (y/n)? ", ["y", "n"])
    if cont3 == "y":
示例#15
0
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
示例#16
0
def generate_data():
    raw_text = read_data(data_file)
    mc = MarkovChain()
    mc.add_string(raw_text)
    mcg = mc.generate_text()
    return mcg
示例#17
0
# 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())
示例#18
0
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"""
示例#19
0
    desired_period = str(randint(1, 3))

if desired_period == "1":
    period = wagnerian
elif desired_period == "2":
    period = positivistic
else:
    period = late

# use method to read a string (call multiple times to add additional data)
print
print("Accessing works... ")
for link in period:
    if desired_activity == "1":
        print period[link] + ": " + link
    mc.add_string(fetch_data.acquire_data(link))

# smooth out time if playing game
if desired_activity == "2":
    if period == wagnerian:
        sleep(12)
    if period == late:
        sleep(8)

# set parameters of passages (number and length)
number = ""
while number != "1" and number != "2" and number != "3" and number != "4" and number != "5":
    print
    print("How many passages would you like to generate(1-5)")
    number = raw_input("? ")
示例#20
0
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)
示例#21
0
文件: run.py 项目: TobiasFK/stevie
from markov_python.cc_markov import MarkovChain
from fetch_data import lyric_links

lyric_links()

markov_lyrics = open("lyrics.txt", "r")
mc = MarkovChain()
mc.add_string(markov_lyrics.read())
markov_lyrics.close()

answer = " ".join(mc.generate_text())
answer = answer.capitalize()
print answer
示例#22
0
'''Running a Markov Chain to build Robert Frost's first "original" poem in 50 years. May need to rerun until you get a good one!'''


from markov_python.cc_markov import MarkovChain 
from fetch_data_f import just_poems 


mc = MarkovChain()
mc.add_string(just_poems)

#Goal: Make a three stanza poem of 4 lines, 9 words per line in the phrasing of Robert Frost

print '"Random thoughts" by Robert (Markov) Frost\n'

def poem():
	stanzas = 3
	while stanzas > 0:
		stanza()
		print ("\n")
		stanzas = stanzas - 1

def stanza():
	set = 4
	while set > 0:
		raw_list = mc.generate_text()
		#convert list to a string, make the first word uppercase and print
		string = ' '.join(raw_list)
		line = string.capitalize()
		print line
		set = set - 1
		
示例#23
0
        length = raw_input("?: ")
        length = int(length)
    except ValueError:
        print ""
        print "Sorry, that's not a valid length."
        print ""
        print "Please, try again!"
        length = 0

print ""
print "Accessing works... "
for link in books_catalog:
    if book == "A":
        print "Dead Men Tell No Tale"
        mc.add_string(
            fetch_data.fetch_web_data(
                "https://www.gutenberg.org/files/1703/1703-h/1703-h.htm"))
    else:
        print "No Hero"
        mc.add_string(
            fetch_data.fetch_web_data(
                "https://www.gutenberg.org/files/11153/11153-h/11153-h.htm"))
"""
generate text from the Markov Chain and 
convert output to the required format
"""
for current in range(num_quote):
    while True:
        try:
            temporary = mc.generate_text(length)
            formatted = " ".join(temporary)