[Markov chain] I tried to read a quote into Python.

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.

Program preparation

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.

Contents of Text.txt

Text.txt is the original file of Markov. This time, it's a saying, so I'll make it.

Collect quotes

This time, we have collected quotes from https://iyashitour.com/meigen/theme/life. I will not put it for the time being.

Execution result

Run it first.

python Markov.full path of py

After a while, you will get results. What kind of sentence is it?

First time

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.

Second time

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.

third time

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 ...

Summary

Quotations can only be said because I have lived my life, and I can't imitate them.

Recommended Posts

[Markov chain] I tried to read a quote into Python.
[Markov chain] I tried to read negative emotions into Python.
[Markov chain] I tried to read quotes and negative emotions into Python.
I tried to automatically create a report with Markov chain
I tried to draw a route map with Python
I tried to implement a pseudo pachislot in Python
I tried to automatically generate a password with Python3
I tried to touch Python (installation)
I tried to implement a one-dimensional cellular automaton in Python
I tried "How to get a method decorated in Python"
I tried to make a stopwatch using tkinter in python
I tried to divide the file into folders with Python
[5th] I tried to make a certain authenticator-like tool with python
I made a library to easily read config files with Python
I tried to convert a Python file to EXE (Recursion error supported)
[2nd] I tried to make a certain authenticator-like tool with python
I tried to make a regular expression of "amount" using Python
[Python] I tried to implement stable sorting, so make a note
I tried to make a regular expression of "time" using Python
I tried to implement anomaly detection using a hidden Markov model
[3rd] I tried to make a certain authenticator-like tool with python
[Python] A memo that I tried to get started with asyncio
I tried to create a list of prime numbers with python
I tried to make a regular expression of "date" using Python
I tried to implement a misunderstood prisoner's dilemma game in Python
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
I tried to make a strange quote for Jojo with LSTM
I tried to create a linebot (implementation)
I tried to summarize Python exception handling
I tried to implement PLSA in Python
I tried to implement permutation in Python
Python3 standard input I tried to summarize
I want to build a Python environment
I tried to implement ADALINE in Python
I tried a functional language with Python
I tried to implement PPO in Python
I tried to make a Web API
[Python] I tried to calculate TF-IDF steadily
I tried to touch Python (basic syntax)
Python: I tried to make a flat / flat_map just right with a generator
I tried to communicate with a remote server by Socket communication with Python.
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
Mayungo's Python Learning Episode 6: I tried to convert a character string to a number
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I tried to develop a Formatter that outputs Python logs in JSON
I tried to discriminate a 6-digit number with a number discrimination application made with python
[Outlook] I tried to automatically create a daily report email with Python
I tried to build a Mac Python development environment with pythonz + direnv
I tried to create a sample to access Salesforce using Python and Bottle
I tried to implement a card game of playing cards in Python
I tried to build a super-resolution method / ESPCN
How to read a CSV file with Python 2/3
Markov Chain Chatbot with Python + Janome (1) Introduction to Janome
I want to INSERT a DataFrame into MSSQL
I tried to build a super-resolution method / SRCNN ①
I want to create a window in Python
I tried playing a typing game in Python