python 单词纠错_自然语言处理1——语言处理与Python(内含纠错)
学习Python自然语言处理,记录一下学习笔记。
运用Python进行自然语言处理需要用到nltk库,关于nltk库的安装,我使用的pip方式。
pip nltk
或者下载whl文件进行安装。(推荐pip方式,简单又适用)。
安装完成后就可以使用该库了,但是还需要下载学习所需要的数据。启动ipython,键入下面两行代码:
>>>importnltk>>>nltk.download()
就会出现下面的一个界面:
选择book,选择好文件夹,(我选择的是E:\nltk_data)。下载数据。
下载完成后,可以验证一下下载成功与否:
>>> from nltk.book import *
*** Introductory Examples for the NLTK Book ***
Loading text1, ..., text9 and sent1, ..., sent9
Type the name of the text or sentence to view it.
Type: 'texts()' or 'sents()' to list the materials.
text1: Moby Dick by Herman Melville 1851
text2: Sense and Sensibility by Jane Austen 1811
text3: The Book of Genesis
text4: Inaugural Address Corpus
text5: Chat Corpus
text6: Monty Python and the Holy Grail
text7: Wall Street Journal
text8: Personals Corpus
text9: The Man Who Was Thursday by G . K . Chesterton 1908
如果出现上面的文本,说明下载数据成功。
在进行下面的操作之前,一定要保证先导入数据(from nltk.book import *)
prac1:搜索文本:
1.concordance('要搜索的文本')
>>>text1.concordance('monstrous')
Displaying11 of 11matches:
ong the former , one was of a most monstrous size . ... This came towards us ,
ON OF THE PSALMS ."Touching that monstrous bulk of the whale or ork we have r
ll over with a heathenish array of monstrous clubs andspears . Some were thick
d as you gazed ,and wondered what monstrous cannibal andsavage could ever hav
that has survived the flood ; most monstrousandmost mountainous ! That Himmal
they might scout at Moby Dick as a monstrous fable ,or still worse andmore de
th of Radney .'" CHAPTER 55 Of the Monstrous Pictures of Whales . I shall ere l
ing Scenes . In connexion with the monstrous pictures of whales , I am strongly
ere to enter upon those still more monstrous stories of them which are to be fo
ght have been rummaged out of this monstrous cabinet thereisno telling . But
of Whale- Bones ; for Whales of a monstrous size are oftentimes cast up dead u
2.similar('文本'):搜索那些词出现在相似的上下文中:
>>>text1.similar('monstrous')
exasperate imperial gamesome candid subtly contemptible lazy part
pitiable delightfully domineering puzzled determined vexatious
modifies fearless christian horrible mouldy doleful>>>text2.similar('monstrous')
very heartily so exceedingly a extremely good great remarkably
amazingly sweet as vast
可以看出text1和text2在使用monstrous这个词上在表达意思上完全不同,在text2中,monstrous有正面的意思。
3.common_contexts(['word1','word2'...]):研究共用2个或者2个以上的词汇的上下文。
>>>text2.common_contexts(['monstrous','very'])
a_lucky be_glad am_glad a_pretty is_pretty
4.dispersion_plot():位置信息离散图。每一列代表一个单词,每一行代表整个文本。(ps:该函数需要依赖numpy和matplotlib库)
>>>text4.dispersion_plot(['citizens','democracy','freedom','duties','America'])
piac2:计数词汇:
计数词汇主要函数为len(),sorted():用于排序,set():用于得到唯一的词汇,去除重复。这些函数的用法和Python中一样,不做重复。
piac3:简单的统计:
该部分中很多函数都不在适用于python3,有的用法需要自己改进,有的则完全不可用
1.频率分布:FreqDist(文本):统计文本中每个标识符出现的频率。该函数在Python3上使用需要改进。
例如我们在text1《白鲸记》中统计最常出现的50个词:
原始版本:
>>>fdist1=FreqDist(text1)>>>vocabulary1=fdist1.keys()>>>vocabulary[:50]
但是在Python3中却行不通了。我们需要自己对其进行排序;
>>>fdist1=FreqDist(text1)>>>len(fdist1)19317
>>>vocabulary1=sorted(fd.items(),key=lambda jj:jj[1],reverse=True)>>>s=[]>>>for i inrange(len(vocabulary1)):
s.append(vocabulary1[i][0])>>>print(s)
[',', 'the', '.', 'of', 'and', 'a', 'to', ';', 'in', 'that', "'", '-', 'his', 'it', 'I', 's', 'is', 'he', 'with', 'was','as', '"', 'all', 'for', 'this', '!', 'at', 'by', 'but', 'not', '--', 'him', 'from', 'be', 'on', 'so', 'whale', 'one','you', 'had', 'have', 'there', 'But', 'or', 'were', 'now', 'which', '?', 'me', 'like']
在本书中,凡是涉及到FreqDist的都需要对其进行改进操作。
2.细粒度选择词:这里需要用到Python的列表推导式。
例如:选择text1中长度大于15的单词:
>>>V=sorted([w for w in set(text1) if len(w)>15])
['CIRCUMNAVIGATION','Physiognomically','apprehensiveness','cannibalistically','characteristically','circumnavigating','circumnavigation','circumnavigations','comprehensiveness','hermaphroditical','indiscriminately','indispensableness','irresistibleness','physiognomically','preternaturalness','responsibilities','simultaneousness','subterraneousness','supernaturalness','superstitiousness','uncomfortableness','uncompromisedness','undiscriminating','uninterpenetratingly']
判断的条件还有:s.startwith(t)、s.endwith(t)、t in s、s.islower()、s.isupper()、s.isalpha():都是字母、s.isalnum():字母和数字、s.isdigit()、s.istitle()
3.词语搭配和双连词:collocations()函数可以帮助我们完成这一任务。
如查看text4中的搭配:
>>>text4.collocations()
United States; fellow citizens; four years; years ago; Federal
Government; General Government; American people; Vice President; Old
World; Almighty God; Fellow citizens; Chief Magistrate; Chief Justice;
God bless; every citizen; Indian tribes; public debt; one another;
foreign nations; political parties
本书的第一章中还有一个babelize_shell()翻译函数,键入后会出现下面错误:
NameError: name 'babelize_shell' is not defined
原因是因为该模块已经不再可用了。
利用Python的条件分支和循环就可以简单的来处理一些文本信息。
python 单词纠错_自然语言处理1——语言处理与Python(内含纠错)相关推荐
- 2的10次方-1的python表达式_第1章 语言处理与Python
1.尝试使用Python解释器作为一个计算器,输入表达式,如12/(4+1). >>>12/(4+1) 2 2.26个字母可以组成26的10次方或者26**10个10字母长的字符串. ...
- python语音识别预处理_自然语言处理之数据预处理
摘要: 数据预处理的整个步骤流程在自然语言处理的工程中要比其在机器学习的工程中精简一些,最大的区别就在数据清洗和特征构造这两个至关重要的过程.在自然语言处理中特征构造是否良好,很大程度上取决于所构造的 ...
- java c++ python哪个好_程序员学java好还是python语言好 c++又如何
技术程序员学Java.C++.Python... 分析对比,到底学什么语言吃香,哪种语言最靠谱,如何学好这些语言呢? 几天前,我们在知识上看到这样一个问题:"java,C++,Python, ...
- python控制灯泡_人工智能应用-手把手教你用Python硬件编程实现打开或关闭电灯泡...
之前我们已经给广大爱好者或程序员朋友们,带来了硬件版的或者说物联网版本的Hello World C++Builder版.Delphi.Visual Basic.Net等的程序源码和教学资料,让大家对硬 ...
- python ide 最好_我在iPad上最好的Python IDE
python ide 最好 Having finished my first year at university, and being left with very little to do thi ...
- 怎样学python最快_如何少走弯路,快速学会Python
前言 Python其实是一门门槛非常低的语言,学起来很容易的.她的语法十分优雅,只要会基本的英文方式,就能写了,比如a is 1,b is 2这样的.本文定位为Python语言初级教程指南. 图片来自 ...
- python文字冒险游戏_模拟射击文字类游戏(Python)
之前使用java进行一个比较简单的实验--模拟打枪游戏,具体内容就在这里不多说了,以下是python版本,对于python在语法要求上相对其他语言较为松散,所以在编写时遇到了一些坑. 因为单纯以实现为 ...
- python 建筑计算_制图小技巧:巧用Python和ELK瞬间完成总图建筑名称标注
哎呦,又到了每周一次的制图教室啦.经过前面两次制图教程的分享,相信大家对于白模填色和写实渲染这两种表达方式肯定有了较好的掌握. 那么今天我们就转战制图技巧篇,和童鞋们聊一下总平面图中的建筑名称标注问题 ...
- python编程首选_为什么说学编程首选是python
为什么学编程 你可能不会成为一名专业的程序员, 不过学编程的确是有很多的原因的 1. 最重要的是你想学!不论是因为业余爱好还是作为职业,编程都是十分有意思的, 都会让你收获很多 2. 如果你是对计算机 ...
最新文章
- 就在今晚 | 港科大李世玮教授问诊未来,开辟大湾区新航路
- 如何实现更换Jupyter Notebook内核Python版本
- Eclipse插件安装的三种方法
- 设计|从活泼的C端产品到严肃B端产品设计,我是如何自如切换的
- Rational Rose 下载安装
- Java中Lambda表达式与方法引用和构造器引用
- Netsparker超轻量级Web安全漏洞扫描工具使用教程介绍
- Linux内核网络协议栈6-socket地址绑定(2)
- 学习日记0904并发编程socketserver模块 进程理论
- ORA-00054:资源正忙,但指定以nowait方式
- Fortan写出数据到CSV文件中
- matlab 教案,matlab第七讲教案.doc
- miniprogram-ci官方文档
- C++--【基础】--HEX、DEC、OCT数据转换
- 代码编辑器语法着色功能实现-Java版
- Java 的上溯造型和下溯造型以及举例,以及判断参数等指向的类
- RecyclerView使用探索1--了解及使用
- 现在做网站依然可以赚钱
- JVM-对象什么时候进入老年代(实战篇)
- 网路游侠:网络协议分析类产品简析