从文本内容生成代码

我很好奇,如果有一个algorithm/方法存在从给定的文本生成关键字/标签,通过使用一些重量计算,发生率或其他工具。

另外,如果你指出任何基于Python的解决scheme/库,我将不胜感激。

谢谢

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

要识别这样的关键字,您可以使用关键字和文档的点分式互信息 。 这由PMI(term, doc) = log [ P(term, doc) / (P(term)*P(doc)) ] 。 这将粗略地告诉你,在特定文档中碰到这个词的意思是多less(或者更多)惊讶于在较大的集合中碰到这个词。

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

如果要提取多字标记 ,请参阅StackOverflow问题如何从一系列文本条目中提取常见/重要短语 。

借用我对这个问题的回答, NLTK搭配如何在大约7行代码中使用n-gram PMI来提取有趣的多字expression式,例如:

 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) 

首先,计算语言学的关键Python库是NLTK (“ 自然语言工具包 ”)。 这是由专业计算语言学家创build和维护的一个稳定,成熟的图书馆。 它也有广泛的教程,常见问题等集合 ,我高度推荐它。

以下是Python代码中的一个简单模板,用于解决问题中提出的问题; 虽然它是一个模板,它运行 – 提供任何文本作为一个string(如我所做的),它会返回一个单词频率列表以及这些单词的sorting列表,按照“重要性”(或适用性作为关键字)根据一个非常简单的启发式。

给定文档的关键词(显然)是从文档中的重要词语中选出来的,也就是那些可能与其他文档区分开的词语。 如果您对文本的主题没有先验知识,那么一种常用的技术是从其频率或重要性= 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()) 

http://en.wikipedia.org/wiki/Latent_Dirichlet_allocation尝试将每个文档在训练语料库中表示为主题的混合,而这又是将单词映射到概率的分布。;

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

我build议你尝试玩http://mallet.cs.umass.edu/ ,看看这个模型是否符合你的需求..

LDA是一个完全无监督的algorithm,这意味着它不需要你手工注释任何很棒的内容,但另一方面,可能无法提供你所期待的主题。

这个问题的一个非常简单的解决scheme是:

  • 统计文本中每个单词的出现次数
  • 考虑最常用的术语作为关键短语
  • 有一个黑名单的“停止词”,以消除常见的词,如,它,等等

我敢肯定,有一些聪明的,基于统计的解决scheme。

如果你需要一个解决scheme来使用更大的项目而不是为了利益,雅虎BOSS有一个关键的术语提取方法。