2010-04-18 231 views
40

我很好奇,是否存在通过使用某些权重计算,出现比率或其他工具从给定文本生成关键字/标签的算法/方法。从文本内容生成标签

此外,如果您指出了任何基于Python的解决方案/库,我将不胜感激。

感谢

+0

你有训练数据? – bayer 2010-04-18 17:13:17

回答

50

做到这一点的一种方法是提取出现在文档中比您期望的更频繁的单词。例如,在更大量的文件中,“马尔可夫”这个词几乎从未见过。然而,马尔科夫在同一个集合中的特定文档中出现频率非常高。这表明马尔可夫可能是一个很好的关键字或标签与文档相关联。

要识别这样的关键字,您可以使用关键字和文档的point-wise mutual information。这由PMI(term, doc) = log [ P(term, doc)/(P(term)*P(doc)) ]给出。这将粗略地告诉你,在特定文档中遇到该术语会让你惊讶于在更大的集合中碰到这个术语的意图是多少(或更多)。

要确定与文档关联的5个最佳关键字,您只需按照他们的PMI分数对文档进行排序,然后选出5个分数最高的分数。

如果要提取多字标记,请参阅StackOverflow问题How to extract common/significant phrases from a series of text entries

从我回答这个问题的借款中,NLTK collocations how-to介绍如何在代码中的约7系使用的n-gram PMI做 提取有趣多字的表达,例如:

import nltk 
from nltk.collocations import * 
bigram_measures = nltk.collocations.BigramAssocMeasures() 

# change this to read in your data 
finder = BigramCollocationFinder.from_words(
    nltk.corpus.genesis.words('english-web.txt')) 

# only bigrams that appear 3+ times 
finder.apply_freq_filter(3) 

# return the 5 n-grams with the highest PMI 
finder.nbest(bigram_measures.pmi, 5) 
+0

这太棒了,非常感谢您花时间! – Hellnar 2010-04-19 15:36:26

+0

我在这里提出了类似的问题:http://stackoverflow.com/questions/2764116/tag-generation-from-a-small-text-content-such-as-tweets 我想知道如果这个算法是在这样一小段文字上取得成功。 – Hellnar 2010-05-04 09:53:09

+0

+1:哇!究竟是我在找什么,甚至没有问它:-) – tmow 2011-01-12 21:22:06

0

一个非常简单的解决问题的方法是:

  • 计算每个单词的出现次数在文本
  • 考虑的最频繁的条款为关键短语
  • 有黑名单中的“停用词”,用于删除常见单词,例如,和,等等。

虽然我确信有更聪明的,基于统计的解决方案。

如果您需要一个解决方案用于大型项目而不是为了利益,雅虎BOSS有一个关键术语提取方法。

9

首先,计算语言学的关键python库是NLTK(“Natural Language Toolkit”)。这是由专业计算语言学家创建和维护的一个稳定,成熟的图书馆。它也有广泛的教程,常见问题等collection我推荐它高度。

下面是一个简单的模板,在python代码中,针对问题中提出的问题;虽然它是一个运行的模板 - 将任何文本作为字符串提供(如我所做的那样),它将返回一个词频列表以及这些词的排列列表,按照“重要性”(或适用性作为关键词)根据一个非常简单的启发式。

给定文档的关键词(显然)是从文档中的重要词语中选出来的 - 也就是那些可能与其他文档区分开来的词语。如果你没有掌握文本主题的知识,一种常用的技术是根据频率或重要性= 1 /频率来推断给定词/词的重要性或权重。

text = """ The intensity of the feeling makes up for the disproportion of the objects. Things are equal to the imagination, which have the power of affecting the mind with an equal degree of terror, admiration, delight, or love. When Lear calls upon the heavens to avenge his cause, "for they are old like him," there is nothing extravagant or impious in this sublime identification of his age with theirs; for there is no other image which could do justice to the agonising sense of his wrongs and his despair! """ 

BAD_CHARS = ".!?,\'\"" 

# transform text into a list words--removing punctuation and filtering small words 
words = [ word.strip(BAD_CHARS) for word in text.strip().split() if len(word) > 4 ] 

word_freq = {} 

# generate a 'word histogram' for the text--ie, a list of the frequencies of each word 
for word in words : 
    word_freq[word] = word_freq.get(word, 0) + 1 

# sort the word list by frequency 
# (just a DSU sort, there's a python built-in for this, but i can't remember it) 
tx = [ (v, k) for (k, v) in word_freq.items()] 
tx.sort(reverse=True) 
word_freq_sorted = [ (k, v) for (v, k) in tx ] 

# eg, what are the most common words in that text? 
print(word_freq_sorted) 
# returns: [('which', 4), ('other', 4), ('like', 4), ('what', 3), ('upon', 3)] 
# obviously using a text larger than 50 or so words will give you more meaningful results 

term_importance = lambda word : 1.0/word_freq[word] 

# select document keywords from the words at/near the top of this list: 
map(term_importance, word_freq.keys()) 
4

http://en.wikipedia.org/wiki/Latent_Dirichlet_allocation试图表示在训练语料库为主题,其又是分布映射词概率的混合物的每个文档。

我曾经使用过它一次,将产品评论的语料分解成所有文档如“客户服务”,“产品可用性”等等的潜在观点。基本模型并不主张一种将主题模型转换为描述一个主题的单个单词的方式......但是人们一旦提出了他们的模型,就想出了各种启发式方法。

我建议你尝试用http://mallet.cs.umass.edu/玩,如果这种模式适合您的需要看到..

LDA是完全无监督的算法,这意味着它不要求你交出任何注释这是伟大的,但在翻盖可能无法为您提供您期望它提供的主题。