微网站制作价格,静态网页有哪些网站,自己做网站 怎样下载模板,摄影设计师招聘第一次在堆栈中发布 – 总是发现以前的问题足以解决我的问题#xff01;我遇到的主要问题是逻辑……即使是伪代码答案也会很棒.
我正在使用python从文本文件的每一行读取数据,格式如下#xff1a;
This is a tweet captured from the twitter api #hashtag http://url.com/si…第一次在堆栈中发布 – 总是发现以前的问题足以解决我的问题我遇到的主要问题是逻辑……即使是伪代码答案也会很棒.
我正在使用python从文本文件的每一行读取数据,格式如下
This is a tweet captured from the twitter api #hashtag http://url.com/site
使用nltk,我可以逐行标记,然后可以使用reader.sents()迭代等
reader TaggedCorpusReader(filecorpus, r.*\.txt, sent_tokenizerLine_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_tokenizerLineTokenizer())
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(, {}),
defaultdict(, {foo: 2, bar: 1, cool: 2}),
defaultdict(, {cool: 1})]