首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从文本内容生成标签

从文本内容生成标签
EN

Stack Overflow用户
提问于 2010-04-18 17:39:24
回答 5查看 27.2K关注 0票数 51

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

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

谢谢

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2010-04-19 06:57:29

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

要识别这样的关键字,可以使用关键字和文档的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提取有趣的多词表达式,例如:

代码语言:javascript
复制
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)  
票数 59
EN

Stack Overflow用户

发布于 2010-04-19 02:19:09

首先,计算语言学的关键python库是NLTK ("Natural Language Toolkit")。这是一个由专业计算语言学家创建和维护的稳定、成熟的库。它也有大量的教程,常见问题解答等collection,我强烈推荐它。

下面是一个简单的模板,使用python代码,用于您提出的问题;尽管它是一个模板,但它会运行--以字符串形式提供任何文本(就像我所做的那样),它将根据一个非常简单的启发式方法返回单词频率列表以及这些单词的排序列表(或适合作为关键字)。

给定文档的关键字(显然)是从文档中的重要单词中选择的--即那些可能将其与其他文档区分开来的单词。如果您没有文本主题的先验知识,一种常见的技术是从频率或重要性=1/频率来推断给定单词/术语的重要性或权重。

代码语言:javascript
复制
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())
票数 10
EN

Stack Overflow用户

发布于 2010-04-19 06:31:56

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

我曾经用它剖析过一个产品评论语料库,把它分解成所有文档中都在谈论的潜在想法,比如“客户服务”、“产品可用性”等。基本模型并不提倡将主题模型转换为描述主题内容的单个单词的方法。但是,一旦他们的模型经过训练,人们就会想出各种启发式方法来做到这一点。

我建议您尝试使用http://mallet.cs.umass.edu/,看看此模型是否适合您的需求。

LDA是一种完全无监督的算法,这意味着它不需要你手动注释任何很棒的东西,但另一方面,它可能不会为你提供你期望的主题。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2661778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档