可能なキーのセットが小さい場合は、リストのリストを使用することもできます。これは、入力コーパスからの文字のペアを使用して新しい「文」を作成する、非常に単純なマルコフ生成器です。list-of-lists の初期化に注意してくださいmarkov_pairs
。これは、Python リストを使用して 2D 配列を取得する方法です。
import random
import string
all_letters = string.ascii_uppercase + string.ascii_lowercase
corpus = """Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum."""
# normalize all spaces to single spaces
corpus = ' '.join(corpus.split())
# initialize 2-D array for sequence tally
markov_pairs = [[0]*256 for i in range(256)]
# make sure every letter and space has at least *some* probability of following
# any other letter
for sublist in markov_pairs:
for i in range(len(sublist)):
if chr(i) in all_letters+' ':
sublist[i] += 1
# pairwise iterate over input corpus, updating markov pairs with observed pairs
it = iter(corpus)
last = next(it)
for c in it:
markov_pairs[ord(last)][ord(c)] += 10000
last = c
# function to guess a next character, given a starting character, based on the
# frequencies found in the input corpus
def getNext(pairs, from_):
probs = pairs[ord(from_)]
num = random.randint(0,sum(probs))
# reorder probs so highest weights are up front
for p,c in sorted(((p,chr(i)) for i,p in enumerate(probs) if p),reverse=True):
num -= p
if num <= 0: break
return c
# generate some new text based on the corpus
for i in range(20):
ret = []
last = random.choice("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
while last in (all_letters+' '):
ret.append(last)
last = getNext(markov_pairs, last)
print ''.join(ret)
版画 (ラテン語とトゥレット症候群の混合のようなもの):
NEx quite eria nalorunorein ulag Utalidor eniqum atet
SPllalalad nsiqudont intret
RCve ollat rim don epored lonidolid im cuatempt esepiaent furiutautr lit m vehenise vex nst
Wveta cet at mp icur e co anorepit pidomodor ng ala
Lorurim
Fm dorisseiuimondedor nid lunor
HJcupter Excarcuro don itaboffuitese cosensest emm cim e ct vonut s cosudodisir
Habocinim issit nia iat uininolla iatere e a aulict catenipa pont
Sgiteps a t cor
PRRfinsmabommo dupagnit norer olulaboi mipit
Exearicat ites e ngn ptrequmpa n uiuia imema cuause dont
Ex inod in amost met
Ae s cutes ia
CKx fint m e cor olllulalicun sedoreuipa ciuam
Oorolatrenor cum a is d Dum e edocipit m cimont eum lliofinsit aret am elorellisterexereminidit elolititresuntexelimomp poisudinisenorute a abolor ex ont
BZt ret taenorenat miqururcon Duit sea ca vexexeserurisulollat
Tlit aboceda qupriut m cualllupip cad d s dect
Ohet em uidid iam it d edollat a ssicaruir idor d unon n e e colaum funsiabontiataboressusia ffise Dum vororeruisedor redeit aupos cat epom ipt
GDust
Hyalorunofit d qur enser