python自动写文章_Python实现文章自动生成

下面的Python程序实现了通过从网页抓取一篇文章,然后根据这篇文章来生成新的文章,这其中的原理就是基于概率统计的文本分析。

过程大概就是网页抓取数据->统计分析->生成新文章。网页抓取数据是通过BeautifulSoup库来抓取网页上的文本内容。统计分析这个首先需要使用ngram模型来把文章进行分词并统计频率。因为文章生成主要依据马尔可夫模型,所以使用了2-gram,这样可以统计出一个单词出现在另一个单词后的概率。生成新文章是基于分析大量随机事件的马尔可夫模型。随机事件的特点是在一个离散事件发生之后,另一个离散事件将在前一个事件的条件下以一定的概率发生。

from urllib.request import urlopen

from random import randint

from bs4 import BeautifulSoup

import re

def wordListSum(wordList):

sum = 0

for word, value in wordList.items():

sum = sum + value

return sum

def retrieveRandomWord(wordList):

randomIndex = randint(1, wordListSum(wordList))

for word, value in wordList.items():

randomIndex -= value

if randomIndex <= 0:

return word

def buildWordDict(text):

text = re.sub('(\n|\r|\t)+', " ", text)

text = re.sub('\"', "", text)

punctuation = [',', '.', ';', ':']

for symbol in punctuation:

text = text.replace(symbol, " " + symbol + " ")

words = text.split(' ')

words = [word for word in words if word != ""]

wordDict = {}

for i in range(1, len(words)):

if words[i-1] not in wordDict:

wordDict[words[i-1]] = {}

if words[i] not in wordDict[words[i-1]]:

wordDict[words[i-1]][words[i]] = 0

wordDict[words[i-1]][words[i]] = wordDict[words[i-1]][words[i]] + 1

return wordDict

def randomFirstWord(wordDict):

randomIndex = randint(0, len(wordDict))

return list(wordDict.keys())[randomIndex]

bsObj = BeautifulSoup(html, "lxml")

ps = bsObj.find("div", {"id": "cmtdiv3523349"}).find_next_siblings("p");

content = ""

for p in ps:

content = content + p.get_text()

text = bytes(content, "UTF-8")

text = text.decode("ascii", "ignore")

wordDict = buildWordDict(text)

length = 100

chain = ""

currentWord = randomFirstWord(wordDict)

for i in range(0, length):

chain += currentWord + " "

currentWord = retrieveRandomWord(wordDict[currentWord])

print(chain)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

buildWordDict(text)函数接收文本内容,生成的内容如下

{‘itself’: {‘,’: 1}, ‘night’: {‘sky’: 1}, ‘You’: {‘came’: 1, ‘will’: 1}, ‘railways’: {‘all’: 1}, ‘government’: {‘while’: 1, ‘,’: 1, ‘is’: 1}, ‘you’: {‘now’: 1, ‘open’: 1, ‘down’: 1, ‘with’: 1, ‘.’: 6, ‘,’: 1, ‘that’: 1},

主要就是生成一个字典,键是文章中所有出现的词语,值其实也是一个字典,这个字典是所有直接出现在键后边的词语及其出现的频率。这个函数就是ngram模型思想的运用。

retrieveRandomWord(wordList)函数的wordList代表的是出现在上一个词语后的词语列表及其频率组成的字典,然后根据统计的概率随机生成一个词。这个函数是马尔可夫模型的思想运用。

然后运行这个程序会生成一个长度为100的文章,如下面所示

fail . We will stir ourselves , but we will never before . Do not share one heart and pleasant it back our jobs . We are infused with the orderly and railways all of the gangs and robbed our jobs for their success will determine the civilized world . We will their success will be a great men and highways and millions to all bleed the world . It belongs to great national effort to defend our products , constantly complaining , D . We will be ignored again . It belongs to harness the expense of America .

生成的文章看起来语法混乱,这也难怪,因为只是抓取分析统计了一篇的文章。我想如果可以抓取足够多的英文文章,数据集足够大那么语法准确度会大大提高。python高级用法_iOS-创客学院​www.makeru.com.cnv2-6c43fc8c9cce0f8537d4032cfe389869_ipico.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值