【问题标题】:How do I write a function in python [closed]如何在python中编写函数[关闭]
【发布时间】: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写一个函数,它与文件读取、数据帧或绘图无关。

标签: python function


【解决方案1】:

这是你的意思吗?

def readStuff( filename )
    with open(filename) as f:
        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()

【讨论】:

  • 我想是的,谢谢!
  • 别忘了把它标记为正确,这样人们就知道不要再回答你的问题了:)
  • 这会泄露文件句柄,你应该使用with open(filename) as f: ...
  • 我只是将上面的代码放入一个函数中,但@Daenyth 是正确的。已编辑。
  • 谢谢,我会试试的
猜你喜欢
  • 2022-01-02
  • 2021-05-30
  • 1970-01-01
  • 2021-05-03
  • 2017-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多