|
|
|
|
@ -0,0 +1,86 @@
|
|
|
|
|
import os
|
|
|
|
|
import jieba
|
|
|
|
|
import wordcloud
|
|
|
|
|
|
|
|
|
|
def GET_fileName():
|
|
|
|
|
fileList = []
|
|
|
|
|
for files in os.listdir():
|
|
|
|
|
if files.endswith('.txt'):
|
|
|
|
|
fileList.append(os.path.join(files))
|
|
|
|
|
for i in range(len(fileList)):
|
|
|
|
|
print('{:d}---'.format(i+1) + fileList[i])
|
|
|
|
|
return fileList
|
|
|
|
|
|
|
|
|
|
def PUT_endTxt(fileList):
|
|
|
|
|
f = None
|
|
|
|
|
try:
|
|
|
|
|
fileName = fileList[int(input('请输入列表中文件的序号:'))-1]
|
|
|
|
|
f = open(fileName , 'r', encoding='utf-8')
|
|
|
|
|
Jieba(fileName)
|
|
|
|
|
Sort(Account(Turn()))
|
|
|
|
|
|
|
|
|
|
except IndexError:
|
|
|
|
|
print('重新输入!')
|
|
|
|
|
PUT_endTxt(fileList)
|
|
|
|
|
finally:
|
|
|
|
|
if f:
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
def Jieba(fileName):
|
|
|
|
|
with open(fileName, 'r', encoding='utf-8') as f:
|
|
|
|
|
cutWord = jieba.cut(f.read())
|
|
|
|
|
result = " ".join(cutWord)
|
|
|
|
|
result = result.replace(' : ', ' ')
|
|
|
|
|
result = result.replace(' ; ', ' ')
|
|
|
|
|
result = result.replace(' ! ', ' ')
|
|
|
|
|
result = result.replace(' 、 ', ' ')
|
|
|
|
|
result = result.replace(' 。 ', ' ')
|
|
|
|
|
result = result.replace(' 》 ', ' ')
|
|
|
|
|
result = result.replace(' 《 ', ' ')
|
|
|
|
|
result = result.replace(' ) ', ' ')
|
|
|
|
|
result = result.replace(' ( ', ' ')
|
|
|
|
|
result = result.replace(' ', ' ')
|
|
|
|
|
fp = open('1.txt', 'w', encoding='utf-8')
|
|
|
|
|
fp.write(result)
|
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
|
|
def Turn():
|
|
|
|
|
with open('1.txt', 'r', encoding='utf-8') as f:
|
|
|
|
|
wordList = []
|
|
|
|
|
for wordStr in f.readlines():
|
|
|
|
|
wordStr = wordStr.strip()
|
|
|
|
|
wordLine = wordStr.split(' ')
|
|
|
|
|
wordList.extend(wordLine)
|
|
|
|
|
return wordList
|
|
|
|
|
|
|
|
|
|
def Account(wordList):
|
|
|
|
|
accountDict = {}
|
|
|
|
|
for i in wordList:
|
|
|
|
|
accountDict[i] = wordList.count(i)
|
|
|
|
|
return accountDict
|
|
|
|
|
|
|
|
|
|
def Sort(accountDict):
|
|
|
|
|
sortDict = sorted(accountDict.items(), key=lambda d: d[1], reverse=True)
|
|
|
|
|
sortDict = dict(sortDict)
|
|
|
|
|
clearStr = str(sortDict)
|
|
|
|
|
clearStr = clearStr.replace('\'','')
|
|
|
|
|
fp = open('2.txt', 'w', encoding='utf-8')
|
|
|
|
|
fp.write(clearStr)
|
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
|
|
def Printpicture():
|
|
|
|
|
fb = open('2.txt', 'r', encoding='utf-8')
|
|
|
|
|
txt = fb.read()
|
|
|
|
|
fb.close()
|
|
|
|
|
w = wordcloud.WordCloud(font_path='msyhbd.ttc',
|
|
|
|
|
width=1600,
|
|
|
|
|
height=700,
|
|
|
|
|
background_color='white')
|
|
|
|
|
w.generate(txt)
|
|
|
|
|
w.to_file('结果.png')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ =='__main__':
|
|
|
|
|
PUT_endTxt(GET_fileName())
|
|
|
|
|
Printpicture()
|