这里利用2-gram模型来提取一篇英文演讲的初略的主题句子,这里是英文演讲的的链接:http://pythonscraping.com/files/inaugurationSpeech.txt

n-gram模型是指n个连续的单词组成的序列

以下贴出代码(基于python2.7),详情参考《python网络数据采集》

#-*- coding:utf-8 -*-
from urllib2 import urlopen
import re
import string
import operator #单词清洗
def isCommon(ngram):
ngrams=ngram.split(' ')
#清洗以下没有意义的单词
commonWords=['the', 'be', 'and', 'of', 'a', 'in', 'to', 'have', 'it', 'i', 'for', 'you', 'he',
'with', 'on', 'do', 'say', 'this', 'they', 'is', 'an', 'at', 'but', 'we', 'his',
'from', 'that', 'not', 'by', 'she', 'or', 'what', 'go', 'their', 'can', 'who',
'get', 'if', 'would', 'her', 'all', 'my', 'make', 'about', 'know', 'will', 'as',
'up', 'one', 'time', 'has', 'been', 'there', 'year', 'so', 'think', 'when', 'which',
'them', 'some', 'me', 'people', 'take', 'out', 'into', 'just', 'see', 'him', 'your',
'come', 'could', 'now', 'than', 'like', 'other', 'how', 'then', 'its', 'our', 'two',
'more', 'these', 'want', 'way', 'look', 'first', 'also', 'new', 'because', 'day', 'use',
'no', 'man', 'find', 'here', 'thing', 'give', 'many', 'well']
#判断2-gram中是否存在要清洗的单词
for word in ngrams:
if word.lower() in commonWords:
return False
return True #数据清洗
def cleanInput(input):
#装换多个\n和空格为单个空格
input=re.sub('\n+',' ',input)
input=re.sub('\[[0-9]*\]','',input)
input=re.sub(' +',' ',input)
input=bytes(input.decode('utf-8'))
input=input.decode('ascii','ignore')
cleanInput=[]
input=input.split(' ')
for item in input:
#string.punctuation 去除所有符号:!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
item=item.strip(string.punctuation)
if len(item)>1 or (item.lower()=='a' or item.lower()=='i'):
cleanInput.append(item)
return cleanInput #input为输入的整个字符串,n表示以几个字符作为参照,即n-gram
def ngrams(input,n):
input=cleanInput(input)
#声明一个数组
output={}
for i in range(len(input)-n-1):
ngramTemp=' '.join(input[i:i+n])
if isCommon(ngramTemp):
if ngramTemp not in output:
output[ngramTemp]=0
output[ngramTemp]+=1
return output html=urlopen('http://pythonscraping.com/files/inaugurationSpeech.txt').read().decode('utf-8')
content=str(html)
ngrams=ngrams(content,2) #key=operator.itemgetter(0) 表示以字典中的key(字符首字母)为前提排序
#key=operator.itemgetter(1) 表示以字典中的value(数字)为前提排序
#reverse=True 表示降序输出
sortedNGrams=sorted(ngrams.items(),key=operator.itemgetter(1),reverse=True)
#输出有意义的2-gram的单词,以及它们出现的数据
print sortedNGrams #获取上面的的2-gram单词
keywords=[]
for i in range(0,len(sortedNGrams)):
word=sortedNGrams[i]
#除去概率小于2的词组
if int(word[1])>2:
keywords.append(word[0]) #定义一个集合存取文章的所有句子
sentences=set()
#定义一个main_sentences来存储结果
main_sentences=set()
i=content.split('.')
for j in i:
sentences.add(j) for keyword in keywords:
for sentence in sentences:
#获取第一个存在该词组的句子
b=sentence.find(keyword)
if b!=-1:
#除去句子里的\n和多余空格
sentence=re.sub(" +"," ",sentence)
sentence=re.sub("\n+","",sentence)
main_sentences.add(sentence)
break for i in main_sentences:
print i

获取的2-gram的词组为(出现次数大于2):

[u'United States', u'General Government', u'executive department', u'legislative body', u'Mr Jefferson', u'Chief Magistrate', u'called upon', u'same causes', u'whole country', u'Government should']

输出的句子有点多,这里就不贴出来了,这只是初级处理这篇演讲。

自然语言处理(英文演讲)_2-gram的更多相关文章

  1. YouTube排名第一的励志英文演讲《Dream(梦想)》

    I don’t know what that dream is that you have, I don't care how disappointing it might have been as ...

  2. 柳青(Jean)英文演讲集合

    1.Didi Chuxing's Jean Liu on The Future of Cities  https://www.youtube.com/watch?v=G9uPGoN0dvQ 2.Did ...

  3. 《三体》刘慈欣英文演讲:说好的星辰大海你却只给了我Facebook

    美国当地时间2018日11月8日,著名科幻作家刘慈欣被授予2018年度克拉克想象力贡献社会奖(Clarke Award for Imagination in Service to Society),表 ...

  4. 【转载】Deep Learning(深度学习)学习笔记整理

    http://blog.csdn.net/zouxy09/article/details/8775360 一.概述 Artificial Intelligence,也就是人工智能,就像长生不老和星际漫 ...

  5. Deep Learning(深度学习)学习笔记整理系列之(一)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0  2013-0 ...

  6. Deep Learning速成教程

          引言         深度学习,即Deep Learning,是一种学习算法(Learning algorithm),亦是人工智能领域的一个重要分支.从快速发展到实际应用,短短几年时间里, ...

  7. Deep Learning(深度学习)学习笔记整理系列之(一)(转)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0  2013-0 ...

  8. Deep Learning(深度学习)学习笔记整理系列 一

    声明: 1)该Deep Learning的学习系列是整理自网上很大牛和机器学习专家所无私奉献的资料的.具体引用的资料请看参考文献.具体的版本声明也参考原文献. 2)本文仅供学术交流,非商用.所以每一部 ...

  9. N元马尔科夫链的实现

    马尔可夫模型(Markov Model)是一种统计模型,广泛应用在语音识别,词性自动标注,音字转换,概率文法等各个自然语言处理等应用领域.经过长期发展,尤其是在语音识别中的成功应用,使它成为一种通用的 ...

随机推荐

  1. Day1-python基础-变量常量

    不积跬步无以至千里 补充上一节字符串的内容: 字符串格式化输出: name = input("name>>") print("My name is %s&qu ...

  2. 关于内存类型 UDIMM、RDIMM、LRDIMM 的学习结论(转)

    随着内存技术不断发展,服务器上内存的容量.密度和速度也越来越高.目前在市场上出现的内存条最高密度可以做到每条内存条 4 个 Rank,容量达到 32GB/条,最高速度达到 1.6GHz.高密度高频率也 ...

  3. Qt_模块简介

    Qt4 和 Qt5最大的区别之一就是底层架构有了修改.Qt5引入了模块化的概念,讲众多功能细分到几个模块之中.需要达到,用什么模块知道到哪个模块去寻找. Qt5模块分为Essentials Modul ...

  4. 《mysql必知必会》学习_第八章_20180730_欢

    第八章学习LIKE操作符,百分百(%)通配符,下划线(_)通配符 P47 select prod_id,prod_name from products where prod_name LIKE 'je ...

  5. Maven中项目的启动

    在run选项卡中,选择Run Configurations

  6. cpld fpga 区别

    cpld fpga 区别 系统的比较,与大家共享: 尽管FPGA和CPLD都是可编程ASIC器件,有很多共同特点,但由于CPLD和FPGA结构上的差异,具有各自的特点: ①CPLD更适合完成各种算法和 ...

  7. Kali Linux渗透测试实战 2.1 DNS信息收集

    目录 2.1 DNS信息收集1 2.1.1 whois查询3 2.1.2 域名基本信息查询4 Dns服务器查询4 a记录查询4 mx记录查询5 2.1.3 域名枚举5 fierse 5 dnsdict ...

  8. MAC系统下用Idea创建spring boot工程 基于maven

    1.创建项目 打开idea编辑器,选择file  -> new -> project 点击next 依次填入group,artifact 填写完成之后再点击“next” 根据自己的需求在最 ...

  9. python threading模块2

    Thread 是threading模块中最重要的类之一,可以使用它来创建线程.有两种方式来创建线程:一种是通过继承Thread类,重写它的run方法:另一种是创建一个threading.Thread对 ...

  10. UWP Button添加圆角阴影(一)

    原文:UWP Button添加圆角阴影(一) 众所周知,17763之前的UWP控件,大部分是没有圆角属性的:而阴影也只有17763中的ThemeShadow可以直接在xaml中使用,之前的版本只能用D ...