資源簡介
python編程語言 預處理 統計詞頻 計算IT-IDF
代碼片段和文件信息
#?coding=utf-8???????????????????#注意此句注釋要放在第一句,可進行中文注釋
from?__future__?import?division??#?保證得到正常除法計算的結果
import?os
import?math
import?nltk
import?codecs??#?保證可以用指定的編碼格式打開文件
from?nltk.corpus?import?stopwords
import?sys
reload(sys)
sys.setdefaultencoding(‘utf-8‘)
doc_num?=?0??#?統計文檔總數
dictionary?=?[]??#?詞典,即文件中的單詞列表
word_idf_dict?=?{}??#?字典,存放詞典中每個單詞的idf值
num_doc_word?=?{}??#?字典,存放每篇論文的單詞總數
def?processing(str):
????“對給定的文本進行預處理返回值是一個列表,存儲預處理之后的單詞“
????str_lower?=?str.lower()??#?將文本小寫化
????sens?=?nltk.sent_tokenize(str_lower)??#?將小寫化的文本進行句子分詞
????words?=?[]
????#?對每個句子進行分詞
????for?sen?in?sens:
????????words.extend(nltk.word_tokenize(sen))??#?注意區分append?VS?extend
????????stopword?=?stopwords.words(‘english‘)??#?去除停頓詞
????????punctuation?=?[‘‘?‘.‘?‘:‘?‘;‘?‘(‘?‘)‘?‘[‘?‘]‘?‘&‘?‘#‘?‘!‘?‘?‘?‘@‘?‘$‘?‘%‘]??#?去除標點符號
????????stemming?=?nltk.stem.SnowballStemmer(‘english‘)??#?提取詞干
????????new_words?=?[]
????????for?word?in?words:
????????????if?(word.isalpha())?and?(word?not?in?stopword)?and?(word?not?in?punctuation):??#?去除亂碼,即非字母的單詞
????????????????new_words.append(stemming.stem(word))??#?append?VS?extend
????return?new_words
def?compute_tf(wordlist):
????“統計給定單詞列表的詞頻,返回值是一個詞典,key為單詞,value為該單詞對應的詞頻“
????temp_dict?=?{}
????for?word?in?wordlist:
????????if?word?in?temp_dict:
????????????temp_dict[word]?+=?1
????????else:
????????????temp_dict[word]?=?1
????return?temp_dict
def?compute_idf(word_in_file):
????“統計單詞的逆向文檔頻率,參數的數據類型為dict{file_name:dict{word:word_tf}}}“
????for?word?in?dictionary:
????????word_in_doc?=?0??#?統計出現過該單詞的文檔數目
????????for?index?in?word_in_file:
????????????if?word?in?word_in_file[index].keys():
????????????????word_in_doc?+=?1
????????word_in_doc?=?math.log10(doc_num?/?word_in_doc)??#?計算單詞的idf值
????????word_idf_dict[word]?=?word_in_doc??#?得到全局變量word_idf_dict
????????#?此處沒有將word_idf_dict作為返回值返回,而是將其定義為全局變量,便于其他函數使用
def?compute_tfidf(word_in_file):
????“計算單詞的tf-idf值,參數的數據類型為dict{file_name:dict{word:word_tf}}“
????word_tfidf?=?{}??#?存放單詞的tf-idf值,數據類型為dict{file_name:dict{word:word_tfidf}}
????for?index?in?word_in_file:
????????word_tfidf[index]?=?{}??#?存放指定文檔下的dict{word:word_tfidf}
????????temp_len?=?num_doc_word[index]??#?使用全局變量num_doc_word,計算指定文檔下的單詞總數
????????for?word?in?word_in_file[index].keys():
????????????word_tfidf[index][word]
- 上一篇:python爬蟲爬取58租房信息
- 下一篇:Python-opencv-植物葉片識別
評論
共有 條評論