def word_frequency():word_dict = {}
with open('e:\\pythonfile\\tingyongci.txt') as ti:
ti_list = list(ti.read()) # 獲取停用詞表(綜合哈工大停用詞詞表)
with open('e:\\pythonfile\\jd\\phone\\3133927.txt') as wf:
comments = list(wf.read().split())
for comment in comments:
if comment in ti_list:
continue
else:
if comment not in word_dict:
word_dict[comment] = int(1)
else:
word_dict[comment] += 1
file = open('e:\\pythonfile\\jd\\phone\\test.txt', mode='a') # 將處理結果存到本地txt檔案中
sorted(word_dict.items(), key=lambda item: item[1]) # 按value將字典排序
for key in word_dict:
print(key, word_dict[key])
file.write(key + ' ' + str(word_dict[key]) + '\n') # 寫入文件
file.close()
用jieba分詞處理字串,將分詞結果存到txt檔案中
去停用詞
Python 統計詞頻
calhamletv1.py def gettext txt open hamlet.txt r read txt txt.lower for ch in txt txt.replace ch,將文字中特殊字元替換為空格 return txt hamlettxt gettext words haml...
python 詞頻統計
import re 正規表示式庫 import collections 詞頻統計庫 f open text word frequency statistics.txt article f.read lower 統一轉化成小寫 f.close pattern re.compile t n articl...
python統計詞頻
已知有鍵值對 店名,城市 的鍵值對,我們現在的需求是根據城市來統計店的分布。資料的格式如下 我們希望輸出資料的格式如下所示 所有的資料都是以txt檔案儲存的。from collections import counter from pprint import pprint import os imp...