Esempio n. 1
0
 def load_chain(self, filepath, key):
     print(filepath, key)
     c = Chain()
     with open(filepath) as f:
         for line in f:
             c.grow(line.split())
     self.cache[key] = c
Esempio n. 2
0
def test_generate():
    c = Chain()
    c.grow(s1)
    c.grow(s2)

    for i in range(10):
        print('%s.' % ' '.join(c.generate()))
Esempio n. 3
0
def test_generate_rms():
    c = Chain()

    d = os.path.dirname(__file__)
    filename = os.path.join(d, 'files/rms.txt')
    with open(filename, 'r') as f:
        content = f.read()
    for sentence in content.split('.'):
        words = sentence.split()
        c.grow(words)

    for i in range(10):
        print('%s.' % ' '.join(c.generate()))
Esempio n. 4
0
    def __init__(self):
        """Reads in phonetic data and creates a markov chain"""
        data = []
        for x in open('data/phonmap.txt').readlines():
            data.extend(x.split())

        #Build up a markov chain with it
        self.chain = Chain(data, 2)
Esempio n. 5
0
def build_chain(db, max_comments=None):
    c = Chain(tokenizer=markdown_tokenizer(), N=10)

    comment_count = len(db)
    if max_comments:
        comment_count = min(comment_count, max_comments)

    for index, comment_id in enumerate(db):
        body = db[comment_id]['body']
        c.train(body)
        set_line('Loaded %s/%s comments...' % (index + 1, comment_count))
        if max_comments and index + 1 >= max_comments:
            break

    set_line('')

    return c
Esempio n. 6
0
def build_chain(db, max_comments=None):
    c = Chain(tokenizer=markdown_tokenizer(), N=10)
    
    comment_count = len(db)
    if max_comments:
        comment_count = min(comment_count, max_comments)
    
    for index, comment_id in enumerate(db):
        body = db[comment_id]['body']
        c.train(body)
        set_line('Loaded %s/%s comments...' % (index+1, comment_count))
        if max_comments and index+1 >= max_comments:
            break
        
    set_line('')
        
    return c
Esempio n. 7
0
def test_save(tmp_file):
    c = Chain()
    c.grow(s1)
    c.grow(s2)

    c.save(tmp_file)
    c = load(tmp_file)
    
    for i in range(10):
        print('%s.' % ' '.join(c.generate()))
Esempio n. 8
0
class WordGen(object):
    def __init__(self):
        """Reads in phonetic data and creates a markov chain"""
        data = []
        for x in open('data/phonmap.txt').readlines():
            data.extend(x.split())

        #Build up a markov chain with it
        self.chain = Chain(data, 2)
    
    def make_word(self):
        """Constructs words"""
        fragments = self.chain.generate(random.randint(2, 6))
        return "".join(fragments)
Esempio n. 9
0
def test_grow():
    c = Chain()

    c.grow(s1)
    for w in s1:
        assert w in c.words
    assert len(c.words) == 3

    c.grow(s2)
    for w in s2:
        assert w in c.words
    assert len(c.words) == 5
Esempio n. 10
0
class ShaqFu(object):
    def __init__(self):
        
        data = open('training_data.txt').read()
        self.chain = Chain(tokens(data), 5)
    
    def generate_line(self, rhymes):
        words = self.chain.generate(100)
        lines = ' '.join([x for x in words]).splitlines(False)
        return random.choice(lines)
    
    def generate_chorus(self):
        return ['[CHORUS]'] + [self.generate_line(True) for x in xrange(5)] + ['\n']
    
    def generate_verse(self, min_length, max_length):
        length = random.randint(min_length, max_length)
        return ['[VERSE]'] + [self.generate_line(False) for x in xrange(length)] + ['\n']
        
    def generate_song(self):
        chorus = self.generate_chorus()
        verses = [self.generate_verse(5, 10) for x in xrange(3)]
        return verses[0] + chorus + verses[1] + chorus + verses[2]
Esempio n. 11
0
#!/usr/bin/env python3

from lxml import html
from bs4 import BeautifulSoup
import requests
from markov import Chain

simpsons_synopsis = Chain(2)

for year in range(1990, 2017):
    url_base = 'http://www.imdb.com/title/tt0096697/episodes?year='
    page = requests.get(url_base + str(year))
    soup = BeautifulSoup(page.content, "lxml")
    item_divs = soup.find_all("div", class_="item_description")
    descriptions = [
        div.string.strip() for div in item_divs if div.string is not None
    ]
    for desc in descriptions:
        simpsons_synopsis.add_passage(desc)

for i in range(10):
    print(' '.join(simpsons_synopsis.simulate()))
Esempio n. 12
0
# secret.json is: {
#   twitter: {
#     key, keysecret, token, tokensecret
#   }
# }
secret = json.load(open('secret.json'))['twitter']

auth = tweepy.OAuthHandler(secret['key'], secret['keysecret'])
auth.set_access_token(secret['token'], secret['tokensecret'])

twitter = tweepy.API(auth)

dedupe = json.load(open('dedupe.json')) or {}

try:
    chain = Chain(json.load(open('chains/trumpov_2.json')))
    post = None
    i = 0
    while (not post) or (post in dedupe):
        post = chain.gen(1000)
        i += 1
        if i >= 20:
            raise Exception('too many duplicates')
    twitter.update_status(html.unescape(post))
    dedupe[post] = True
    json.dump(dedupe, open('dedupe.json', 'w'))
    print('success')
except Exception as ex:
    print('failed')
    print(ex)
Esempio n. 13
0
#!coding:utf-8

from random import random
from graphviz import Digraph

from markov import Chain
from markov.plot import transitions_to_graph


def double_dice():
    return int(random() * 6) + int(random() * 6)


chain = Chain(11, double_dice())

# flip coin many times and build Markov chain for this process
# let 0 be heads and 1 tails
for i in range(1000000):
    chain.learn(double_dice())

g = Digraph(format='svg',
            engine='dot',
            graph_attr={
                'overlap': 'false',
                'pad': '0.1',
                'nodesep': '0.35',
                'ranksep': '0.35'
            },
            edge_attr={
                'fontname': 'Helvetica',
                'fontsize': '8.0'
Esempio n. 14
0
    "--token",
    type=str,
    default=get_token(),
    help="The Discord token for your bot.",
)
parser.add_argument(
    "-p",
    "--persist",
    type=Path,
    default=Path("model.json"),
    help="The file path for storing the model persistently.",
)

args = parser.parse_args()

chain = Chain(1)
if args.persist.exists():
    chain.deserialize(args.persist.read_text())
chain_lock = asyncio.Lock()

client = discord.Client()


@client.event
async def on_ready():
    print(f"Logged on as {client.user}.")


@client.event
async def on_message(message):
    async with chain_lock:
Esempio n. 15
0
def main(text_list, order):
    chain = Chain(order, text_list)
    # return chain.sample(10, 'rat-faced')
    # print(chain)
    return chain.sample(5)
Esempio n. 16
0
def main():
    chain = Chain(tokens(poem), 4)
    words = chain.generate(2000)
    print "".join([x for x in words])
Esempio n. 17
0
    def test_moving_stochastically_adds_new_state(self):
        chain = Chain(self.model)

        chain.move()

        self.assertEqual(len(chain.states), 1)
Esempio n. 18
0
 def __init__(self):
     
     data = open('training_data.txt').read()
     self.chain = Chain(tokens(data), 5)
Esempio n. 19
0
    def test_moving_to_specific_state_from_empty(self):
        chain = Chain(self.model)

        chain.move_to("a")

        self.assertEqual(chain.states, ["a"])
Esempio n. 20
0
#!coding:utf-8

from random import betavariate
from graphviz import Digraph

from markov import Chain
from markov.plot import transitions_to_graph


def rigged_dice():
    return int((betavariate(0.5, 0.7) * 6))

chain = Chain(6, rigged_dice())

# roll dice many times and build Markov chain for this process
for i in range(100000):
    chain.learn(rigged_dice())


def state_mapping(state):
    if state == 0: return u'⚀'
    if state == 1: return u'⚁'
    if state == 2: return u'⚂'
    if state == 3: return u'⚃'
    if state == 4: return u'⚄'
    if state == 5: return u'⚅'



g = Digraph(format='svg', engine='dot',
            graph_attr={'pad': '0.1', 'nodesep': '0.15', 'ranksep': '0.5'},