- 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:
推薦閱讀
- Dreamweaver CS3+Flash CS3+Fireworks CS3創意網站構建實例詳解
- 大數據管理系統
- Word 2000、Excel 2000、PowerPoint 2000上機指導與練習
- 大數據挑戰與NoSQL數據庫技術
- VB語言程序設計
- 網絡綜合布線設計與施工技術
- 項目管理成功利器Project 2007全程解析
- Nginx高性能Web服務器詳解
- Bayesian Analysis with Python
- Mastering Exploratory Analysis with pandas
- 運動控制系統(第2版)
- Machine Learning with Spark(Second Edition)
- 傳感技術基礎與技能實訓
- 工業機器人技術
- 實戰大數據(Hadoop+Spark+Flink):從平臺構建到交互式數據分析(離線/實時)