一、简介:

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

举一个例子:

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. windows 64位mysql5.7安装

    一.安装mysql 1.下载mysql-5.7.15-winx64.zip http://dev.mysql.com/downloads/mysql/ 2.解压缩到D:\ProgramFiles 3. ...

  2. mysql新增用户并开启远程连接

    之前使用mysql一直使用root来连接登录数据库,现在想使用新的用户名来连接数据库,碰到数据连接不上的情况. 把这些记录下来,以备后用 1.首先,创建用户 CREATE USER 'xiazhenx ...

  3. iOS TabelViewCell 删除 编辑 插入

    /** TableView 进入或退出编辑状态(TableView 方法). */ - (void)setEditing:(BOOL)editing animated:(BOOL)animate{ / ...

  4. C# autocomplete

    前台代码 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runa ...

  5. ORB_SLAM2安装

    进入工程目录,我们发现有两个sh文件,一个是build.sh另一个是build_ros.sh. 这两个都可以进行ORB_SLAM2的安装,我们先来看一下build.sh echo "Conf ...

  6. 微信小程序之点击列表的item带参数跳转界面

    1.在js文件里写个界面跳转的事件处理函数gotableinfo,var index = parseInt(e.currentTarget.dataset.index); 为获取当前点击列表的下脚标, ...

  7. jmeter脚本录制与性能指标分析

    一.浏览器代理设置(猎豹) 1.打开猎豹浏览器,进行如下图操作 2.点击局域网设置 3.输入如下信息,注意端口不要重复 4.输入网址www.baidu.com,不能正常访问就是正确的 5.查看添加的端 ...

  8. listview 点击时间被拦截

    记录下自己所犯的错误,在写ListView的点击事件时OnItemClickListener,onItemClick方法没有执行,导致ListView条目点击事件失效,检查发现百度上有很多不同的答案, ...

  9. LeetCode 922. Sort Array By Parity II C++ 解题报告

    922. Sort Array By Parity II 题目描述 Given an array A of non-negative integers, half of the integers in ...

  10. Django Admin初识

    一.网站后台的作用 网站后台通常是相对于动态网站而言,即网站建设是基于数据库驱动的网站.网站后台,有时也称为网站管理后台,是指用于管理网站前台的一些列操作,如:产品.企业 信息的增加.更新.删除等.通 ...