亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Python提取文本tf、idf

系統 1549 0
            
              """python提取文本的tfidf特征"""

import math
from collections import Counter

# 1.語料庫
corpus = [
    'this is the first document',
    'this is the second second document',
    'and the third one',
    'is this the first document'
]

# 2.對語料進行分詞
word_list = []
for i in range(len(corpus)):
    word_list.append(corpus[i].split(' '))
print('2-->', word_list)

# 3.統計詞頻
countlist = []
for i in range(len(word_list)):
    count = Counter(word_list[i])
    countlist.append(count)
print('3詞頻-->', countlist)


# 4.定義計算tfidf公式的函數
# count[word]可以得到每個單詞的詞頻, sum(count.values())得到整個句子的單詞總數
def tf(word, count):
    return count[word] / sum(count.values())


# 統計的是含有該單詞的句子數
def n_containing(word, count_list):
    return sum(1 for count in count_list if word in count)


# len(count_list)是指句子的總數,n_containing(word, count_list)是指含有該單詞的句子的總數,加1是為了防止分母為0
def idf(word, count_list):
    return math.log(len(count_list) / (1 + n_containing(word, count_list)))


# 將tf和idf相乘
def tfidf(word, count, count_list):
    return tf(word, count) * idf(word, count_list)


all_dict = {}
for counte in countlist:
    counter = dict(counte)
    for k, v in counter.items():
        try:
            all_dict[k] += v
        except:
            all_dict[k] = v
print('merge-->', all_dict)

with open('tf.txt', 'w+') as tfin, open('idf.txt', 'w+') as idfin:
    for k in all_dict.keys():
        # k_tf = tf(k, all_dict)
        tfin.write(k + ' ' + str(all_dict[k]) + '\n')
        k_idf = idf(k, countlist)
        idfin.write(k + ' ' + str(k_idf) + '\n')

            
          

?


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 精品国产日韩亚洲一区二区 | 一区二区三区鲁丝不卡麻豆 | 亚洲欧美中文字幕专区 | 日韩视频区| 99热视热频这里只有精品 | 国产好大好爽久久久久久久 | 日本一级毛片2021免费 | 玖玖在线资源 | 国产成人一区二区视频在线观看 | 老司机午夜精品视频在线观看免费 | 国产欧美日韩图片一区二区 | 狠狠狠地啪香蕉 | 日韩国产成人资源精品视频 | 久久香蕉国产线看观看亚洲片 | 成人美女黄网站色大色费 | 波多野结衣一区二区三区 | 日日夜夜操视频 | 欧美日韩国产最新一区二区 | 综合亚洲精品一区二区三区 | 大片国产片日本观看免费视频 | 六月婷婷久香在线视频 | 人成午夜免费大片在线观看 | 国产精品中文字幕在线观看 | 久在线观看 | 神马我不卡在线观看 | 国产一区二区三区在线影院 | 不一样的天空在线高清观看 | 青草久操 | 欧美xxxxbbbb在线播放 | 精品国产一区二区麻豆 | 久草在线视频看看 | 中文字幕一区精品 | 久久99国产乱子伦精品免费 | 久草免费在线视频观看 | 99精品欧美一区二区三区 | 中文字幕免费在线看线人动作大片 | 免费费看的欧亚很色大片 | 久久免费看视频 | 久久爱www成人 | 911视频免费 | 日日夜夜网站 |