欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實(shí)現(xiàn)簡(jiǎn)單中文詞頻統(tǒng)計(jì)示例

 更新時(shí)間:2017年11月08日 11:46:55   作者:Weyne  
本篇文章主要介紹了python實(shí)現(xiàn)簡(jiǎn)單中文詞頻統(tǒng)計(jì)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

本文介紹了python實(shí)現(xiàn)簡(jiǎn)單中文詞頻統(tǒng)計(jì)示例,分享給大家,具體如下:

任務(wù)

簡(jiǎn)單統(tǒng)計(jì)一個(gè)小說(shuō)中哪些個(gè)漢字出現(xiàn)的頻率最高

知識(shí)點(diǎn)

1.文件操作
2.字典
3.排序
4.lambda

代碼

import codecs
import matplotlib.pyplot as plt
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong'] # 指定默認(rèn)字體
mpl.rcParams['axes.unicode_minus'] = False # 解決保存圖像是負(fù)號(hào)'-'顯示為方塊的問(wèn)題

word = []
counter = {}

with codecs.open('data.txt') as fr:
 for line in fr:
  line = line.strip()
  if len(line) == 0:
   continue
  for w in line:
   if not w in word:
    word.append(w)
   if not w in counter:
    counter[w] = 0
   else:
    counter[w] += 1

counter_list = sorted(counter.items(), key=lambda x: x[1], reverse=True)

print(counter_list[:50])

label = list(map(lambda x: x[0], counter_list[:50]))
value = list(map(lambda y: y[1], counter_list[:50]))

plt.bar(range(len(value)), value, tick_label=label)
plt.show()

統(tǒng)計(jì)了一個(gè)11M的小說(shuō),結(jié)果如下:

[(',', 288508), ('。', 261584), ('的', 188693), ('陳', 92565), ('歡', 92505), ('不', 91234), ('是', 90562), ('了', 86931), ('一', 79059), ('著', 77997), ('他'
, 71695), ('這', 63580), ('人', 61210), ('“', 59719), ('”', 59115), ('有', 56054), ('就', 52862), ('個(gè)', 49097), ('都', 46850), ('你', 45400), ('來(lái)', 42659),
 ('我', 40057), ('在', 37676), ('們', 36966), ('到', 36351), ('說(shuō)', 35828), ('還', 35260), ('么', 32601), ('下', 31742), ('地', 30692), ('得', 29904), ('上', 2
9627), ('看', 28408), ('沒(méi)', 28333), ('出', 27937), ('道', 27732), ('大', 27012), ('?', 26729), ('那', 26589), ('要', 26076), ('子', 25035), ('自', 24012), ('
點(diǎn)', 23942), ('好', 21345), ('想', 21242), ('里', 20915), ('面', 20661), ('她', 20313), ('過(guò)', 20304), ('話', 20110)]

 

使用jieba先對(duì)中文文檔進(jìn)行分詞處理

import sys 
reload(sys) 
sys.setdefaultencoding("utf-8") 
 
import jieba 
import jieba.analyse 
 
wf = open('clean_title.txt','w+') 
for line in open('/root/clean_data/clean_data.csv'): 
 
  item = line.strip('\n\r').split('\t') //制表格切分 
  # print item[1] 
  tags = jieba.analyse.extract_tags(item[1]) //jieba分詞 
  tagsw = ",".join(tags) //逗號(hào)連接切分的詞 
  wf.write(tagsw) 
 
wf.close() 

輸出的clean_title.txt內(nèi)容
郵輪,地中海,深度,羅馬,自由納西,柏林簽證,步行,三天,批準(zhǔn)申根,手把手,簽證,申請(qǐng),如何贊爆,法蘭,穿越,葡萄酒,風(fēng)景,河谷,世界歐洲顏色,一種,國(guó)家,一個(gè)水族箱,帕勞,七日,上帝奧林匹亞,跑步圣托, 
里尼,文明古國(guó),探訪,愛(ài)琴海,魅力,希臘 

2、統(tǒng)計(jì)詞頻

#!/usr/bin/python 
# -*- coding:utf-8 -*- 
 
word_lst = [] 
word_dict= {} 
with open('/root/clean_data/clean_title.txt') as wf,open("word.txt",'w') as wf2: //打開文件 
 
  for word in wf: 
    word_lst.append(word.split(',')) //使用逗號(hào)進(jìn)行切分 
    for item in word_lst: 
       for item2 in item: 
        if item2 not in word_dict: //統(tǒng)計(jì)數(shù)量 
          word_dict[item2] = 1 
        else: 
          word_dict[item2] += 1 
 
  for key in word_dict: 
    print key,word_dict[key] 
    wf2.write(key+' '+str(word_dict[key])+'\n') //寫入文檔 

結(jié)果:

最后 4 
歐洲幽藍(lán) 1 
集美 1 
葡萄牙法多 1 
工地 1 
知道湖光山色 1 
神圣 7 
歐洲少女瑞士加游 1 

根據(jù)詞匯數(shù)量排序查看:

cat word.txt |sort -nr -k 2|more

神圣 7 
最后 4 
歐洲幽藍(lán) 1 
集美 1 
葡萄牙法多 1 
工地 1 
知道湖光山色 1 
歐洲少女瑞士加游 1 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論