自然语言处理2.3——词典资源

词典或者词典资源是一个词和/或者短语及其相关信息的集合,例如:词性和词意定义等相关信息。词典资源隶属于文本,并且通过在文本的基础上创建和丰富。例如定义了一个文本my_text,然后通过vocab=sorted(set(my_text))建立my_text的词汇表,再利用word_Freq=FreqDist(my_text)计数文本中每个词的频率。vocab和word_Freq都是简单的词汇资源。

【词项】包括词目(词条)及其他附加信息。例如:词性和词意

1、词汇列表语料库

1.1NLTK中包括一些仅仅包含词汇列表的语料库。我们可以使用它来检查文本预料中不常见的或者拼写错误的词汇。

例子:过滤文本:本程序能计算文本的词汇表,然后删除所有在现在的词汇列表中出现的元素,值留下罕见的或者拼写错误的词汇。

def unusual_words(text):
	text_vocab=set(w.lower() for w in text in w.isalpha())
	english_vocab=set(w.lower() for w in nltk.corpus.words.words())
	unusual=text_vocab.difference(english_vocab)
	return sorted(unusual)

>>>unusual_words(nltk.corpus.gutenberg.words(‘austen-sense.txt‘))

输出结果:[‘abbeyland‘, ‘abhorred‘, ‘abilities‘, ‘abounded‘, ‘abridgement‘, ‘abused‘, ‘abuses‘, ‘accents‘, ‘accepting‘, ‘accommodations‘, ‘accompanied‘, ‘accounted‘, ‘accounts‘, ..... 大约有1600个。

1.2还有一个停用词语料库,所谓停用词就是指高频词汇,如the,a,and等等。有时候在进一步处理之前需要将他们过滤出去。

>>>from nltk.corpus import stopwords
>>>stopwords=stopwords.words(‘english‘)

[‘i‘, ‘me‘, ‘my‘, ‘myself‘, ‘we‘, ‘our‘, ‘ours‘, ‘ourselves‘, ‘you‘, ‘your‘, ‘yours‘, ‘yourself‘, ‘yourselves‘, ‘he‘, ‘him‘, ‘his‘, ‘himself‘, ‘she‘, ‘her‘, ‘hers‘, ‘herself‘, ‘it‘, ‘its‘, ‘itself‘, ‘they‘, ‘them‘, ‘their‘, ‘theirs‘, ‘themselves‘, ‘what‘, ‘which‘, ‘who‘, ‘whom‘, ‘this‘, ‘that‘, ‘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‘, ‘should‘, ‘now‘, ‘d‘, ‘ll‘, ‘m‘, ‘o‘, ‘re‘, ‘ve‘, ‘y‘, ‘ain‘, ‘aren‘, ‘couldn‘, ‘didn‘, ‘doesn‘, ‘hadn‘, ‘hasn‘, ‘haven‘, ‘isn‘, ‘ma‘, ‘mightn‘, ‘mustn‘, ‘needn‘, ‘shan‘, ‘shouldn‘, ‘wasn‘, ‘weren‘, ‘won‘, ‘wouldn‘]
可以定义一个函数来计算文本中不包含在停用词列表中的词所占的比例:

from nltk.corpus import stopwords
def content_fraction(text):
	spwords=stopwords.words(‘english‘)
	content=[w for w in text if w.lower() not in spwords]
	return len(content)/len(text)
>>>print(content_fraction(nltk.corpus.reuters.words()))
0.735240435097661

可以看出停用词占了将近1/3的词。

词谜问题:如下图:

词汇列表对于解决上面所示的字谜问题很有用。运行程序去遍历每一个词,检查每一个词是否符合条件。检查必须出现的字母和长度限制很简单,但是指定某些字母出现的两次(V),这样的检查很棘手。利用FreqDist比较法可以检查候选词中的每个字母出现的频率关系。

def puzzle(text):
	puzzle_letter=nltk.FreqDist(text)
	obligatory=‘r‘
	wordlist=nltk.corpus.words.words()
	res=[w for w in wordlist if len(w)>=6 and obligatory in w and nltk.FreqDist(w)<=puzzle_letter]
	print(res)

puzzle(‘egivrvonl‘)

结果为:[‘glover‘, ‘gorlin‘, ‘govern‘, ‘grovel‘, ‘ignore‘, ‘involver‘, ‘lienor‘, ‘linger‘, ‘longer‘, ‘lovering‘, ‘noiler‘, ‘overling‘, ‘region‘, ‘renvoi‘, ‘revolving‘, ‘ringle‘, ‘roving‘, ‘violer‘, ‘virole‘]
1.3 还有一个词汇列表时名字语料库,包括8000个按性别分类的名字。男性和女性的名字存储在单独的文件中。

>>>names=nltk.corpus.names
>>>names.fileids()
[‘female.txt‘,‘male.txt‘]
####寻找男女生都使用的名字:
>>>male_names=names.words(‘male.txt‘)
>>>female_names=names.words(‘female.txt‘)
>>>[w for w in male_names if w in female_name]
[‘Abbey‘, ‘Abbie‘, ‘Abby‘, ‘Addie‘, ‘Adrian‘, ‘Adrien‘, ‘Ajay‘, ‘Alex‘, ‘Alexis‘, ‘Alfie‘, ‘Ali‘, ‘Alix‘, ‘Allie‘, ‘Allyn‘, ‘Andie‘, ‘Andrea‘, ‘Andy‘, ‘Angel‘, ‘Angie‘, ‘Ariel‘, ‘Ashley‘, ‘Aubrey‘, ‘Augustine‘, ...

我们来看看名字最后一位在男女性之间有什么差别

>>>from nltk.corpus import names
>>>cfd=nltk.ConditionalFreqDist((fileid,name[-1]) for fileid in names.fileids() for name in names.words(fileid))
>>>cfd.plot

可以看出大多数以a,e,i结尾的名字为女性,以k,o,r,s,t结尾的为男性。

2.发音的词典

2.1NLTK中包括了美国英语的CMU发音词典,他是为语音合成器而设计的。

>>>entries=nltk.corpus.cumdict.entries()
>>>print(len(entries))
133737
>>>for entry in entries[39943:39948]:
        print(entry)
(‘explorer‘, [‘IH0‘, ‘K‘, ‘S‘, ‘P‘, ‘L‘, ‘AO1‘, ‘R‘, ‘ER0‘])
(‘explorers‘, [‘IH0‘, ‘K‘, ‘S‘, ‘P‘, ‘L‘, ‘AO1‘, ‘R‘, ‘ER0‘, ‘Z‘])
(‘explores‘, [‘IH0‘, ‘K‘, ‘S‘, ‘P‘, ‘L‘, ‘AO1‘, ‘R‘, ‘Z‘])
(‘exploring‘, [‘IH0‘, ‘K‘, ‘S‘, ‘P‘, ‘L‘, ‘AO1‘, ‘R‘, ‘IH0‘, ‘NG‘])
(‘explosion‘, [‘IH0‘, ‘K‘, ‘S‘, ‘P‘, ‘L‘, ‘OW1‘, ‘ZH‘, ‘AH0‘, ‘N‘])

例子:寻找发音包含三个音素的条目,并且第一个发音为‘P‘,第三个发音为‘T‘,打印满足条件的词和该词的第二个音素

>>>entries=nltk.corpus.cmudict.entries()
>>>for word,pron in entries:
            if len(pron)==3:
		ph1,ph2,ph3=pron
		if ph1==‘P‘ and ph3==‘T‘:
			print(word,ph2)

结果:pait EY1 pat AE1 pate EY1 patt AE1 peart ER1 peat IY1 peet IY1 peete IY1 pert ER1 pet EH1 pete IY1 pett EH1 piet IY1 piette IY1 pit IH1 pitt IH1
pot AA1 pote OW1 pott AA1 pout AW1 puett UW1 purt ER1 put UH1 putt AH1

音素包含数字表示主重音(1)、次重音(2)和无重音(0)。定义一个函数来提取重音数字,然后寻找具有特定重音模式的词汇。

>>>entries=nltk.corpus.cmudict.entries()
>>>def stress(pron):
    return [char for phone in pron for char in phone if char.isdigit()]
####寻找音素为01020的词汇
>>>res=[w for w,pron in entries if stress(pron)==[‘0‘,‘1‘,‘0‘,‘2‘,‘0‘]]
>>>print(res)
[‘abbreviated‘, ‘abbreviated‘, ‘abbreviating‘, ‘accelerated‘, ‘accelerating‘, ‘accelerator‘, ‘accelerators‘, ‘accentuated‘, ‘accentuating‘, ‘accommodated‘, ‘accommodating‘, ‘accommodative‘, ‘accumulated‘, ‘accumulating‘, ‘accumulative‘, ‘accumulator‘, ‘accumulators‘...

3.比较词表

NLTK中包含了所谓的斯瓦迪士核心词列表,包括几种语言的约200个常见词的列表。

>>>from nltk.corpus import swadesh
>>>swadesh.fileids()
[‘be‘, ‘bg‘, ‘bs‘, ‘ca‘, ‘cs‘, ‘cu‘, ‘de‘, ‘en‘, ‘es‘, ‘fr‘, ‘hr‘, ‘it‘, ‘la‘, ‘mk‘, ‘nl‘, ‘pl‘, ‘pt‘, ‘ro‘, ‘ru‘, ‘sk‘, ‘sl‘, ‘sr‘, ‘sw‘, ‘uk‘]

可以使用entries()方法来制定一个语言链表来访问多语言的同源词。而且,可以把它转换成一个简单的词典、

>>>fr2en=swadesh.entries([‘fr‘,‘en‘])  ###法语和英语
>>>translate=dict(fr2en)
>>>translate[‘chien‘]  ###进行翻译
‘dog‘
>>>translate[‘jeter‘]
‘throw‘
时间: 2024-08-05 19:28:32

自然语言处理2.3——词典资源的相关文章

文本情感分析的基础在于自然语言处理、情感词典、机器学习方法等内容。以下是我总结的一些资源。

词典资源:SentiWordNet<知网>中文版中文情感极性词典 NTUSD情感词汇本体下载 自然语言处理工具和平台:哈尔滨工业大学社会计算与信息检索研究中心isnowfy/snownlp · GitHub 汉语分词:自然语言处理与信息检索共享平台 NLPIR.orgfxsjy/jieba · GitHub 语料资源:信息分类与情感发现 课程:斯坦福大学自然语言处理第七课"情感分析(Sentiment Analysis)" 网站和博客:Text Classification

python 自然语言处理(四)____词典资源

词典或者词典资源是一个词和/或短语及其相关信息的集合,例如:词性和词意定义等相关信息.词典资源附属于文本,而且通常在文本的基础上创建和丰富.下面列举几种nltk中的词典资源. 1. 词汇列表语料库 nltk中包括了一些仅仅包含词汇列表的语料库.词汇语料库是UNIX中的/usr/dict/words文件,被一些拼写检查程序所使用.我们可以用它来寻找文本语料中不常见的或拼写错误的词汇. 1)过滤词汇 1 >>> def unusual_words(text): 2 ... text_voca

python+NLTK 自然语言学习处理五:词典资源

前面介绍了很多NLTK中携带的词典资源,这些词典资源对于我们处理文本是有大的作用的,比如实现这样一个功能,寻找由egivronl几个字母组成的单词.且组成的单词每个字母的次数不得超过egivronl中字母出现的次数,每个单词的长度要大于6. 要实现这样的一个功能,首先我们要调用FreqDist功能.来得到样本字母中各个字母出现的次数 puzzle_letters=nltk.FreqDist('egivrvonl') for k in puzzle_letters: print(k,puzzle_

自然语言处理相关技术文献资源汇集

1.ACL Anthology A Digital Archive of Research Papers in Computational Linguistics and Natural Language Processing 旧版:http://aclweb.org/anthology/ 新版:http://aclanthology.info/ 2.ACL Anthology Network http://clair.eecs.umich.edu/aan/index.php 3.ACL Wik

自然语言11_情感分析

http://blog.csdn.net/erli11/article/details/23918751 斯坦福大学自然语言处理第七课"情感分析(Sentiment Analysis)" 转自:52opencourse.com/ http://52opencourse.com/235/%E6%96%AF%E5%9D%A6%E7%A6%8F%E5%A4%A7%E5%AD%A6%E8%87%AA%E7%84%B6%E8%AF%AD%E8%A8%80%E5%A4%84%E7%90%86%E7

《Python自然语言处理》

<Python自然语言处理> 基本信息 作者: (美)Steven Bird    Ewan Klein    Edward Loper 出版社:人民邮电出版社 ISBN:9787115333681 上架时间:2014-6-13 出版日期:2014 年6月 开本:16开 页码:508 版次:1-1 所属分类:计算机 > 软件与程序设计 > Python 更多关于>>><Python自然语言处理> 内容简介 书籍 计算机书籍 自然语言处理(natural

《用Python进行自然语言处理》归纳一

1.自然语言工具包(NLTK) NLTK 创建于2001 年,最初是宾州大学计算机与信息科学系计算语言学课程的一部分.从那以后,在数十名贡献者的帮助下不断发展壮大.如今,它已被几十所大学的课程所采纳,并作为许多研究项目的基础.表P -2 列出了NLTK 的一些最重要的模块. 这本书提供自然语言处理领域非常方便的入门指南.它可以用来自学,也可以作为自然语言处理或计算语言学课程的教科书,或是人工智能.文本挖掘.语料库语言学课程的补充读物.本书的实践性很强,包括几百个实际可用的例子和分级练习. 本书基

《21世纪英汉汉英双向词典》《朗文当代英语辞典第五版》《牛津高阶英汉双解词典第7版》

<21世纪英汉汉英双向词典><朗文当代英语辞典第五版><牛津高阶英汉双解词典第7版>10合1英英合集 (2013-03-26 11:38:19)转载▼ <21世纪英汉汉英双向词典> <朗文当代英语辞典第五版> <牛津高阶英汉双解词典第7版> <10合1英英合集字典> 等MDict手机PC iphone多平台词典资源合辑[22.13 GB] MDict 是由国人张文伟(Rayman Zhang)开发的一款可用于 PC.PPC

(离线)英语词典软件推荐

(离线)英语词典软件推荐 MDict Lingoes GoldenDict 欧路 所有软件截图均原创!(为了了解我也够拼了...) 词典数据格式说明: 灵格斯词霸(.ld2) MDict(.mdx,.mdd) GoldenDict(...) 欧路(.eudic,.ld2,.bgl,.mdx .mdd,.tar.gz) 1 MDict MDict for PC 2.0 RC2-win7 MDict软件本身并不提供"词库"(mdx文件),但软件作者提供了词库制作工具(MDXBuilder)