一、简介:

      把每三个三个单词作为一个整体进行训练。

举一个例子:

input:

      my dream is that I can be an engineer, so I design more applications for people to use.

      my dream is that I can be a bird, so I can fly to everywhere I want.

      it is also my dream that I can be a house, so I can warm you in the cold winter.

生成的马尔可夫链:

    

{'START': ['my dream is'], 'my dream is': ['that i can'], 'dream is that': ['i can be'], 'is that i': ['can be a'], 'that i can': ['be a house,'], 'i can be': ['a house, so'], 'can be an': ['engineer, so i'], 'be an engineer,': ['so i design'], 'an engineer, so': ['i design more'], 'engineer, so i': ['design more applications'], 'so i design': ['more applications for'], 'i design more': ['applications for people'], 'design more applications': ['for people to'], 'more applications for': ['people to use.\nmy'], 'applications for people': ['to use.\nmy dream'], 'for people to': ['use.\nmy dream is'], 'people to use.\nmy': ['dream is that'], 'to use.\nmy dream': ['is that i'], 'use.\nmy dream is': ['that i can'], 'can be a': ['house, so i'], 'be a bird,': ['so i can'], 'a bird, so': ['i can fly'], 'bird, so i': ['can fly to'], 'so i can': ['warm you in'], 'i can fly': ['to everywhere i'], 'can fly to': ['everywhere i want.\nit'], 'fly to everywhere': ['i want.\nit is'], 'to everywhere i': ['want.\nit is also'], 'everywhere i want.\nit': ['is also my'], 'i want.\nit is': ['also my dream'], 'want.\nit is also': ['my dream that'], 'is also my': ['dream that i'], 'also my dream': ['that i can'], 'my dream that': ['i can be'], 'dream that i': ['can be a'], 'be a house,': ['so i can'], 'a house, so': ['i can warm'], 'house, so i': ['can warm you'], 'i can warm': ['you in the'], 'can warm you': ['in the cold'], 'warm you in': ['the cold winter.'], 'you in the': ['cold winter.'], 'in the cold': ['winter.'], 'END': ['the cold winter.', 'winter.', 'cold winter.']}

生成的文本:

my dream is that i can be a house, so i can warm you in the cold winter.

代码:

 # I sperate the input.txt with space, and use dictionary to store the next three words after the current 3 words.
# in the same time, store the first three word as the beginning, and the last three or two or one words as the end
# how to generate output.txt: form the start, start to look for the next three words in ramdom, once meets the end, the geration is end.
import random fhand=open("E:\\a2.txt",'r',encoding='UTF-8')
dataset_file=fhand.read() # dataset_file='my friend makes the best raspberry pies'
dataset_file=dataset_file.lower().split(' ')
model={} for i, word in enumerate(dataset_file):
if i == len(dataset_file) - 3:
model['END'] = model.get('END', []) + [dataset_file[i] + " " + dataset_file[i + 1] + " " + dataset_file[i + 2]]
model['END'] = model.get('END', []) + [dataset_file[i + 2]]
model['END'] = model.get('END', []) + [dataset_file[i + 1] +" "+dataset_file[i + 2]]
elif i == 0:
model['START'] = model.get('START', []) + [dataset_file[i] + " " + dataset_file[i + 1] + " " + dataset_file[i + 2]]
# model['START']=model.get('START',[])+[dataset_file[i]]
# model['START']=model.get('START',[])+[dataset_file[i]+" "+dataset_file[i+1]]
model[dataset_file[i] + " " + dataset_file[i + 1] + " " + dataset_file[i + 2]] = model.get(word, []) + [
dataset_file[i + 3] + " " + dataset_file[i + 4] + " " + dataset_file[i + 5]]
elif i <= (len(dataset_file) - 6):
model[dataset_file[i] + " " + dataset_file[i + 1] + " " + dataset_file[i + 2]] = model.get(word, []) + [
dataset_file[i + 3] + " " + dataset_file[i + 4] + " " + dataset_file[i + 5]]
elif i == (len(dataset_file) - 5):
model[dataset_file[i] + " " + dataset_file[i + 1] + " " + dataset_file[i + 2]] = model.get(word, []) + [
dataset_file[i + 3] + " " + dataset_file[i + 4]]
elif i == (len(dataset_file) - 4):
model[dataset_file[i] + " " + dataset_file[i + 1] + " " + dataset_file[i + 2]] = model.get(word, []) + [
dataset_file[i + 3]]
print(model) generated = []
while True:
if not generated:
words = model['START']
elif generated[-1] in model['END']:
break
else:
words = model[generated[-1]]
generated.append(random.choice(words)) fhand=open("E:\output.txt",'a')
for word in generated:
fhand.write(word+" ") print(word,end=' ')

3阶马尔可夫链 自然语言处理python的更多相关文章

  1. Python标准模块--functools

    1 模块简介 functools,用于高阶函数:指那些作用于函数或者返回其它函数的函数,通常只要是可以被当做函数调用的对象就是这个模块的目标. 在Python 2.7 中具备如下方法, cmp_to_ ...

  2. 自然语言26_perplexity信息

    http://www.ithao123.cn/content-296918.html 首页 > 技术 > 编程 > Python > Python 文本挖掘:简单的自然语言统计 ...

  3. 可爱的 Python : Python中的函数式编程,第三部分

    英文原文:Charming Python: Functional programming in Python, Part 3,翻译:开源中国 摘要:  作者David Mertz在其文章<可爱的 ...

  4. Python 与 Javascript 之比较

    最近由于工作的需要开始开发一些Python的东西,由于之前一直在使用Javascript,所以会不自觉的使用一些Javascript的概念,语法什么的,经常掉到坑里.我觉得对于从Javascript转 ...

  5. python学习菜单

    一.python简介 二.python字符串 三.列表 四.集合.元组.字典 五.函数 六.python 模块 七.python 高阶函数 八.python 装饰器 九.python 迭代器与生成器  ...

  6. Python 与 Javascript 比较

    最近由于工作的需要开始开发一些Python的东西,由于之前一直在使用Javascript,所以会不自觉的使用一些Javascript的概念,语法什么的,经常掉到坑里.我觉得对于从Javascript转 ...

  7. 时间序列算法理论及python实现(1-算法理论部分)

    如果你在寻找时间序列是什么?如何实现时间序列?那么请看这篇博客,将以通俗易懂的语言,全面的阐述时间序列及其python实现. 就餐饮企业而言,经常会碰到如下问题. 由于餐饮行业是胜场和销售同时进行的, ...

  8. MyFirstDay(附6篇python亲历面试题)

    一直以来都是在看别人写的内容,学习前辈们的经验,总感觉自己好像没有什么值得拿出来分享和交流的知识,最近在准备换工作(python后端开发),坐标上海,2019年3月,半个月面了6家(感觉效率是真不高. ...

  9. Python 练习汇总

    1. Python练习_Python初识_day1 2. Python练习_Python初识_day2 3. Python练习_初识数据类型_day3 4. Python练习_数据类型_day4 5. ...

随机推荐

  1. Python学习第七课

    Python学习第七课 'Alex' "Alex"print('hello'*5) #重复输出字符串 print('hellowold'[2:]) #类似于切片操作:会取出 llo ...

  2. 元组tuple 可迭代对象

    1. 字符串.元组和列表相互转化 s = 'abcdef' li = ['a','b','c','d'] tu = (1,2,4,9) v = tuple(s) print('v = ',v) w = ...

  3. AI五子棋需求规格说明书

    AI-Gobang AI五子棋小程序 github地址:https://github.com/holidaysss/AI-Gobang 程序简介 AlphaGo Zero在世界舞台上取得的巨大成功体现 ...

  4. 天转凉了,注意保暖,好吗(需求规格说明书放在github了)

    团队项目——AI五子棋(小程序) 一.团队展示: 队名:未来的将来的明天在那里等你 小组 队员: 龙天尧(队长)(3116005190),林毓植(3116005188),黄晖朝(3116005178) ...

  5. Sublime 黑科技之——lorem快速输入

    需要一段文字填充某块演示的空间,但不管写什么文字都觉得不合适,那么这个黑科技你值的拥有. 在Sublime Text中,输入lorem,再按Tab键,即可快速输入一段无意义的占位字符: Lorem i ...

  6. FileUrl

    package com.rscode.credits.util; import java.io.BufferedReader; import java.io.File; import java.io. ...

  7. adb+monkey压力测试入门

    一.ADB安装步骤及ADB环境配置 1.ADB安装步骤 1)adb工具安装地址:http://www.wmzhe.com/soft-39913.html 2)下载安装包后,解压,将adb安装在根目录下 ...

  8. Jmeter接口测试+压力测试

    链接推荐:https://blog.csdn.net/github_27109687/article/details/71968662

  9. 用python给邮箱发邮件,问题,以及解决方法。

    模版 import smtplib #导入相关模块 from email.mime.text import MIMEText from email.utils import formataddr de ...

  10. Windows与Linux的命令行命令对比

    Windows与Linux的命令行命令对比 * Windows不区分大小写,Linux区分大小写的. sn DOS Command UNIX Equivalent Effect 影响 1 ASSIGN ...