Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

allow to be used with python2.7 #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion markov.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self, n=3):
def train(self, training_data):
prev = ()
for token in training_data:
token = sys.intern(token)
#token = sys.intern(token)
for pprev in [prev[i:] for i in range(len(prev) + 1)]:
if not pprev in self.data:
self.data[pprev] = [0, {}]
Expand Down Expand Up @@ -75,6 +75,9 @@ def __next__(self):
self.prev = self.prev[1:]

return next

def next(self):
return self.__next__()

def _choose(self, freqdict):
total, choices = freqdict
Expand Down
1 change: 0 additions & 1 deletion markovstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ def more(self, chunks=1):
def train(self, n, stream, noparagraphs=False):
"""Train a new markov chain, overwriting the existing one.
"""

training_data = tokenise.Tokeniser(stream=stream,
noparagraphs=noparagraphs)

Expand Down
18 changes: 10 additions & 8 deletions repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ def wrapper(self, line):
return wrapper


class Repl(cmd.Cmd):
class Repl(object,cmd.Cmd):
"""REPL for Markov interaction. This is way overkill, yay!
"""

def __init__(self):
"""Initialise a new REPL.
"""

super().__init__()
self.completekey = False
self.cmdqueue = []
super(Repl, self).__init__()
self.markov = markovstate.MarkovState()

def help_generators(self):
Expand Down Expand Up @@ -144,11 +145,12 @@ def do_train(self, args):
for path in glob.glob(os.path.expanduser(ps))]

def charinput(paths):
with fileinput.input(paths) as fi:
for line in fi:
for char in line:
yield char

fi = fileinput.input(paths)
for line in fi:
for char in line:
yield char
fi.close()

self.markov.train(args["<n>"],
charinput(paths),
noparagraphs=args["--noparagraphs"])
Expand Down
5 changes: 4 additions & 1 deletion tokenise.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __next__(self):
# after this point.
try:
next_char = next(self.stream)
except:
except Exception:
next_char = ''
self.halt = True
if not self.buffer:
Expand Down Expand Up @@ -70,3 +70,6 @@ def __next__(self):

# If we're here, we got nothing but EOF.
raise StopIteration

def next(self):
return self.__next__()