NLTK(词典资源)

本系列博客为学习《用Python进行自然语言处理》一书的学习笔记。
2.4 节 P63

一、词汇列表语料库

nltk.corpus.words
仅仅包含词汇列表的语料库,可以用来寻找文本语料中不常见的或者拼写错误的词汇

import nltk
def unusual_words(text):
    text_vocab = set([w.lower() for w in text if w.isalpha()])
    english_vocab = set([w.lower() for w in nltk.corpus.words.words()])
    unusual = text_vocab.difference(english_vocab)#集合的差集
    return sorted(unusual)

print unusual_words(nltk.corpus.gutenberg.words('austen-sense.txt'))
[u'abbeyland', u'abhorred', u'abilities', u'abounded', u'abridgement', u'abused', u'abuses', 
...
u'years', u'yielded', u'youngest']

二、停用词语料库

停用词:如 to 、the、等通常没有什么词汇内容
nltk.corpus.stopwords
英语的停用词

print nltk.corpus.stopwords.words('english')
[u'i', u'me', u'my', u'myself',
...
 u"weren't", u'won', u"won't", u'wouldn', u"wouldn't"]

法语的停用词

print nltk.corpus.stopwords.words('French')
[u'au', u'aux', u'avec', u'ce', u'ces', 
...
u'e\xfbt', u'eussions', u'eussiez', u'eussent']
def content_fraction(text):#计算文本中不包含在停用词列表的词所占的比例
    stopwords = nltk.corpus.stopwords.words('english')
    content =[w for w in text if w.lower() not in stopwords]
    return len(content)/len(text)

三、名字语料库

nltk.corpus.names
包括8000个按性别分类的名字。男女的名字存储在单独的文件

names = nltk.corpus.names

cfd = nltk.ConditionalFreqDist(
    (fileid, name[-1])
    for fileid in names.fileids()
    for name in names.words(fileid))

cfd.plot()

这里写图片描述

四、 发音的词典

属于表格词典
nltk.corpus.cmudict
对任意一个词,词典资源都有语音的代码——不同的声音有着不同的标签称作音素
p68
未完

五、比较词表

属于表格词典
nltk.corpus.swadesh
包括几种语言的约200个常用词的列表

from nltk.corpus import swadesh

swadesh.fileids()
Out[172]: 
[u'be',
 u'bg',
 u'bs',
 u'ca',
 u'cs',
 u'cu',
 u'de',
 u'en',
 u'es',
 u'fr',
 u'hr',
 u'it',
 u'la',
 u'mk',
 u'nl',
 u'pl',
 u'pt',
 u'ro',
 u'ru',
 u'sk',
 u'sl',
 u'sr',
 u'sw',
 u'uk']
swadesh.words('en')#英语
Out[173]: 
[u'I',
 u'you (singular), thou',
 u'he',
 u'we',
 ...
 u'if',
 u'because',
 u'name']

简单的翻译器

fr2en = swadesh.entries(['fr','en'])

fr2en
Out[175]: 
[(u'je', u'I'),
 (u'tu, vous', u'you (singular), thou'),
 (u'il', u'he'),
 (u'nous', u'we'),
 ...
 (u'si', u'if'),
 (u'parce que', u'because'),
 (u'nom', u'name')]
translate = dict(fr2en)

translate['chien']
Out[177]: u'dog'

translate['nom']
Out[178]: u'name'

猜你喜欢

转载自blog.csdn.net/csdn_lzw/article/details/80403168