日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Frequency 频率统计

發(fā)布時間:2024/1/23 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Frequency 频率统计 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 20 19:16:41 2017

@author: ESRI
"""
import nltk
from nltk import FreqDist
# 做個詞庫先
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)計一下文字出現(xiàn)的頻率
fdist = FreqDist(tokens)
print(fdist)
# 它就類似于一個Dict
# 帶上某個單詞, 可以看到它在整個文章中出現(xiàn)的次數(shù)
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: 按照出現(xiàn)頻率?大?小, 記錄下每?一個單詞的位置
def position_lookup(v):
??? res = {}
??? counter = 0
??? for word in v:
??????? #print(word[0])
??????? res[word[0]] = counter
??????? counter += 1
??? return res

# 把標準的單詞位置記錄下來
standard_position_dict = position_lookup(standard_freq_vector)
print(standard_position_dict)
# 得到?一個位置對照表
# {'is': 0, 'the': 3, 'day': 4, 'this': 1,
# 'sentence': 5, 'my': 2, 'life': 6}

# 這時, 如果我們有個新句句?子:
sentence = 'this is cool'
# 先新建?一個跟我們的標準vector同樣?大?小的向量量
freq_vector = [0] * size
# 簡單的Preprocessing
tokens = nltk.word_tokenize(sentence)
# 對于這個新句句?子?里里的每?一個單詞
for word in tokens:
??? try:
# 如果在我們的詞庫?里里出現(xiàn)過
# 那么就在"標準位置"上+1
??????? freq_vector[standard_position_dict[word]] += 1
??? except KeyError:
# 如果是個新詞
# 就pass掉
??????? continue
print(freq_vector)
# [1, 1, 0, 0, 0, 0, 0]
# 第?一個位置代表 is, 出現(xiàn)了了?一次
# 第?二個位置代表 this, 出現(xiàn)了了?一次
# 后?面都?木有

總結(jié)

以上是生活随笔為你收集整理的Frequency 频率统计的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。