2016-06-09 88 views
-2

我有這個腳本,它讀取文件(文件包含收集的推文),清理它,獲取頻率分佈並創建情節,但現在我只能用一個文件工作,我需要什麼是從它創建功能,以便能夠傳遞更多的文件。所以我可以創建數據結果從freqdist更多的文件來繪製它如何在Python中編寫函數

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() 

非常感謝您的任何幫助!

+3

所以你問「我該怎麼做一個功能」? [你在這裏](https://docs.python.org/3/tutorial/controlflow.html#defining-functions)。 – Kevin

+0

基本上是的,我不知道如何編寫它在函數 –

+0

所以你的問題是在Python中寫一個函數,它與文件讀取,數據框或繪圖無關。 – Eular

回答

-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() 
+0

我想是的,謝謝! –

+0

不要忘記標記它是正確的,所以人們不知道再回答你的問題:) – Brian

+0

這泄漏文件句柄,你應該使用'與開放(文件名)作爲f:...' – Daenyth