藉由马可夫模型的机率特性,将语言中的最小单位-词语组成句子,再由句子组为文章
马可夫模型:一种纪录机率的统计模型,已任一状态为起点,
依照机率走放到下一阶段,走访完成时机率会为100%
with open("test.txt", encoding="utf-8") as f:
sentences = f.readlines()
sentences = [s.strip() for s in sentences]
引入程序文件并分割
import re
import string
delims = [
",", "。", ";", ":", "!",
"?", "?", ";", ":", "!",
",", ".", "\"", "'", "“",
"‘", "’", "(", ")", "”",
"(", ")", "%", "%", "@",
"~", "`", "~", "`", "#",
"、", "/", "\\", "<", ">",
"《", "》", "/", "{", "}",
"{", "}", "[", "]", "[",
"]", "|", "|", "\n", "\r",
" ", "\t", " ", '+', '=', '*', '^', '·'
]\
+ list("0123456789") \
+ list(string.punctuation)
escaped = re.escape(''.join(delims))
exclusions = '['+escaped+']'
## 断句字典
splitsen = []
for s in sentences:
cleans = re.sub(exclusions, ' ', s)
subs = cleans.split()
splitsen.extend(subs)
## 加入空格後切割 最後存入阵列
for idx, s in enumerate(splitsen):
splitsen[idx] = 'S'+s+"E"
## 头尾加分隔符号
jieba.load_userdict('dict.txt.big')
words = []
for s in splitsen:
ws = list(jieba.cut(s))
words.extend(ws)
def build_word_dict(words):
word_dict = {}
for i in range(1, len(words)):
if words[i-1] not in word_dict:
word_dict[words[i-1]] = {}
if words[i] not in word_dict[words[i-1]]:
word_dict[words[i-1]][words[i]] = 0
word_dict[words[i-1]][words[i]] += 1
return word_dict
word_dict = build_word_dict(words)
print(words)
print(word_dict["人"])
# 算总次数
def wordListSum(wordList):
sumfreq = 0
for word, freq in wordList.items():
sumfreq += freq
#print(sumfreq)
return sumfreq
# 依照机率分布,随机产生下一个字
def retrieveRandomWord(wordList):
#print(wordList)
# 1~n 取乱数
randIndex = randint(1, wordListSum(wordList))
for word, freq in wordList.items():
randIndex -= freq
if randIndex <= 0:
return word
# 产生长度100的Markov chain
length = 100
chain = ""
currentWord = "生活"
for i in range(0, length):
chain += currentWord+""
print(currentWord,"=>",word_dict[currentWord])
currentWord = retrieveRandomWord(word_dict[currentWord])
#print(chain)
import re
reply = re.split('S|E',chain)
reply = [s for s in reply if s != '']
for x in reply:
print(x)
>>: Day 24 [编程03] [译文] 如何在JavaScript 中更好地使用数组
TCP 是一种要求资料正确性的传输方式, 这表示它需要一些特殊机制, 来确保传输的数据不会出错。 其...
UI版面配置 几近年的FPDC游戏,单角色的控制多以First Preson Shooter玩法为主...
再来是办公室表单的处理, 假设有些表单只有图像或是只有纸本, 想要汇入成Excel档案时, 我们就可...
Day 21: Informix(2) 环境设定 查询自己帐号预设的登入 shell 设定 对於 I...
今天的目标: 要怎麽样依照范本复制并改动 Google Sheet,并一次性地的将结果搜集到同一份 ...