Python進行詞頻統計

2021-08-07 11:20:30 字數 1282 閱讀 7165

1.測試文字:

test.txt

2.測試文字內容:

this is just for test

這只是用來測試的

this is just for test

這只是用來測試的

3.**及解釋如下:

import jieba

def doc2matrix(doc):

x=open(doc,'r')

y=x.read()#讀入所有內容

z1=jieba.cut(y)#對所有內容進行分詞

z2=for i in z1:

token=u"';?,。,.!、()() "#標點符號

filter=[i for i in z2 if i not in token]#去除所有標點符號

d={}

for i in filter:

if d.has_key(i):

d[i]+=1

else:

d[i]=1

for keys,values in d.iteritems():

print keys+':'+str(values)

print '/'.join(filter)#輸出分詞結果

if __name__=='__main__':

doc2matrix(r'd:\desktop\test.txt')

4.執行結果如下:

>>> runfile('d:/desktop/untitled0.py', wdir=r'd:/desktop')

的:2just:2

for:2

this:2

is:2

:3這:2

只是:2

測試:2

test:2

用來:2

this/is/just/for/test/

/這/只是/用來/測試/的/

/this/is/just/for/test/

/這/只是/用來/測試/的

from collections import

counter

c =counter()

with open(

'a.txt

','r

',encoding='

utf-8

') as f:

for line in

f.readlines():

words =line.split()

c1 =counter(words)

c.update(c1)

Python進行詞頻統計

基礎python統計詞頻,未考慮到刪除停用詞 詞頻統計 defgettext 處理檔案 txt open english.txt r read txt txt.lower 將英文全部變為小寫 for ch in txt txt.replace ch,return txt mytxt gettext ...

用Python進行詞頻統計

def gettext txt open hamlet.txt r read 讀取檔案 txt txt.lower 把文字全部變為小寫 for ch in 把特殊字元變為空格 txt txt.replace ch,return txt hamlettext gettext words hamlett...

Python分詞並進行詞頻統計

方法一 strs 1 大專以上學歷,年齡在18 28歲之間 2 計算機相關專業 自動化 測控 生儀 機電 數學 物理等等理工科專業優先 3 熱愛軟體開發事業 有較強的邏輯思維能力,對it行業抱有濃厚的興趣並有志於在it行業長遠發展,創造個人價值 非銷售 非保險崗位 4 有無相關經驗均可,歡迎優秀的應...