我为我的网站创建了一个cron作业,每2小时运行一次,它会计算Feed中的单词,然后显示10个最高计数单词作为热门话题.
推特在主页上做了些什么,以展示正在讨论的最热门话题.
我的cron工作现在做的是除了我提到的单词之外的单词,例如:
array('of', 'a', 'an', 'also', 'besides', 'equally', 'further', 'furthermore', 'in', 'addition', 'moreover', 'too',
'after', 'before', 'when', 'while', 'as', 'by', 'the', 'that', 'since', 'until', 'soon', 'once', 'so', 'whenever', 'every', 'first', 'last',
'because', 'even', 'though', 'although', 'whereas', 'while', 'if', 'unless', 'only', 'whether', 'or', 'not', 'even',
'also', 'besides', 'equally', 'further', 'furthermore', 'addition', 'moreover', 'next', 'too',
'likewise', 'moreover', 'however', 'contrary', 'other', 'hand', 'contrast', 'nevertheless', 'brief', 'summary', 'short',
'for', 'example', 'for instance', 'fact', 'finally', 'in brief', 'in conclusion', 'in other words', 'in short', 'in summary', 'therefore',
'accordingly', 'as a result', 'consequently', 'for this reason', 'afterward', 'in the meantime', 'later', 'meanwhile', 'second', 'earlier', 'finally', 'soon', 'still', 'then', 'third'); //words that are negligible
Run Code Online (Sandbox Code Playgroud)
但这并没有完全解决消除所有不需要的词的问题.并且只给出有用的单词.
有人可以指导我这个,并告诉我如何改进我的算法.
关心Zeeshan
Jam*_*ber 11
如果您想要静态显着的异常值,您可能需要计算相对于整个文本的最近子集中每个单词的z分数.
因此,如果
t is number of occurrences of word in subset
o is number of occurrences of word overall
n_t is number of words in subset
n_o is number of words overall
Run Code Online (Sandbox Code Playgroud)
然后计算:
p_hat = t / n_t
p_0 = o / n_o
z = (p_hat - p_0) / sqrt((p_0 * (1 - p_0)) / n_t)
Run Code Online (Sandbox Code Playgroud)
z越高,在子集中提及单词相对于整个文本的统计意义越大.这也可以用于计算相对于整个文本在子集中奇怪罕见的单词.
欢迎来到精彩的语言处理世界.基本上,趋势主题和朋友之类的东西都是搜索语言使用异常.
从理论上讲,通过分析单词随时间的频率,你应该能够滤除噪音(常用词,就像你上面列出的那样).这不是一件容易实现的事,但绝对是可能的.
另一个方法是不要专注于特定时期内的原始数量,而是关注趋势主题发展的模式.它们通常会呈指数级增长,并且应该可以通过尝试应用过滤器来重新验证现有搜索的结果,该过滤器会丢弃所有不符合此类增长的"热词".
只是一些想法:-)
编辑:
为了进一步概述我的频率过滤意味着什么,也许你应该查看包含有关单词的频率信息的字典.建立起来并不是那么难,并且使用纯文本语料库(维基百科可以免费下载,我用它进行测试),你将获得非常好的结果.
下面是我们如何DjangoCon期间实施此为DjangoDose活饲料(注:这是一个hackjob,我们没有测试写在1日下午,并occsaionally大喊大叫分歧,尽我可以告诉分叉有什么用什么做的).所有这一切,它或多或少都为我们工作(意味着在晚上啤酒得到适当的跟踪).
IGNORED_WORDS = set(open(os.path.join(settings.ROOT_PATH, 'djangocon', 'ignores.txt')).read().split())
def trending_topics(request):
logs = sorted(os.listdir(LOG_DIRECTORY), reverse=True)[:4]
tweets = []
for log in logs:
f = open(os.path.join(LOG_DIRECTORY, log), 'r')
for line in f:
tweets.append(simplejson.loads(line)['text'])
words = defaultdict(int)
for text in tweets:
prev = None
for word in text.split():
word = word.strip(string.punctuation).lower()
if word.lower() not in IGNORED_WORDS and word:
words[word] += 1
if prev is not None:
words['%s %s' % (prev, word)] += 1
words[prev] -= 1
words[word] -= 1
prev = word
else:
prev = None
trending = sorted(words.items(), key=lambda o: o[1], reverse=True)[:15]
if request.user.is_staff:
trending = ['%s - %s' % (word, count) for word, count in trending]
else:
trending = [word for word, count in trending]
return HttpResponse(simplejson.dumps(trending))
Run Code Online (Sandbox Code Playgroud)