0420學習筆記(NLTK繼續)

情感分析:打分
實例1
利用AF111.txt打分

sentiment_dictionary={}
for line in open('data/AFINN-111.txt'):
    word, score = line.split('\t')
    sentiment_dictionary[word] = int(score)
words="i love you"
import nltk
word_list=nltk.word_tokenize(words)
total_score = sum(sentiment_dictionary.get(word, 0) for word in word_list)
print(total_score)

結果:
3

實例2
NLTK頻率統計

import nltk
from nltk import FreqDist
# 做個詞庫先
corpus = 'this is my sentence ' \
'this is my life ' \
'this is the day'
# 隨便tokenize⼀下,顯然, 正如上⽂提到,這⾥可以根據需要做任何的preprocessing:stopwords, lemma, stemming, etc.
tokens = nltk.word_tokenize(corpus)
print(tokens)
# 得到token好的word list['this', 'is', 'my', 'sentence','this', 'is', 'my', 'life', 'this','is', 'the', 'day']借用NLTK的FreqDist統計⼀下⽂字出現的頻率
fdist = FreqDist(tokens)
# 它就類似於一個Dict,帶上某個單詞, 可以看到它在整個⽂章中出現的次數
print(fdist['is'])
# 3
# 此刻, 我們可以把最常用的50個單詞拿出來
standard_freq_vector = fdist.most_common(50)
size = len(standard_freq_vector)
print(standard_freq_vector)
# [('is', 3), ('this', 3), ('my', 2),('the', 1), ('day', 1), ('sentence', 1),('life', 1)
# Func: 按照出現頻率⼤小, 記錄下每⼀個單詞的位置
def position_lookup(v):
    res = {}
    counter = 0
    for word in v:
        res[word[0]] = counter
        counter += 1
    return res
# 把標準的單詞位置記錄下來
standard_position_dict = position_lookup(standard_freq_vector)
print(standard_position_dict)
# 得到⼀一個位置對照表{'this': 0, 'is': 1, 'my': 2, 'sentence': 3, 'life': 4, 'the': 5, 'day': 6}
# 這時, 如果我們有個新句子:
sentence = 'this is cool'
# 先新建⼀個跟我們的標準vector同樣⼤小的向量
freq_vector = [0] * size
# 簡單的Preprocessing
tokens = nltk.word_tokenize(sentence)
# 對於這個新句子⾥的每一個單詞
for word in tokens:
    try:
    # 如果在我們的詞庫⾥出現過
    # 那麼就在"標準位置"上+1
        freq_vector[standard_position_dict[word]] += 1
    except KeyError:
    # 如果是個新詞
    # 就pass掉
        continue
print(freq_vector)
# [1, 1, 0, 0, 0, 0, 0]
# 第⼀個位置代表 this, 出現了一次
# 第⼆個位置代表 is, 出現了一次
# 後⾯面都⽊有

結果:
[‘this’, ‘is’, ‘my’, ‘sentence’, ‘this’, ‘is’, ‘my’, ‘life’, ‘this’, ‘is’, ‘the’, ‘day’]
3
[(‘this’, 3), (‘is’, 3), (‘my’, 2), (‘sentence’, 1), (‘life’, 1), (‘the’, 1), (‘day’, 1)]
{‘this’: 0, ‘is’: 1, ‘my’: 2, ‘sentence’: 3, ‘life’: 4, ‘the’: 5, ‘day’: 6}
[1, 1, 0, 0, 0, 0, 0]

實例3
利用NLTK計算TF-IDF值
TF: Term Frequency, 衡量⼀個term在⽂檔中出現得有多頻繁。
TF(t) = (t出現在⽂檔中的次數) / (⽂檔中的term總數).
IDF: Inverse Document Frequency, 衡量⼀個term有多重要。
有些詞出現的很多,但是明顯沒用。
IDF(t) = log_e(⽂檔總數 / 含有t的⽂檔總數).

import nltk
from nltk import FreqDist
from nltk.text import TextCollection
# ⾸先, 把所有的文檔放到TextCollection類中。這個類會⾃自動幫你斷句, 做統計, 做計算
sents=['this is sentence one','this is sentence two','this is sentence three']
sents=[nltk.word_tokenize(sent) for sent in sents] #對每個句子進行分詞
corpus=TextCollection(sents)
# 直接就能算出tf-idf
# (term: ⼀句句話中的某個term, text: 這句話)
print(corpus.tf('one', corpus))
print(corpus.idf('one'))
print(corpus.tf_idf('one',corpus))
# 同理理, 怎麼得到⼀個標準⼤小的vector來表示所有的句子?
# 對於每個新句子
new_sentence = 'this is sentence five'
fdist = FreqDist(corpus)
standard_freq_vector = fdist.most_common(50)
print(standard_freq_vector)
standard_vocab = []
for i in standard_freq_vector:
    standard_vocab.append(i[0])
# 遍歷⼀遍所有的vocabulary中的詞:
for word in standard_vocab:
    print(word)
    print(corpus.tf_idf(word, corpus))
# 我們會得到一個巨⻓(=所有vocab長度)的向量

結果:
0.08333333333333333 #tf
1.0986122886681098 #idf
0.0915510240556758 #tf-idf
[(‘this’, 3), (‘is’, 3), (‘sentence’, 3), (‘one’, 1), (‘two’, 1), (‘three’, 1)]
this
0.0
is
0.0
sentence
0.0
one
0.0915510240556758
two
0.0915510240556758
three
0.0915510240556758

沒出現過的tf爲0,全出現的idf爲0

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章