第一次張貼在文本文件中 - 總能找到以前能夠解決問題的問題!我的主要問題是邏輯......即使是僞代碼答案也會很棒。爲文本文件中的每一行計數(並書寫)文字頻率
我使用python從一個文本文件中的每一行數據讀取,格式爲:
This is a tweet captured from the twitter api #hashtag http://url.com/site
使用NLTK,我可以通過線標記化則可以使用reader.sents()迭代通過等:
reader = TaggedCorpusReader(filecorpus, r'.*\.txt', sent_tokenizer=Line_Tokenizer())
reader.sents()[:10]
但我想進行計數的某些「熱詞」(存儲在數組中或類似的)每行的頻率,然後將它們寫回一個文本文件。如果我使用reader.words(),我可以計算整個文本中「熱門詞彙」的頻率,但是我正在尋找每行的數量(或本例中的「句子」)。
理想的情況下,這樣的:
hotwords = (['tweet'], ['twitter'])
for each line
tokenize into words.
for each word in line
if word is equal to hotword[1], hotword1 count ++
if word is equal to hotword[2], hotword2 count ++
at end of line, for each hotword[index]
filewrite count,
而且,不那麼擔心URL變得破碎(使用WordPunctTokenizer會刪除標點 - 那不是問題)
任何有用的線索(包括僞或鏈接到其他類似的代碼)會很好。
----編輯------------------
結束了做這樣的事情:
import nltk
from nltk.corpus.reader import TaggedCorpusReader
from nltk.tokenize import LineTokenizer
#from nltk.tokenize import WordPunctTokenizer
from collections import defaultdict
# Create reader and generate corpus from all txt files in dir.
filecorpus = 'Twitter/FINAL_RESULTS/tweetcorpus'
filereader = TaggedCorpusReader(filecorpus, r'.*\.csv', sent_tokenizer=LineTokenizer())
print "Reader accessible."
print filereader.fileids()
#define hotwords
hotwords = ('cool','foo','bar')
tweetdict = []
for line in filereader.sents():
wordcounts = defaultdict(int)
for word in line:
if word in hotwords:
wordcounts[word] += 1
tweetdict.append(wordcounts)
輸出是:
print tweetdict
[defaultdict(<type 'dict'>, {}),
defaultdict(<type 'int'>, {'foo': 2, 'bar': 1, 'cool': 2}),
defaultdict(<type 'int'>, {'cool': 1})]
是的 - 只是稍微調整了這一點,但邏輯是偉大的 - 首選這個櫃檯解決方案。爲文本文件中的每行創建一個defaultdict最有效嗎? – bhalsall 2011-04-08 15:35:58
@bhalsall:你可以在每行之後調用'word_counts.clear()',而不是每次創建一個新的defaultdict。 – jfs 2011-04-09 10:13:07