@Author : By Runsen

文章目录

  • 自然语言处理
  • 自然语言处理应用
  • NLTK
  • 安装语料库
  • 了解Tokenize
  • 标记文本
  • 加载内置语料库
  • 分词(注意只能分英语)
  • 停用词
  • 具体使用
  • 过滤停用词
  • 词性标注
  • 分块
  • 命名实体识别

自然语言处理

自然语言处理(natural language processing)是计算机科学领域与人工智能领域中的一个重要方向。它研究能实现人与计算机之间用自然语言进行有效通信的各种理论和方法。自然语言处理是一门融语言学、计算机科学、数学于一体的科学。

自然语言处理应用

  • 搜索引擎,比如谷歌,雅虎等等。谷歌等搜索引擎会通过NLP了解到你是一个科技发烧友,所以它会返回科技相关的结果。
  • 社交网站信息流,比如 Facebook 的信息流。新闻馈送算法通过自然语言处理了解到你的兴趣,并向你展示相关的广告以及消息,而不是一些无关的信息。
  • 语音助手,诸如苹果 Siri。
  • 垃圾邮件程序,比如 Google 的垃圾邮件过滤程序 ,这不仅仅是通常会用到的普通的垃圾邮件过滤,现在,垃圾邮件过滤器会对电子邮件的内容进行分析,看看该邮件是否是垃圾邮件。

NLTK

NLTK是构建Python程序以使用人类语言数据的领先平台。它为50多种语料库和词汇资源(如WordNet)提供了易于使用的界面,还提供了一套用于分类,标记化,词干化,标记,解析和语义推理的文本处理库。NLTK是Python上著名的⾃然语⾔处理库 ⾃带语料库,具有词性分类库 ⾃带分类,分词,等等功能。NLTK被称为“使用Python进行教学和计算语言学工作的绝佳工具”,以及“用自然语言进行游戏的神奇图书馆”。

安装语料库

pip install nltk

注意,这只是安装好了一个框子,里面是没东西的

# 新建一个ipython,输入
import nltk
nltk.download()

我觉得下book 和popular下好就可以了

功能⼀览表

安装好了,我们来愉快的玩耍

了解Tokenize

把长句⼦拆成有“意义”的⼩部件,,使用的是nltk.word_tokenize

>>> import nltk
>>> sentence = "hello,,world"
>>> tokens = nltk.word_tokenize(sentence)
>>> tokens
['hello', ',', ',world']

标记文本

>>> import nltk
>>> sentence = """At eight o'clock on Thursday morning
... Arthur didn't feel very good."""
>>> tokens = nltk.word_tokenize(sentence)
>>> tokens
['At', 'eight', "o'clock", 'on', 'Thursday', 'morning',
'Arthur', 'did', "n't", 'feel', 'very', 'good', '.']
>>> tagged = nltk.pos_tag(tokens)  # 标记词性
>>> tagged[0:6]
[('At', 'IN'), ('eight', 'CD'), ("o'clock", 'JJ'), ('on', 'IN'),
('Thursday', 'NNP'), ('morning', 'NN')]

加载内置语料库

分词(注意只能分英语)

>>> from nltk.tokenize import word_tokenize
>>> from nltk.text import Text
>>> input_str = "Today's weather is good, very windy and sunny, we have no classes in the afternoon,We have to play basketball tomorrow."
>>> tokens = word_tokenize(input_str)
>>> tokens[:5]
['Today', "'s", 'weather', 'is', 'good']
>>> tokens = [word.lower() for word in tokens] #小写
>>> tokens[:5]
['today', "'s", 'weather', 'is', 'good']

查看对应单词的位置和个数

>>> t = Text(tokens)
>>> t.count('good')
1
>>> t.index('good')
4

还可以画图

t.plot(8)

停用词

from nltk.corpus import stopwords
stopwords.fileids() # 具体的语言果然没有中文
['arabic', 'azerbaijani', 'danish', 'dutch', 'english', 'finnish', 'french', 'german', 'greek','hungarian', 'italian', 'kazakh', 'nepali', 'norwegian', 'portuguese', 'romanian', 'russian','spanish', 'swedish', 'turkish']# 看下英文的停用词
stopwords.raw('english').replace('\n',' ') #会有很多\n,这里替换"i me my myself we our ours ourselves you you're you've you'll you'd your yours yourself yourselves he him his himself she she's her hers herself it it's its itself they them their theirs themselves what which who whom this that that'll these those am is are was were be been being have has had having do does did doing a an the and but if or because as until while of at by for with about against between into through during before after above below to from up down in out on off over under again further then once here there when where why how all any both each few more most other some such no nor not only own same so than too very s t can will just don don't should should've now d ll m o re ve y ain aren aren't couldn couldn't didn didn't doesn doesn't hadn hadn't hasn hasn't haven haven't isn isn't ma mightn mightn't mustn mustn't needn needn't shan shan't shouldn shouldn't wasn wasn't weren weren't won won't wouldn wouldn't "

具体使用

test_words = [word.lower() for word in tokens] #  tokens是上面的句子
test_words_set = set(test_words) # 集合
test_words_set.intersection(set(stopwords.words('english')))
>>>{'and', 'have', 'in', 'is', 'no', 'the', 'to', 'very', 'we'}

在 "Today’s weather is good, very windy and sunny, we have no classes in the afternoon,We have to play basketball tomorrow."中有这么多个停用词

‘and’, ‘have’, ‘in’, ‘is’, ‘no’, ‘the’, ‘to’, ‘very’, ‘we’

过滤停用词

filtered = [w for w in test_words_set if(w not in stopwords.words('english'))]
filtered
['today','good','windy','sunny','afternoon','play','basketball','tomorrow','weather','classes',',','.',"'s"]

词性标注

from nltk import pos_tag
tags = pos_tag(tokens)
tags
[('Today', 'NN'),("'s", 'POS'),('weather', 'NN'),('is', 'VBZ'),('good', 'JJ'),(',', ','),('very', 'RB'),('windy', 'JJ'),('and', 'CC'),('sunny', 'JJ'),(',', ','),('we', 'PRP'),('have', 'VBP'),('no', 'DT'),('classes', 'NNS'),('in', 'IN'),('the', 'DT'),('afternoon', 'NN'),(',', ','),('We', 'PRP'),('have', 'VBP'),('to', 'TO'),('play', 'VB'),('basketball', 'NN'),('tomorrow', 'NN'),('.', '.')]

分块

from nltk.chunk import RegexpParser
sentence = [('the','DT'),('little','JJ'),('yellow','JJ'),('dog','NN'),('died','VBD')]
grammer = "MY_NP: {<DT>?<JJ>*<NN>}"
cp = nltk.RegexpParser(grammer) #生成规则
result = cp.parse(sentence) #进行分块
print(result)
out:
result.draw() #调用matplotlib库画出来

命名实体识别

命名实体识别是NLP里的一项很基础的任务,就是指从文本中识别出命名性指称项,为关系抽取等任务做铺垫。狭义上,是识别出人命、地名和组织机构名这三类命名实体(时间、货币名称等构成规律明显的实体类型可以用正则表达式等方式识别)。当然,在特定的领域中,会相应地定义领域内的各种实体类型。

from nltk import ne_chunk
sentence = "Edison went to Tsinghua University today."
print(ne_chunk(pos_tag(word_tokenize(sentence))))
(S(PERSON Edison/NNP)went/VBDto/TO(ORGANIZATION Tsinghua/NNP University/NNP)today/NN./.)

一、NLTK工具包使用相关推荐

  1. 2.3.NLTK工具包安装、分词、Text对象、停用词、过滤掉停用词、词性标注、分块、命名实体识别、数据清洗实例、参考文章

    2.3.NLTK工具包安装 2.3.1.分词 2.3.2.Text对象 2.3.3.停用词 2.3.4.过滤掉停用词 2.3.5.词性标注 2.3.6.分块 2.3.7.命名实体识别 2.3.8.数据 ...

  2. 离线安装NLTK工具包

    以wordnet为例: 我想使用wordnet工具包,如果在代码中直接使用工具包会报错: ******************************************************* ...

  3. 基于python的语料库数据处理电子版_基于 Python 自然语言处理工具包在语料库研究中的运用...

    基于 Python 自然语言处理工具包在语料库研究中的运用 刘 旭 [摘 要] 摘要:国内当前以语料库为基础的研究,在研究工具方面,多以 AntConc . PowerGREP 为主,使用 Pytho ...

  4. NLTK异常问题 [nltk_data] Error loading reuters: <urlopen error [Errno 11004] [nltk_data] getaddrinfo

    [nltk_data] Error loading reuters: urlopen error [Errno 11004] [nltk_data] getaddrinfo failed 解决办法 1 ...

  5. python使用nltk进行中文语料库的词频分布统计

    文章目录 问题描述 构建语料库 统计字数 统计词频分布 问题描述 根据给定的语料库,统计其中共包含多少字.平均每个词使用了多少次以及常用词的分布以及累计分布情况. 本文就以大秦帝国第一部小说为例进行实 ...

  6. python自然语言处理工具NLTK各个包的意思和作用总结

    [转]http://www.myexception.cn/perl-python/464414.html [原]Python NLP实战之一:环境准备 最近正在学习Python,看了几本关于Pytho ...

  7. python nlp_【NLP】Python NLTK获取文本语料和词汇资源

    作者:白宁超 2016年11月7日13:15:24 摘要:NLTK是由宾夕法尼亚大学计算机和信息科学使用python语言实现的一种自然语言工具包,其收集的大量公开数据集.模型上提供了全面.易用的接口, ...

  8. python电脑配置苹果笔记本-tensorflow学习笔记1——mac开发环境配置

    1. mac电脑推荐配置 内存:8G+ cpu:i5+ 硬盘:SSD 128G+ 本人的电脑配置是cpu:i7, 内存:16G,硬盘:SSD 256G 2. mac开发环境配置 1.1 安装pip 打 ...

  9. 数据科学竞赛-自然语言处理赛流程

    自然语言处理赛流程 在NLP赛中,如今很多思路和CV赛是类似的,甚至Pipeline思路也是一致的. 简介 随着CV(计算机视觉)大量研究人员的涌入,相对而言,计算机视觉的发展进入了一个饱和时期,而自 ...

最新文章

  1. 2022-2028年中国急救中心行业发展战略规划及未来前景展望报告
  2. 链表问题13——删除无序单链表中值重复出现的节点(方法二)
  3. BCH测试网上出现第一个UTXO证明
  4. 给所有想从事软件研发的年轻工程师的忠告与建议
  5. HttpRequest Get和Post调用其他页面的方法
  6. 习题4-8 高空坠球(20 分)
  7. 服务器搬迁方案_医院机房迁移细则规范 amp; 机房搬迁实施规划方案
  8. dts : rx8025t与lm75bd
  9. 苹果手机网速慢_都2020年了,该不该换5G手机?
  10. 金蝶 系统服务器繁忙,金蝶登录时提示云服务器繁忙
  11. feign远程调用传参问题
  12. Qt + libVlc
  13. virtual box上安装ubuntu20.04遇到的问题
  14. pikachu靶场通关之暴力破解
  15. 庆祝祖国成立72周年 做点题目之 BUUCTF Crypto 刷题
  16. obs源码二次开发,自定义插入SEI
  17. 我又愿中国青年都只是向上走,不必理会这冷笑和暗箭!!!!!!!!
  18. 在Windows Server 2008系统环境下无损调整分区
  19. 东南大学校园网自动重连脚本
  20. FineUI(专业版)v3.2.0 发布(ASP.NET UI控件库)!

热门文章

  1. zblog php 指定分类,zblogPHP 为某些分类指定分类模板,后台版方法
  2. 基于sklearn的朴素贝叶斯_朴素贝叶斯分类实战:对文档进行分类
  3. vivado开发编译流程
  4. 转账 程序c语言,求C语言原创小游戏源代码(运行成功可支付宝转账)急!!!
  5. ReactNative ES6简介 及基本语法第一篇
  6. 程序运行正常,数据库没反应
  7. c#基础,单线程,跨线程访问和线程带参数
  8. C# 每月第一天和最后一天
  9. 对PostgreSQL的prepared statement的深入理解
  10. 管道半双工通信程序linux,Linux进程间通信的几种方法-半双工管道,命名管道,消息队列...