詞頻統計2

2021-10-10 15:29:03 字數 2119 閱讀 3905

#開啟並讀取檔案  

file

=open

(r'c:\users\21033\desktop\walden.txt'

,'r'

)lines =

file

.readlines(

)

#要把每行拆成單詞、

words =

for line in lines:

#print(line.split(" "))

tmp_list = line.split(

" ")

for word in tmp_list:

',','')

.replace(

',','')

.replace(

'"','')

.replace(

':','')

.lower)

words

# 對words中每乙個元素計算它出現的個數

# 把統計結果儲存到字典中,字典的key是單詞,value是單詞出現的次數

words.count =

word_set =

set(words)

#for word in words:

for word in word_set:

count_num = words.count(word)

#重複取單詞太耗時間所以加入set

word_count[word]

= count_num

word_count

# 對 word_count字典進行排序,按照出現的次數(value)進行降序排序 

sorted

(word_count.items(

),key=lambdn item: item[1]

,reverse=

true

)#word_count.items()

# 整合**輸出py檔案(執行時先把這行刪除)

%%writefile cipin.py

#開啟並讀取檔案

file

=open

(r'c:\users\21033\desktop\walden.txt'

,'r'

)lines =

file

.readlines(

)#要把每行拆成單詞、

words =

for line in lines:

#print(line.split(" "))

tmp_list = line.split(

" ")

for word in tmp_list:

',','')

.replace(

',','')

.replace(

'"','')

.replace(

':','')

.lower)

words

# 對words中每乙個元素計算它出現的個數

# 把統計結果儲存到字典中,字典的key是單詞,value是單詞出現的次數

words.count =

word_set =

set(words)

#for word in words:

for word in word_set:

count_num = words.count(word)

#重複取單詞太耗時間所以加入set

word_count[word]

= count_num

word_count

# 對 word_count字典進行排序,按照出現的次數(value)進行降序排序

sorted

(word_count.items(

),key=lambdn item: item[1]

,reverse=

true

)#word_count.items()

writing ciping.py(執行成功時顯示)

file

.read(

)

PTA 詞頻統計

請編寫程式,對一段英文文字,統計其中所有不同單詞的個數,以及詞頻最大的前10 的單詞。所謂 單詞 是指由不超過80個單詞字元組成的連續字串,但長度超過15的單詞將只擷取保留前15個單詞字元。而合法的 單詞字元 為大小寫字母 數字和下劃線,其它字元均認為是單詞分隔符。輸入給出一段非空文字,最後以符號 ...

詞頻統計(上機)

include include include define error 1 define ok 0 const int word length 250 定義單個單詞最大長度 typedef int status 定義儲存單詞及其出現次數的結構體 typedef struct nodewordnod...

hamlet詞頻統計

part2 code 10.1calhamlet.py def gettext txt open hamlet.txt r read 將文字中的英文本母全部轉為小寫字母 txt txt.lower return txt hamlettxt gettext words hamlettxt.split ...