2 獲得文本語料和詞匯資源

from__future__importdivision

importnltk

fromnltk.probabilityimportFreqDist

fromnltk.corpusimportbrown, inaugural, stopwords,swadesh, wordnetaswn, state_union,names,gutenberg

#from nltk.book import *

?4

# print(state_union.fileids())

# cfd=nltk.ConditionalFreqDist((words, fileid[:4])

#? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for fileid in state_union.fileids()

#? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for words in state_union.words(fileid)

#? ? ? ? ? ? ? ? ? ? ? ? ? ? ? )

# words=['men','women','people']

# print(cfd.plot(conditions=words))

8

# cfd=nltk.ConditionalFreqDist((fileid,name.lower()[0])

#? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for fileid in names.fileids()

#? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for name in names.words(fileid))

#

# print(cfd.plot())

15

# fd=nltk.FreqDist(brown.words())

# more_than_tree_times=[word for word in fd if fd[word]>=3]

16

# def word_diversity(words):

#? ? num_word=len(words)

#? ? num_vocab=len(set([word.lower()for word in words]))

#? ? word_diversity=int(num_word/num_vocab)

#? ? return word_diversity

#

# def word_diversity_categories():

#? ? for categorie in brown.categories():

#? ? ? ? word_diversity_categories = word_diversity(brown.words(categories=categorie))

#? ? ? ? print(categorie,':', word_diversity_categories)

#

# word_diversity_categories()

17

# stopwords=stopwords.words('english')

# def frequentest_words(text):

#? ? words=[word for word in text if word.lower() not in stopwords and word.isalpha()]

#? ? cfd=nltk.FreqDist(words)

#? ? frequentest_words = sorted(cfd.items(),key=lambda item:item[1], reverse=True)

#? ? print (frequentest_words[:50])

#

# frequentest_words(gutenberg.words('austen-emma.txt'))

19

# cfd=nltk.ConditionalFreqDist((genre,word)

#? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for genre in brown.categories()

#? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for word in brown.words(categories=genre))

# for genre in brown.categories():

#? ? fdist=cfd[genre]

#? ? sorted_words=sorted(fdist.keys(),key=lambda x:fdist[x],reverse=True)

#? ? print (type(fdist), genre, sorted_words[:10], sorted_words[-10::])

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容