【发布时间】:2016-06-10 00:30:34
【问题描述】:
我有这个脚本,它读取文件(文件由收集的推文组成),清理它,获取频率分布并创建情节,但现在我只能使用一个文件,我需要从中创建函数,能够传递更多文件。所以我可以从更多文件中创建带有freqdist结果的数据框来绘制它
f = open(.......)
text = f.read()
text = text.lower()
for p in list(punctuation):
text = (text.replace(p, ''))
allWords = nltk.tokenize.word_tokenize(text)
allWordDist = nltk.FreqDist(w.lower() for w in allWords)
stopwords = set(stopwords.words('english'))
allWordExceptStopDist = nltk.FreqDist(w.lower() for w in allWords if w not in stopwords)
mostCommon = allWordExceptStopDist.most_common(25)
frame = pd.DataFrame(mostCommon, columns=['word', 'frequency'])
frame.set_index('word', inplace=True)
print(frame)
histog = frame.plot(kind='barh')
plt.show()
非常感谢您的帮助!
【问题讨论】:
-
所以你问“我如何制作一个函数”? Here you go.
-
基本上是的,我不知道如何在函数中编写它
-
所以你的问题是用python写一个函数,它与文件读取、数据帧或绘图无关。