- 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:
推薦閱讀
- Oracle SOA Governance 11g Implementation
- 大數據項目管理:從規劃到實現
- Deep Learning Quick Reference
- 輕松學Java
- 計算機控制技術
- 圖解PLC控制系統梯形圖和語句表
- 大數據時代的數據挖掘
- STM32嵌入式微控制器快速上手
- Photoshop CS3圖層、通道、蒙版深度剖析寶典
- 計算機網絡安全
- Python:Data Analytics and Visualization
- 空間機械臂建模、規劃與控制
- 人工智能:語言智能處理
- Dreamweaver+Photoshop+Flash+Fireworks網站建設與網頁設計完全實用
- Linux Shell Scripting Cookbook(Third Edition)