一、简介:

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

举一个例子:

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. C#杀掉进程的方法

    C#杀掉进程的方法 private static string CmdName = "cmd"; /// <summary> /// 关闭进程 /// </sum ...

  2. L2-013 红色警报 (25 分)

    L2-013 红色警报 (25 分)   战争中保持各个城市间的连通性非常重要.本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报.注意:若该国本来就不完全 ...

  3. Linux下Apache的安装【可用】

    失败的情况有很多种,但成功的路有时候只有一条.在经历了多次失败安装后,特在此将apache安装的精简步骤罗列出来供日后参考. ====================APACHE 安装方法====== ...

  4. c# 在.NET使用Newtonsoft.Json转换,读取,写入json

    转自:http://blog.sina.com.cn/s/blog_70686f3a0101kemg.html  首先,大家要明白什么是json,了解更多关于json方面资料大家可以点击https:/ ...

  5. 关于grub修复引导系统

    这周末遇到停电,机房的一台数据服务器启动不了,开机硬件自检以后,就停留在一个黑屏状态左上角有光标闪烁,却一直进入不了系统. 还好手里有centos6.5的系统盘,进入修复选项,具体进入修复请参照这里 ...

  6. ROS使用小知识点

    输入 rosrun rqt_graph rqt_graph 可以打开一个界面观察节点与话题的关系 绿色和蓝色的是节点 红色的是话题 查看ros中额的tf转换信息 rosrun rqt_tf_tree ...

  7. vue 判断数组是否为空

    为空:array == undefined || array.length <= 0 (顺序不能调换) 不为空: array !==undefined && array.leng ...

  8. Lock详解

    在JDK1.5后,并发包里新增了Lock接口以及其实现类来实现锁功能,它提供了与synchronized关键字类似的锁功能,但它需要手动开启.关闭锁.虽然看起来没有synchronized方便,但它可 ...

  9. vue day7 table checkbox 全选

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  10. 网页真机调试之Browsersync简介

    以前的调试方式 修改完项目文件(html.js.css等)后保存,在浏览器刷新页面查看修改后的效果 本地开启一个 tomcat 服务,修改文件后保存刷新页面,移动端或其他 pc 则需要输入 ip + ...