- Machine Learning for Cybersecurity Cookbook
- Emmanuel Tsukerman
- 344字
- 2021-06-24 12:28:57
How to do it...
Let's see how to generate text using Markov chains by performing the following steps:
- Start by importing the markovify library and a text file whose style we would like to imitate:
import markovify
import pandas as pd
df = pd.read_csv("airport_reviews.csv")
As an illustration, I have chosen a collection of airport reviews as my text:
"The airport is certainly tiny! ..."
- Next, join the individual reviews into one large text string and build a Markov chain model using the airport review text:
from itertools import chain
N = 100
review_subset = df["content"][0:N]
text = "".join(chain.from_iterable(review_subset))
markov_chain_model = markovify.Text(text)
Behind the scenes, the library computes the transition word probabilities from the text.
- Generate five sentences using the Markov chain model:
for i in range(5):
print(markov_chain_model.make_sentence())
- Since we are using airport reviews, we will have the following as the output after executing the previous code:
Surprisingly realistic! Although the reviews would have to be filtered down to the best ones.
- Generate 3 sentences with a length of no more than 140 characters:
for i in range(3):
print(markov_chain_model.make_short_sentence(140))
With our running example, we will see the following output:
推薦閱讀
- 電力自動(dòng)化實(shí)用技術(shù)問(wèn)答
- SCRATCH與機(jī)器人
- Managing Mission:Critical Domains and DNS
- Getting Started with Clickteam Fusion
- CSS全程指南
- 精通特征工程
- 水晶石精粹:3ds max & ZBrush三維數(shù)字靜幀藝術(shù)
- Ceph:Designing and Implementing Scalable Storage Systems
- Machine Learning with the Elastic Stack
- 電腦日常使用與維護(hù)322問(wèn)
- Linux內(nèi)核精析
- Visual Studio 2010 (C#) Windows數(shù)據(jù)庫(kù)項(xiàng)目開(kāi)發(fā)
- 數(shù)據(jù)要素:全球經(jīng)濟(jì)社會(huì)發(fā)展的新動(dòng)力
- 筆記本電腦使用與維護(hù)
- FreeCAD [How-to]