1,语料库的获取
>>>import nltk
>>>nltk.corpus.gutenberg.fileids()
['austen-emma.txt', 'austen-persuasion.txt', 'austen-sense.txt', 'bible-kj
v.txt', 'blake-poems.txt', 'bryant-stories.txt', 'burgess-busterbrown.txt'
, 'carroll-alice.txt', 'chesterton-ball.txt', 'chesterton-brown.txt', 'che
sterton-thursday.txt', 'edgeworth-parents.txt', 'melville-moby_dick.txt',
'milton-paradise.txt', 'shakespeare-caesar.txt', 'shakespeare-hamlet.txt',
'shakespeare-macbeth.txt', 'whitman-leaves.txt']
emma = nltk.corpus.gutenberg.words('austen-emma.txt')
len(emma)
获取的是一些txt文件,好像是一些作者写的文章,如果要用第一章的text1.concordance()这个函数的话,得经过处理
>>>emma = nltk.Text(nltk.corpus.gutenberg.words('austen-emma.txt'))
>>>emma.concordance('surprize')
获取文本语料库
导入包
>>>from nltk.corpus import gutenberg
>>>guitenberg.fileids()
执行得出gutenberg得所有得文件,可以进行遍历得到每个文件然后进行词汇的获取
>>>gutenberg.words() #获取每个文件包含多少个单词
['[', 'Emma', 'by', 'Jane', 'Austen', '1816', ']', ...]
获取每个文件中有多少个词汇,包括空格,对文本没有进行任何处理(部分截图)
>>>gutenberg.raw()
se.\n\nThe wedding was very much like other weddings, where the parties\nh
ave no taste for finery or parade; and Mrs. Elton, from the\nparticulars d
etailed by her husband, thought it all extremely shabby,\nand very inferio
r to her own.--"Very little white satin, very few\nlace veils; a most piti
ful business!--Selina would stare when she\nheard of it."--But, in spite o
f these deficiencies, the wishes,\nthe hopes, the confidence, the predicti
ons of the small band\nof true friends who witnessed the ceremony, were fu
lly answered\nin the perfect happiness of the union.\n\n\nFINIS\n'
gutenberg.sents() 将文件划分成句子
2,利用tokenize包进行语句切分
from nltk.corpus import TreebankWordTokenizer
#切分方式是用空格和‘,’来进行分割,因为英文单词之间有空格
tokenizer = TreebankWordTokenizer()
print(tokenizer.tokenize("如 果 有 一 天 ,我 老 无 所 依 ,请 把 我 埋 在 在 这 春 天 里"))
print(tokenizer.tokenize("today is nice day, happy day"))
#['如', '果', '有', '一', '天', ',我', '老', '无', '所', '依', ',请', '把', '我', '埋', '在', '在', '这', '春', '天', '里']
#['today', 'is', 'nice', 'day', ',', 'happy', 'day']
#按照'.'符号切分句子
from nltk.tokenize import sent_tokenize
print(sent_tokenize("today. is nice day. happy day"))
print(sent_tokenize("如果有一天 .我老无所依.请把我埋在在这春天里"))
#['today.', 'is nice day.', 'happy day']
#['如果有一天 .我老无所依.请把我埋在在这春天里']
#加载pickle文件
french_token = nltk.data.load('tokenizers/punkt/french.pickle')
print(french_token.tokenize("Deux agressions en quelques jours, voilà ce qui a motivé hier matin le débrayage collège franco- britanniquede Levallois-Perret. Deux agressions en quelques jours, voilà ce qui a motivé hier matin le débrayage Levallois. L'équipe pédagogique de ce collège de 750 élèves avait déjà été choquée par l'agression, janvier , d'un professeur d'histoire. L'équipe pédagogique de ce collège de 750 élèves avait déjà été choquée par l'agression, mercredi , d'un professeur d'histoire"))
#['Deux agressions en quelques jours, voilà ce qui a motivé hier matin le débrayage collège franco- britanniquede Levallois-Perret.', 'Deux agressions en quelques jours, voilà ce qui a motivé hier matin le débrayage Levallois.', "L'équipe pédagogique de ce collège de 750 élèves avait déjà été choquée par l'agression, janvier , d'un professeur d'histoire.", "L'équipe pédagogique de ce collège de 750 élèves avait déjà été choquée par l'agression, mercredi , d'un professeur d'histoire"]
#3,word_tokenize 句子切分成单词,按照空格和','来分割,中文也可以
text = nltk.word_tokenize("pierreVinken, 59 years old , will join as a nonexecutive director on Nov .29.")
text1 = nltk.word_tokenize("如 果有一天,我老无 所依,请把我埋 在我家")
#['pierreVinken', ',', '59', 'years', 'old', ',', 'will', 'join', 'as', 'a', 'nonexecutive', 'director', 'on', 'Nov', '.29', '.']
#['如', '果有一天', ',', '我老无', '所依', ',', '请把我埋', '在我家']
#4,单词切分
from nltk.tokenize import WordPunctTokenizer
wordtokenize = WordPunctTokenizer()
print(wordtokenize.tokenize("Don`t hesitate to ask questions"))
#['Don', '`', 't', 'hesitate', 'to', 'ask', 'questions']
标准化的一般步骤
#1,消除标点
text=[" It is a pleasant evening.","Guests, who came from US arrived at the venue","Food was tasty."]
text_list = [word_tokenize(doc) for doc in text]
print(text_list)
for outer in text_list:
print(outer)
for inner in outer:
if inner == '.' or inner == ',':
outer.remove(inner)
print(text_list)
#2,文本的大小写转换
text='HARdWork IS KEy to SUCCESS'
print(text.lower())
print(text.upper())
print(text.capitalize())
print(text.title())
#3,处理停止词
stops = set(stopwords.words('english'))
words = ['Dont`t','hesitate','to','ask','questions']
print([word for word in words if word not in stops])
#各种语言
print(stopwords.fileids())
#各种语言中的停止词
print(stopwords.words('english'))
相似性度量
#4,相似性度量
from nltk.metrics import *
training = 'PERSON OTHER PERSON OTHER OTHER ORGANIZATION'.split()
testing = 'PERSON OTHER OTHER OTHER OTHER OTHER'.split()
print(accuracy(training, testing))
trainset = set(training)
testset = set(testing)
print(precision(trainset, testset))
print(recall(trainset,testset))
print(f_measure(trainset,testset))
0.6666666666666666
1.0
0.6666666666666666
0.8
#Jaccard相似性系数相似性度量,与两个集合的交集进行比较
X = set([10,20,30,40])
Y = set([20,30,60,70])
print(jaccard_distance(X,Y))
0.6666666666666666
#二进制距离度量,两个字符串相等结果是0.0,否则是1.0
print(binary_distance(X,Y))
1.0
1836



被折叠的 条评论
为什么被折叠?



