我正在使用此代码生成随机文本:
from collections import defaultdict, Counter
from itertools import ifilter
from random import choice, randrange
def pairwise(iterable):
it = iter(iterable)
last = next(it)
for curr in it:
yield last, curr
last = curr
valid = set('abcdefghijklmnopqrstuvwxyz ')
def valid_pair((last, curr)):
return last in valid and curr in valid
def make_markov(text):
markov = defaultdict(Counter)
lowercased = (c.lower() for c in text)
for p, q in ifilter(valid_pair, pairwise(lowercased)):
markov[p][q] += 1
return markov
def genrandom(model, n):
curr = choice(list(model))
for i in xrange(n):
yield curr
if curr not in model: # handle case where there is no known successor
curr = choice(list(model)) #i.e. letter appears for first time at end of text
d = model[curr]
target = randrange(sum(d.values()))
cumulative = 0
for curr, cnt in d.items():
cumulative += cnt
if cumulative > target:
break
model = make_markov(**'the fox jumped over the fence'**)
print ''.join(genrandom(model, 280))
但是,我想使用txt文件作为输入而不是'狐狸跳过栅栏' 我一直在尝试:
text=open('moby.txt','r+').read()
然后:
model = make_markov(text)
但似乎我有些不对劲,我不知道如何处理这个问题。
答案 0 :(得分:2)
打开文件,读入,使用它。我会使用with
语句来保证文件在之后关闭。
with open(my_file, 'r') as f:
text = f.read()
model = make_markov(text)
对于快速而脏的代码,您可以一次完成所有操作。
model = make_markov(open(my_file, 'r').read())
答案 1 :(得分:1)
您的参数text
应该是Moby Dick的全部内容,以字符串形式传入?尝试传递文本文件的位置并将其用作句柄,以便从read()
Def makemarcov(fileloc):
with open(fileloc, 'r') as f:
text = f.read()
#proceed...
我在手机上,否则ID还在open()
条款中包含try
语句。
with
关键字(如果我没记错),使用幕后的yield
语句,使用生成器打开文件。它使用较少的资源来读取大文件。
答案 2 :(得分:1)
我尝试过它有效......
Aux = open("c:/Codes/A.txt",'r')
mystring = []
for i in Aux:
mystring += i
model = make_markov(mystring)
输出不是“那个”清楚;-)
(你读过乔姆斯基关于马尔可夫语法的文章吗?)
但可能你并没有真正处理没有已知继任者的情况!