サムネがコーヒーの記事は書きかけです。

英語のマルコフ連鎖を配列で実装

最近ハマっているマルコフ過程について、形態素解析がほとんど不要な英語について実装してみます。

import random 
def markov_2th(src, length) -> str:
    words_count = {} 
    words = [i.lower() for i in open(src).read().split()]
    for i in words:
        if i not in words_count:
            words_count[i] = words.count(i)
    w_adj = {i:[] for i in words}
    for i in [[words[i],words[i+1]] for i in range(len(words)-1)]:
        w_adj[i[0]].append(i[1])
    sentence = [random.choice(words)]
    i = 0
    while True:
        try:
            sentence.append(random.choice(w_adj[sentence[i]]))
        except IndexError:
            return " ".join(sentence)
        i += 1 
        if i >= length :    
            return " ".join(sentence)

辞書型変数を使えばキーを利用して前後の関係を格納しておくことができます。

試しに以下の文章を使ってテストしてみます。

Text
So shaken as we are, so wan with care,
Find we a time for frighted peace to pant,
And breathe short-winded accents of new broils
To be commenced in strands afar remote.
No more the thirsty entrance of this soil
Shall daub her lips with her own children’s blood;
Nor more shall trenching war channel her fields,
Nor bruise her flowerets with the armed hoofs
Of hostile paces: those opposed eyes,
Which, like the meteors of a troubled heaven,
All of one nature, of one substance bred,
Did lately meet in the intestine shock
And furious close of civil butchery
Shall now, in mutual well-beseeming ranks,
March all one way and be no more opposed
Against acquaintance, kindred and allies:
The edge of war, like an ill-sheathed knife,
No more shall cut his master. Therefore, friends,
As far as to the sepulchre of Christ,
Whose soldier now, under whose blessed cross
We are impressed and engaged to fight,
Forthwith a power of English shall we levy;
Whose arms were moulded in their mothers’ womb
To chase these pagans in those holy fields
Over whose acres walk’d those blessed feet
Which fourteen hundred years ago were nail’d
For our advantage on the bitter cross.
But this our purpose now is twelve month old,
And bootless ‘tis to tell you we will go:
Therefore we meet not now. Then let me hear
Of you, my gentle cousin Westmoreland,
What yesternight our council did decree
In forwarding this dear expedience.

実行結果

mothers' womb to chase these pagans in those holy fields over whose soldier now, under whose soldier now, in mutual well-beseeming ranks, march all of one way and engaged to the intestine shock and breathe short-winded accents of english shall cut his master. therefore, friends, as to chase these pagans in strands afar remote. no more opposed against acquaintance, kindred and breathe short-winded accents of christ, whose acres walk'd those holy fields over whose acres walk'd those blessed feet which fourteen hundred years ago were moulded in mutual well-beseeming ranks, march
her fields, nor more opposed eyes, which, like the meteors of you, my gentle cousin westmoreland, what yesternight our council did lately meet not now. then let me hear of one substance bred, did decree in the bitter cross. but this soil shall cut his master. therefore, friends, as we a troubled heaven, all one substance bred, did lately meet in those holy fields over whose arms were moulded in forwarding this dear expedience.
did decree in their mothers' womb to pant, and furious close of new broils to fight, forthwith a troubled heaven, all of one nature, of this dear expedience.
no more shall now, under whose blessed feet which fourteen hundred years ago were nail'd for frighted peace to chase these pagans in the armed hoofs of civil butchery shall we a power of new broils to the sepulchre of english shall trenching war channel her lips with the edge of one way and allies: the edge of one substance bred, did lately meet in their mothers' womb to be no more opposed eyes, which, like an ill-sheathed knife, no more the meteors of a power of this soil shall now,
then let me hear of one substance bred, did lately meet not now. then let me hear of this our council did decree in mutual well-beseeming ranks, march all of you, my gentle cousin westmoreland, what yesternight our purpose now is twelve month old, and engaged to the bitter cross. but this dear expedience.
over whose arms were nail'd for frighted peace to chase these pagans in those blessed cross we a troubled heaven, all of war, like the bitter cross. but this our advantage on the edge of christ, whose soldier now, under whose arms were moulded in mutual well-beseeming ranks, march all one way and be no more opposed against acquaintance, kindred and be commenced in those blessed cross we are, so wan with her lips with her fields, nor bruise her lips with care, find we meet in their mothers' womb to
soil shall we are, so wan with care, find we levy; whose arms were moulded in those opposed eyes, which, like the thirsty entrance of christ, whose soldier now, under whose arms were nail'd for our advantage on the intestine shock and furious close of one substance bred, did decree in strands afar remote. no more opposed against acquaintance, kindred and breathe short-winded accents of this our advantage on the armed hoofs of a troubled heaven, all one nature, of hostile paces: those holy fields over whose acres walk'd those blessed
those blessed cross we a troubled heaven, all one nature, of war, like the armed hoofs of war, like the edge of you, my gentle cousin westmoreland, what yesternight our council did lately meet in forwarding this dear expedience.
cross we meet in strands afar remote. no more the armed hoofs of new broils to the edge of christ, whose arms were moulded in mutual well-beseeming ranks, march all one way and breathe short-winded accents of war, like the thirsty entrance of this soil shall daub her flowerets with care, find we a troubled heaven, all one nature, of hostile paces: those opposed against acquaintance, kindred and furious close of one way and engaged to the bitter cross. but this soil shall daub her lips with care, find we will
forwarding this dear expedience.
you we are, so shaken as far as we a time for our advantage on the meteors of christ, whose soldier now, under whose arms were moulded in those holy fields over whose blessed cross we meet in forwarding this dear expedience.
an ill-sheathed knife, no more opposed eyes, which, like an ill-sheathed knife, no more the thirsty entrance of english shall we are, so shaken as to pant, and furious close of one substance bred, did decree in strands afar remote. no more the thirsty entrance of english shall cut his master. therefore, friends, as far as far as we meet not now. then let me hear of one way and be commenced in the intestine shock and allies: the edge of a time for frighted peace to pant, and furious close
pagans in mutual well-beseeming ranks, march all of christ, whose blessed feet which fourteen hundred years ago were nail'd for our purpose now is twelve month old, and engaged to chase these pagans in the edge of you, my gentle cousin westmoreland, what yesternight our purpose now is twelve month old, and be commenced in those holy fields over whose acres walk'd those holy fields over whose acres walk'd those holy fields over whose acres walk'd those holy fields over whose soldier now, in strands afar remote. no more opposed against

生成源の規模が小さいため、あまり変わっていないように見えます。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です