中文詞頻統計與詞云生成

2022-08-29 03:24:10 字數 4679 閱讀 8313

中文詞頻統計

2. 從檔案讀取待分析文字。

3. 安裝並使用jieba進行中文分詞。

4. 更新詞庫,加入所分析物件的專業詞彙。

jieba.add_word('天罡北斗陣')  #逐個新增

jieba.add_word("俺老孫")

jieba.add_word("猴子猴孫")

jieba.load_userdict(word_dict)  #詞庫文字檔案

jieba.load_userdict(r'f:\text\西遊記.txt')

jieba.lcut(txt)

轉換**:scel_to_text

# -*- coding: utf-8 -*-

import struct

import os

# 拼音表偏移,

startpy = 0x1540;

# 漢語片語表偏移

startchinese = 0x2628;

# 全域性拼音表

gpy_table = {}

# 解析結果

# 元組(詞頻,拼音,中文片語)的列表

# 原始位元組碼轉為字串

def byte2str(data):

pos = 0

str = ''

while pos < len(data):

c = chr(struct.unpack('h', bytes([data[pos], data[pos + 1]]))[0])

if c != chr(0):

str += c

pos += 2

return str

# 獲取拼音表

def getpytable(data):

data = data[4:]

pos = 0

while pos < len(data):

index = struct.unpack('h', bytes([data[pos],data[pos + 1]]))[0]

pos += 2

lenpy = struct.unpack('h', bytes([data[pos], data[pos + 1]]))[0]

pos += 2

py = byte2str(data[pos:pos + lenpy])

gpy_table[index] = py

pos += lenpy

# 獲取乙個片語的拼音

def getwordpy(data):

pos = 0

ret = ''

while pos < len(data):

index = struct.unpack('h', bytes([data[pos], data[pos + 1]]))[0]

ret += gpy_table[index]

pos += 2

return ret

# 讀取中文表

def getchinese(data):

gtable =

pos = 0

while pos < len(data):

# 同音詞數量

same = struct.unpack('h', bytes([data[pos], data[pos + 1]]))[0]

# 拼音索引表長度

pos += 2

py_table_len = struct.unpack('h', bytes([data[pos], data[pos + 1]]))[0]

# 拼音索引表

pos += 2

py = getwordpy(data[pos: pos + py_table_len])

# 中文片語

pos += py_table_len

for i in range(same):

# 中文片語長度

c_len = struct.unpack('h', bytes([data[pos], data[pos + 1]]))[0]

# 中文片語

pos += 2

word = byte2str(data[pos: pos + c_len])

# 擴充套件資料長度

pos += c_len

ext_len = struct.unpack('h', bytes([data[pos], data[pos + 1]]))[0]

# 詞頻

pos += 2

count = struct.unpack('h', bytes([data[pos], data[pos + 1]]))[0]

# 儲存

# 到下個詞的偏移位置

pos += ext_len

return gtable

def scel2txt(file_name):

print('-' * 60)

with open(file_name, 'rb') as f:

data = f.read()

print("詞庫名:", byte2str(data[0x130:0x338])) # .encode('gb18030')

print("詞庫型別:", byte2str(data[0x338:0x540]))

print("描述資訊:", byte2str(data[0x540:0xd40]))

print("詞庫示例:", byte2str(data[0xd40:startpy]))

getpytable(data[startpy:startchinese])

getchinese(data[startchinese:])

return getchinese(data[startchinese:])

if __name__ == '__main__':

# scel所在資料夾路徑

in_path = r"f:\1" #修改為你的詞庫檔案存放資料夾

# 輸出詞典所在資料夾路徑

out_path = r"f:\text" # 轉換之後檔案存放資料夾

fin = [fname for fname in os.listdir(in_path) if fname[-5:] == ".scel"]

for f in fin:

try:

for word in scel2txt(os.path.join(in_path, f)):

file_path=(os.path.join(out_path, str(f).split('.')[0] + '.txt'))

# 儲存結果

with open(file_path,'a+',encoding='utf-8')as file:

file.write(word[2] + '\n')

os.remove(os.path.join(in_path, f))

except exception as e:

print(e)

pass

5. 生成詞頻統計

str

strset=set(str)

strdict={}

for w in strset:

strdict[w]=str.count(w)

strdict

6. 排序

strsort = list(strdict.items())

strsort.sort(key = lambda x:x[1],reverse=true)

strsort

7. 排除語法型詞彙,代詞、冠詞、連詞等停用詞。

stops

stops=

str=set(words)-stops

str

8. 輸出詞頻最大top20,把結果存放到檔案裡

for i in range(20):

print(strsort[i])

import pandas as pd

pd.datafarme(data=strsort).to_csv('f:\\西遊記.cvs',encording='utf-8')

9. 生成詞云。

str_split=' '.join(str)

from wordcloud import wordcloud

import matplotlib.pyplot as plt

mywc=wordcloud().generate(str_split)

plt.imshow(mywc)

plt.axis("off")

plt.show()

mywc.to_file(r'f:\xiyouji.png')

wordclout配置了但是還是不顯示,多次檢查都沒有反應

中文詞頻統計與詞云生成

中文詞頻統計 追風箏的人 txt 2.從檔案讀取待分析文字。3.安裝並使用jieba進行中文分詞。pip install jieba import jieba jieba.lcut text 4.更新詞庫,加入所分析物件的專業詞彙。jieba.add word 天罡北斗陣 逐個新增 jieba.lo...

中文詞頻統計與詞云生成

中文詞頻統計 作業連線 2.從檔案讀取待分析文字。3.安裝並使用jieba進行中文分詞。pip install jieba import jieba jieba.lcut text 4.更新詞庫,加入所分析物件的專業詞彙。jieba.add word 天罡北斗陣 逐個新增 jieba.load us...

中文詞頻統計與詞云生成

2.從檔案讀取待分析文字。3.安裝並使用jieba進行中文分詞。pip install jieba import jieba jieba.lcut text 4.更新詞庫,加入所分析物件的專業詞彙。jieba.add word 天罡北斗陣 逐個新增 jieba.load userdict word ...