Hello. This is Pyloc. This time, I would like to see what kind of wise sayings can be made by using the original data of the Markov chain program using Pyhon and Janome as wise sayings.
The program is [here](http://zipmonkey.wp.xdomain.jp/2020/09/18/%e3%80%90%e3%82%b3%e3%83%94%e3%83%9a%e3 % 81% a7ok% e3% 80% 91-python% e3% 81% a7% e3% 83% 9e% e3% 83% ab% e3% 82% b3% e3% 83% 95% e9% 80% a3% e9 Take from% 8e% 96% e3% 82% 92% e3% 82% 84% e3% 81% a3% e3% 81% a6% e3% 81% bf% e3% 82% 8b /). I will post it here as well.
Markov.py
# -*- coding: utf-8 -*-
import random
from janome.tokenizer import Tokenizer
#Split text data into words using Janome
def wakati(text):
text = text.replace('\n','') #Remove line breaks
text = text.replace('\r','') #Remove space
t = Tokenizer()
result =t.tokenize(text, wakati=True)
return result
#The default number of sentences is 5
def generate_text(num_sentence=5):
src = open(r"Text.Full path of txt", "r", encoding="utf-8").read() #Text.Use the absolute path of txt(If you follow the procedure, C:\Users\username\Desktop\Markov\Text.txt )
wordlist = wakati(src)
#Create a table for Markov chains
markov = {}
w1 = ""
w2 = ""
for word in wordlist:
if w1 and w2:
if (w1, w2) not in markov:
markov[(w1, w2)] = []
markov[(w1, w2)].append(word)
w1, w2 = w2, word
#Automatic sentence generation
count_kuten = 0 #Number of Kuten "."
num_sentence= num_sentence
sentence = ""
w1, w2 = random.choice(list(markov.keys()))
while count_kuten < num_sentence:
tmp = random.choice(markov[(w1, w2)])
sentence += tmp
if(tmp=='。'):
count_kuten += 1
sentence += '\n' #Line breaks for each sentence
w1, w2 = w2, tmp
print(sentence)
if __name__ == "__main__":
generate_text()
Put this in the Markov folder you created on your desktop. Then, create a file called Text.txt in the Markov folder. Change the full path of Text.txt on the 16th line to suit your environment.
Text.txt is the original file of Markov. This time, it's a saying, so I'll make it.
This time, we have collected quotes from https://iyashitour.com/meigen/theme/life. I will not put it for the time being.
Run it first.
python Markov.full path of py
After a while, you will get results. What kind of sentence is it?
Let's start with three pages of text.
There is an aim.
How do you live?
Pretend not to fall and act smartly.
When you are confident in your life, you have to look back on it with a miserable feeling.
Life is the score of break time.
It's kind of like a name, but it's a little unnatural.
Next, I will try with five pages of sentences.
Never think you made it.
That is the greatest feat.
There is that fushi.
Happiness love life.
People in their sixties who do not take the life they love seriously and are abandoned are afraid of freedom.
I feel that my personal computer is getting older than mine.
At the end, all (15) pages are loaded.
I find that I am saying "the source of suffering" in reverse.
People aren't just saddened, they're just trying to do what they can to grow a little more than they did yesterday.
You don't compete with people.
Finding things that are dangerous enough to walk for the sake of others as if they were living thoroughly.
Even if you have money, it comes from thinking awkwardly about things about yourself.
Hmm. After all, it seems that you need life and more data to be like a great man ...
Quotations can only be said because I have lived my life, and I can't imitate them.
Recommended Posts