In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces how to use Python to generate pseudo-random text based on Markov chain, the content is very detailed, interested friends can refer to, hope to be helpful to you.
First, take a look at the definition from Wolfram.
Markov chain is a set of random variables {Xantht}. Given the current state, the future is independent of the past conditions.
The definition of Wolfram is a little clearer
... Markov chain is a stochastic process with Markov property. [this means] the state change is probabilistic, and the future state depends only on the current state.
The Markov chain has many uses, and now let me see how to use it to produce decent-looking gibberish.
The algorithm is as follows
Find a text as a corpus, which is used to select the next transformation.
Start with two consecutive words in the text, and the last two words form the current state.
The process of generating the next word is Markov transformation. To generate the next word, first look at the corpus to find the words that follow these two words. Choose one of them at random.
Repeat 2 until the generated text reaches the desired size.
The code is as follows:
To see an example result, we took Wodehous's "My man jeeves" from the Gutenberg plan as text, and the example result is as follows.
What about the Markov algorithm?
The last two words are in the current state.
The next word depends only on the last two words, that is, the current state.
The next words are randomly selected from the statistical model of the corpus.
This is a sample text.
The corpus corresponding to this text is like this.
Now if we start with "brown fox", the next word can be "jumps" or "who". If we choose "jumps", then the current state becomes "fox jumps", then the next word is "over", and so on.
Prompt
The larger the text we choose, the more choices we have for each conversion, and the better the resulting text.
The status can be set to rely on one word, two words, or any number of words. As the number of words in each state increases, the generated text is less random.
Don't remove punctuation marks and so on. They will make the corpus more representative and the random text more beautiful.
On how to use Python to generate pseudo-random text based on Markov chain to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.