Frequency 频率统计
生活随笔
收集整理的這篇文章主要介紹了
Frequency 频率统计
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 20 19:16:41 2017
@author: ESRI
"""
import nltk
from nltk import FreqDist
# 做個(gè)詞庫先
corpus = 'this is my sentence ' \
'this is my life ' \
'this is the day'
# 隨便便tokenize?一下
# 顯然, 正如上?文提到,
# 這?里里可以根據(jù)需要做任何的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統(tǒng)計(jì)一下文字出現(xiàn)的頻率
fdist = FreqDist(tokens)
print(fdist)
# 它就類似于一個(gè)Dict
# 帶上某個(gè)單詞, 可以看到它在整個(gè)文章中出現(xiàn)的次數(shù)
print(fdist['is'])
# 3
# 好, 此刻, 我們可以把最常?用的50個(gè)單詞拿出來
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: 按照出現(xiàn)頻率?大?小, 記錄下每?一個(gè)單詞的位置
def position_lookup(v):
??? res = {}
??? counter = 0
??? for word in v:
??????? #print(word[0])
??????? res[word[0]] = counter
??????? counter += 1
??? return res
# 把標(biāo)準(zhǔn)的單詞位置記錄下來
standard_position_dict = position_lookup(standard_freq_vector)
print(standard_position_dict)
# 得到?一個(gè)位置對照表
# {'is': 0, 'the': 3, 'day': 4, 'this': 1,
# 'sentence': 5, 'my': 2, 'life': 6}
# 這時(shí), 如果我們有個(gè)新句句?子:
sentence = 'this is cool'
# 先新建?一個(gè)跟我們的標(biāo)準(zhǔn)vector同樣?大?小的向量量
freq_vector = [0] * size
# 簡單的Preprocessing
tokens = nltk.word_tokenize(sentence)
# 對于這個(gè)新句句?子?里里的每?一個(gè)單詞
for word in tokens:
??? try:
# 如果在我們的詞庫?里里出現(xiàn)過
# 那么就在"標(biāo)準(zhǔn)位置"上+1
??????? freq_vector[standard_position_dict[word]] += 1
??? except KeyError:
# 如果是個(gè)新詞
# 就pass掉
??????? continue
print(freq_vector)
# [1, 1, 0, 0, 0, 0, 0]
# 第?一個(gè)位置代表 is, 出現(xiàn)了了?一次
# 第?二個(gè)位置代表 this, 出現(xiàn)了了?一次
# 后?面都?木有
"""
Created on Fri Oct 20 19:16:41 2017
@author: ESRI
"""
import nltk
from nltk import FreqDist
# 做個(gè)詞庫先
corpus = 'this is my sentence ' \
'this is my life ' \
'this is the day'
# 隨便便tokenize?一下
# 顯然, 正如上?文提到,
# 這?里里可以根據(jù)需要做任何的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統(tǒng)計(jì)一下文字出現(xiàn)的頻率
fdist = FreqDist(tokens)
print(fdist)
# 它就類似于一個(gè)Dict
# 帶上某個(gè)單詞, 可以看到它在整個(gè)文章中出現(xiàn)的次數(shù)
print(fdist['is'])
# 3
# 好, 此刻, 我們可以把最常?用的50個(gè)單詞拿出來
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: 按照出現(xiàn)頻率?大?小, 記錄下每?一個(gè)單詞的位置
def position_lookup(v):
??? res = {}
??? counter = 0
??? for word in v:
??????? #print(word[0])
??????? res[word[0]] = counter
??????? counter += 1
??? return res
# 把標(biāo)準(zhǔn)的單詞位置記錄下來
standard_position_dict = position_lookup(standard_freq_vector)
print(standard_position_dict)
# 得到?一個(gè)位置對照表
# {'is': 0, 'the': 3, 'day': 4, 'this': 1,
# 'sentence': 5, 'my': 2, 'life': 6}
# 這時(shí), 如果我們有個(gè)新句句?子:
sentence = 'this is cool'
# 先新建?一個(gè)跟我們的標(biāo)準(zhǔn)vector同樣?大?小的向量量
freq_vector = [0] * size
# 簡單的Preprocessing
tokens = nltk.word_tokenize(sentence)
# 對于這個(gè)新句句?子?里里的每?一個(gè)單詞
for word in tokens:
??? try:
# 如果在我們的詞庫?里里出現(xiàn)過
# 那么就在"標(biāo)準(zhǔn)位置"上+1
??????? freq_vector[standard_position_dict[word]] += 1
??? except KeyError:
# 如果是個(gè)新詞
# 就pass掉
??????? continue
print(freq_vector)
# [1, 1, 0, 0, 0, 0, 0]
# 第?一個(gè)位置代表 is, 出現(xiàn)了了?一次
# 第?二個(gè)位置代表 this, 出現(xiàn)了了?一次
# 后?面都?木有
總結(jié)
以上是生活随笔為你收集整理的Frequency 频率统计的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NLTK完成简单的情感分析
- 下一篇: pandas使用get_dummies进