Skip to content

Instantly share code, notes, and snippets.

@waffle2k
Last active December 18, 2015 18:49
Show Gist options
  • Select an option

  • Save waffle2k/5828633 to your computer and use it in GitHub Desktop.

Select an option

Save waffle2k/5828633 to your computer and use it in GitHub Desktop.
Simple Markov Chain programme and helper utilities
#!/usr/bin/env python
import sys
import random
class Markov:
def __init__(self):
self.h = {}
def add( self, x, y ):
if x not in self.h:
self.h[x] = {}
if y not in self.h[x]:
self.h[x][y] = 0
self.h[x][y] = self.h[x][y] + 1
def next( self, x ):
# Calculate which is the next word
total = 0
for k in self.h[x]:
total = total + self.h[x][k]
r = random.randint( 0, total -1 )
i = 0
for k in self.h[x]:
i = i + self.h[x][k]
if r < i:
return k
if __name__ == '__main__':
markov = Markov()
for line in sys.stdin.readlines():
line = line.rstrip("\n")
( x, y ) = line.split()
markov.add( x, y )
#print markov.h
i = 0
n = "the"
if len(sys.argv) > 1:
n = sys.argv[1]
while i < 100:
n = markov.next( n )
print n,
i = i + 1
#!/bin/bash
SOURCE=$1
if [ -z $SOURCE ]; then
# use the king james bible
SOURCE="http://www.gutenberg.org/ebooks/10.txt.utf-8"
fi
FN=$(echo $SOURCE | awk -F\/ '{print $NF}')
if [ ! -f $FN ]; then
echo $SOURCE | egrep -q -e '^http://' && wget $SOURCE
fi
if [ ! -f /tmp/markov.list ]; then
./split.pl $FN > /tmp/markov.list
fi
./markov.py < /tmp/markov.list
#!/usr/bin/perl -s
use strict;
local $/;
my $text = <>;
chomp $text;
my @a = split /\s+/ => $text;
while( scalar @a > 1 ){
my $x = shift @a;
print "$x $a[0]\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment