Beispiel #1
0
    def test_update_chain_longer_prefix(self):
        prefix_len = 3
        word_dict = {
            "": ["It"],
            "It": ["is"],
            "It is": ["a"],
            "It is a": ["far,"],
            "is a far,": ["far", "far"],
            "a far, far": ["better", "better"],
            "far, far better": ["thing", "rest"],
            "far better thing": ["that"],
            "better thing that": ["I"],
            "thing that I": ["do,"],
            "that I do,": ["than"],
            "I do, than": ["I"],
            "do, than I": ["have"],
            "than I have": ["ever", "ever"],
            "I have ever": ["done;", "known."],
            "have ever done;": ["it"],
            "ever done; it": ["is"],
            "done; it is": ["a"],
            "it is a": ["far,"],
            "far better rest": ["that"],
            "better rest that": ["I"],
            "rest that I": ["go"],
            "that I go": ["to"],
            "I go to": ["than"],
            "go to than": ["I"],
            "to than I": ["have"]
        }

        chain = markov.MarkovChain(prefix_len)
        chain._update(self.long_text)
        self._compare_dictionaries(word_dict, chain._chain)
Beispiel #2
0
def main( size,  **args ):
    network = create_transition_graph( size )
    # Add interaction and return the matrix
    assert args['pUp'] > 0.0
    assert args['pDown'] > 0.0
    T = add_interaction( network
            , args['pUp']
            , args['pDown']
            , args.get('excitation', 0.0) 
            , args.get('inhibition', 0.0) 
            )
    print( "Running with parameters : %s" % args )
    if args.get('plot', False):
        transitionMatFile = 'transition_matrix_%d.csv' % size 
        np.savetxt( transitionMatFile, T )
        matImgFile = 'transition_mat_%04d.png' % size
        plt.figure()
        # create a binary image out of T.
        tImg = np.copy(T)
        tImg[ np.where( tImg != 0 ) ] = 1.0
        plt.figure( )
        plt.imshow( tImg, interpolation = 'none' )
        plt.title( 'Transition matrix. Zero and non-zero entries' )
        # plt.colorbar( )
        plt.savefig( matImgFile )
        plt.close( )
        dotFile = 'network_%d.dot' % size 
        nx.write_dot( network, dotFile )
        print( '[INFO] Graph is written to dot file %s' % dotFile )
        # subprocess.call( [ "neato", "-Tpng", dotFile,  "-O"  ] )

    # Once I have the transition matrix, I now use markov module to solve it.
    s = markov.MarkovChain( T )
    ss = s.find_steady_state( method = args['method'] )
    return get_effects( ss, size )
Beispiel #3
0
 async def cmd_profile(self, c, msg, replyto, params):
     chan = self.get_chan(replyto)
     params = params.strip() if params is not None else ''
     if len(params) == 0:
         chan.mc = None
         chan.mc_learning = False
         await c.send('PRIVMSG', replyto, rest='Chatting deactivated')
     elif params == 'learn':
         chan.mc = markov.MarkovChain()
         chan.mc_learning = True
         await c.send('PRIVMSG', replyto, rest='Now chatting and learning')
     else:
         path = '%s.sqlite' % params.lower()
         if os.path.exists(path):
             chan.mc = markov.MarkovChain(path)
             chan.mc_learning = False
             await c.send('PRIVMSG',
                          replyto,
                          rest='Now chatting like %s' % params)
Beispiel #4
0
 def test_add_chain_start(self):
     chain = markov.MarkovChain(2)
     chain._update("This is a sentence.")
     chain._add_chain_start("I have some text.")
     expected_dict = {
         "": ["This", "I"],
         "This": ["is"],
         "This is": ["a"],
         "is a": ["sentence."],
         "I": ["have"]
     }
     self._compare_dictionaries(expected_dict, chain._chain)
Beispiel #5
0
def create_app():
    app = flask.Flask(__name__)
    dotenv.load_dotenv()

    AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')
    AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY')
    S3_BUCKET = os.getenv('S3_BUCKET')

    if None not in(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, S3_BUCKET):
        print('Attempting to read from AWS')

        s3 = boto3.resource('s3')
        obj = s3.Object(S3_BUCKET, 'metro.txt')
        my_text = obj.get()['Body'].read().decode('utf-8')
    else:
        print('Attempting to read from local')

        with open('./texts/metro.txt') as file:
            my_text = file.read()

    words = markov.get_words(my_text)
    app.markov = markov.MarkovChain(words, order=3)
    app.rng = random.Random()

    @app.route("/")
    def root():
        return flask.render_template('app.html')

    @app.route("/api")
    def api():
        seed = flask.request.args.get('seed') or app.rng.randrange(sys.maxsize)
        seed = int(seed)

        random.seed(seed)

        return flask.jsonify({
            "seed": str(seed),
            "paragraph": app.markov.walk(5)
        })

    return app
Beispiel #6
0
    def trainText(self, source_text):
        self._markov_chain = markov.MarkovChain()
        self._source_text = source_text

        words = source_text.lower().split() + [None]
        length = len(words) - 1

        i = 0
        while i < length:
            a = words[i]
            b = words[i + 1]

            weight = self._markov_chain.getNode(a, b)

            if weight == None:
                weight = 0
            elif b == None:
                weight = 1

            self._markov_chain.setNode(a, b, weight + 1)

            i += 1
Beispiel #7
0
    fileListStr = input('Please enter one or more filenames: ')
    if not fileListStr:
        print('boi u deaf?? No file names found')
    else:
        break

filenames = fileListStr.strip().split(' ')
for name in filenames:
    print('Reading ' + name + '...')
    f = None
    try:
        f = open(name.strip())
    except FileNotFoundError:
        print('The file \'' + name + '\' does not exist, skipping')
        continue
    chain = markov.MarkovChain()
    for line in f:
        chain.munchData(line.strip().lower())
    f.close()
    chain.bakeProbabilities()
    chains.append(chain)
    print('... done!')

if len(chains) == 0:
    print(
        "No markov chains were generated. Did you specify valid text files? Exiting anyway, sorry"
    )
    exit()

helpString = '''Commands/controls are:
<enter> - pressing this generates a name with the last parameters/commands specified.
Beispiel #8
0
#!/usr/bin/env python
import sys
import markov
import string

FILES = sys.argv[1:]
if not FILES:
    FILES = ["/dev/stdin"]

MC = markov.MarkovChain("./traineddb")

for fi in FILES:
    f = open(fi)

    for line in f:
        words = line.split()
        # we have to handle charrak's replies specially (we want to ignore them)
        if words[0] == "charrak":
            empty_reply_index = line.find("EMPTY REPLY")
            if empty_reply_index > -1:
                line = line[empty_reply_index + 11:]
            else:
                cr_index = line.find("\r")
                line = line[cr_index + 1:]

            line = filter(string.printable.__contains__, line)
            words = line.split()

        if len(words) > 3:
            words = line.split()
            if words[3] == "ACTION":
Beispiel #9
0
__email__ = "*****@*****.**"
__status__ = "Development"

import sys
import os
import matplotlib.pyplot as plt
import numpy as np
import markov

ss0, ss1, ss2 = [], [], []
xvec = []
for i in range(20):
    delP = 0.04 * i
    xvec.append(delP)
    expr = "0.09 0.09 0;0.09 0 (0.1+{0})*0.9;0.09 0 (0.1+{0})*0.9;0 0.09 0.09".format(
        delP)
    mc = markov.MarkovChain(expr)
    state = mc.find_steady_state()
    s0, s1, s2 = state[0], sum(state[1:3]), state[3]
    ss0.append(s0)
    ss1.append(s1)
    ss2.append(s2)

plt.plot(xvec, ss0, label='0')
plt.plot(xvec, ss1, label='1')
plt.plot(xvec, ss2, label='2')
plt.title('Two interacting bistable with pUp = %s, pDown = %s' % (0.1, 0.1))
plt.xlabel('delta P (interaction)')
plt.legend()
plt.savefig('%s.png' % sys.argv[0])
Beispiel #10
0
#!/usr/bin/env python
import sys
import re
import markov
import random
import string

db_file = sys.argv[1]
if not db_file:
    db_file = "traineddb"

mc = markov.MarkovChain(db_file)

while True:
    text = raw_input(": ")
    text = string.strip(text, ",./?><;:[]{}\'\"!@#$%^&*()_-+=")
    words = text.split()

    if len(words) < 2:
        continue

    # Use a random bigram of the input message as a seed for the Markov chain
    max_index = min(6, len(words) - 1)
    index = random.randint(1, max_index)
    seed = [words[index - 1], words[index]]
    leading_words = string.join(words[0:index + 1])

    words = leading_words.split()
    seed = [words[-2], words[-1]]

    print ">>", seed
Beispiel #11
0
 def loadMarkovChain(self):
     # Open our Markov chain database
     self.mc = markov.MarkovChain(self.MARKOVDB)
Beispiel #12
0
    import cPickle as pickle
except ImportError:
    import pickle

#

ARGPARSER = argparse.ArgumentParser(description='Convert databases.')
ARGPARSER.add_argument('--in', dest='db_in', default='./oldcharrakdb')
ARGPARSER.add_argument('--out', dest='db_out', default='./newcharrakdb')
ARGS = ARGPARSER.parse_args()

#

DB_IN = str(ARGS.db_in)
DB_OUT = str(ARGS.db_out)

try:
    with open(DB_IN, 'rb') as dbfile:
        DB = pickle.load(dbfile)
except IOError:
    logging.error('Unable to read database file "%s"', DB_IN)
    sys.exit(1)
except ValueError:
    logging.error('Database "%s" corrupt or unreadable', DB_IN)
    sys.exit(2)

MC = markov.MarkovChain(DB_OUT)
MC.db = DB[0]
MC.saveDatabase()

Beispiel #13
0
# Book templates are tuples
# index 0 = file path
# index 1 = another tuple, indicating the area where the icon is applied
#           first two are the top left corner, second two are dimensions
TEMPLATES = [
    ("book.png", (2, 1, 4, 6)),
    ("borderedbook.png", (2, 1, 4, 6)),
]
MODIFIERS = [
    None,
    "bookmark.png",
    "tabs.png",
]

with open("martial.txt") as text:
    MARKOV = markov.MarkovChain(text.read())


class spellbook(object):
    def __init__(self,
                 templates=TEMPLATES,
                 modifiers=MODIFIERS,
                 markov_chain=MARKOV):
        self.template = templates[random.randrange(len(templates))]
        self.modifier = modifiers[random.randrange(len(modifiers))]
        self.markov = markov_chain
        self.generate()
        self.generate_title()

    def show(self, *args, **kwargs):
        if kwargs.has_key("size"):
Beispiel #14
0
#!/usr/bin/python
import markov
from colortext import ENDC

mc = markov.MarkovChain('new.db')
print ENDC

mc.addLine('The big dog likes ham')

for key in mc.db.keys():
    print key, mc.db[key]
print

mc.addLine('The big dog likes scratches')
mc.addLine('The big dog likes scratches')
mc.addLine('The big dog likes scratches')

for key in mc.db.keys():
    print key, mc.db[key]
    resp = mc.db[key]
    for rr in resp:
        print rr[0]

seed = ('the', 'big')
print

for ii in range(0, 10):
    print seed[0], seed[1], mc.respond(seed)
Beispiel #15
0
 def test_prefix_too_short(self):
     with self.assertRaises(ValueError):
         markov.MarkovChain(0)
Beispiel #16
0
 def test_add_sentences(self):
     chain = markov.MarkovChain(2)
     chain._add_sentences(self.factory_text)
     self._compare_dictionaries(self.expected_factory_dict, chain._chain)
Beispiel #17
0
def create_duration(d):
    switcher = {
        "whole": "1",
        "half": "2",
        "quarter": "4",
        "eighth": "8",
        "16th": "16",
        "32th": "32"
    }
    return switcher.get(d, None)


with open(file_name, 'r') as data_file:
    reader = data_file.read().replace("\n", " ").replace("\t", " ").replace("  ", " ").split(' ')
    data = list(reader)
mc = markov.MarkovChain()

mc.load_twins(data, '.')
occurrences = list()

for key in mc.words:
    for c in mc.words[key].Children:
        occurrences.append('{:31}'.format(key) + '{:^10}'.format("-->") + '{:18}'.format(c) + '{:4}'.format(str(mc.words[key].Children[c].Occurrence)) + \
            '{percent:03.2%}'.format(percent=(float(mc.words[key].Children[c].Occurrence) / mc.words[key].ChildCount)))

print occurrences

output = mc.output()
score = ""
score += "4/4 "
for c in output.splitlines():
                       dest='beforedate',
                       default=datetime.date.max)
ARGPARSER.add_argument('--after', dest='afterdate', default=datetime.date.min)
ARGPARSER.add_argument('--db', dest='db', default='./traineddb')
ARGPARSER.add_argument('--nick', dest='nick', default='charrak')
ARGS = ARGPARSER.parse_args()

#

BEFORE_DATE = str(ARGS.beforedate)
AFTER_DATE = str(ARGS.afterdate)

BEFORE = dateutil.parser.parse(BEFORE_DATE)
AFTER = dateutil.parser.parse(AFTER_DATE)

MC = markov.MarkovChain(str(ARGS.db))

for line in ARGS.files:
    time_text = line.split(':')
    if len(time_text) < 3:
        continue

    timestamp = (time_text[0] + ":" + time_text[1] + ":" +
                 time_text[2].split(',')[0])
    cur_time = dateutil.parser.parse(timestamp)

    if cur_time > BEFORE or cur_time < AFTER:
        continue

    words = line.split(':')