python學習筆記之利用jieba庫進行詞頻分析

2021-09-28 19:26:34 字數 1650 閱讀 6992

jieba github倉庫位址

"""詞頻統計"""

import jieba # 沒有安裝jieba庫可以在 使用命令列 pip install jieba 進行自動安裝

deffrequency_()

:# 讀取檔案

txt =

open

("檔案所在目錄.txt"

,"r"

, encoding=

"utf-8"

).read(

)# 這裡檔案路徑位置填寫自己存放檔案的位置

# 這裡使用 jieba 分詞的精確模式

words = jieba.lcut(txt)

# 返回的是list

counts =

'''對進行分詞的之後的結果進行處理,解決類似於孔明和諸葛亮是乙個人物的問題,給其重新賦值'''

for word in words:

# 排除長度為1的

'''if len(word) == 1: # 不想統計單詞或者其它一些字元可以簡單處理一下

continue'''

if word ==

"諸葛亮"

or word ==

"孔明曰"

: rword =

"孔明"

elif word ==

"關公"

or word ==

"雲長"

: rword =

"關羽"

elif word ==

"玄德"

or word ==

"玄德曰"

: rword =

"劉備"

elif word ==

"孟德"

or word ==

"丞相"

: rword =

"曹操"

elif word ==

"周瑜"

or word ==

"都督"

: rword =

"周瑜"

else

: rword = word

# 統計出現的次數

counts[rword]

= counts.get(rword,0)

+1items =

list

(counts.items())

# 安裝從大到小進行排序

items.sort(key=

lambda x: x[1]

, reverse=

true

)# 只要前排序時候出場次數最多的前10個

python爬蟲之利用requests爬取墨跡天氣

import requests from lxml.html import etree import json import time 匯入模組 class mojiweather def city name self 定義乙個輸入城市名稱的函式 cityname str input 輸入城市名稱 ...

python之利用epoll實現http伺服器

usr bin python3.5 coding utf 8 time 18 7 2 下午7 39 author fengwr email fengwenrui1217 163.com file epoll實現http.py 建立epoll物件 epl select.epoll 將tcp sever...

Python筆記 之 利用余弦相似性進行文字選擇

二維余弦相似度計算公式 cos cos theta cos x 1x 2 y1 y2x1 2 y1 2 x2 2 y2 2 over sqrt x12 y 12 x22 y2 2 x 1 x2 y1 y2 n維余弦相識度計算公式 cos cos theta cos k 1k n x1kx 2k k ...