【发布时间】:2017-04-30 21:08:05
【问题描述】:
我正在尝试从文本或语料库中提取关键词。这些不是最常见的词,而是最“关于”文本的词。我有一个比较示例,我生成的列表与示例列表非常不同。您能否给我一个指导,以生成一个很好的关键词列表,其中不包括像“thou”和“tis”这样的低含义词?
我使用“罗密欧与朱丽叶”作为我的文本。我的方法(参见下面的 Scott & Tribble)是将 R&J 与莎士比亚的完整戏剧进行比较,并找出 R&J 中出现频率明显高于完整戏剧的单词。这应该清除像“the”这样的常用词,但在我的代码中它没有。
我得到了很多像“thou”、“she”和“tis”这样的词,它们没有出现在他们的列表中,而且我没有得到像“banished”和“churchyard”这样的词。我得到了“romeo”、“juliet”、“capulet”和“nurse”,所以如果不是真的在正确的轨道上,我至少接近正确的轨道。
这是从文本中提取单词和百分比的函数:
def keywords(corpus, threshold=0):
""" generates a list of possible keywords and the percentage of
occurrences.
corpus (list): text or collection of texts
threshold (int): min # of occurrences of word in corpus
target text has threshold 3, ref corp has 0
return percentKW: list of tuples (word, percent)
"""
# get freqDist of corpus as dict. key is word, value = # occurences
fdist = FreqDist(corpus)
n = len(corpus)
# create list of tuple of w meeting threshold & sort w/most common first
t = [(k, v) for k, v in fdist.items() if v >= threshold]
t = sorted(t, key=lambda tup: tup[1], reverse=True)
# calculate number of total tokens
n = len(corpus)
# return list of tuples (word, percent word is of total tokens)
percentKW =[(k, '%.2f'%(100*(v/n))) for k, v in t]
return percentKW
这是调用代码的关键部分。 targetKW 是 R&J,refcorpKWDict 是完整的莎士比亚戏剧。
# iterate through text list of tuples
for w, p in targetKW:
# for each word, store the percent in KWList
targetPerc = float(p)
refcorpPerc = float(refcorpKWDict.get(w, 0))
# if % in text > % in reference corpus
if (refcorpPerc or refcorpPerc == 0) and (targetPerc > refcorpPerc):
diff = float('%.2f'%(targetPerc - refcorpPerc))
# save result to KWList
KWList.append((w, targetPerc, refcorpPerc, diff))
这是我迄今为止尝试过的:
- 将所有可能的关键字标准化为小写(帮助)
- 创建了自定义的关键词短列表(文本和比较文本)。似乎有效,但没有告诉我任何信息
- 将 R&J 与剧本删减列表、剧本 + 十四行诗和布朗语料库进行比较(没有帮助)
- 检查了潜在关键词的百分比,例如“放逐”。百分比远低于预期。我不知道如何解释。
- 设置潜在关键词的最小长度以消除诸如“ll”和“is”之类的词(帮助)
- 用谷歌搜索了这个问题。 (找不到任何东西)
我正在使用 IDLE 版本 3.5.2 在 Windows 10 上使用 Python 3.5.2。
来源: 在“使用 Python 进行自然语言处理”(http://www.nltk.org/book/)中,练习 4.24 是“阅读‘关键字链接’((Scott & Tribble, 2006)的第 5 章)。从 NLTK 的莎士比亚语料库中提取关键字并使用 NetworkX 包,绘制关键字链接网络。”为了工作中的专业发展,我正在独自阅读这本书。 2006年参考的书是《文本模式:语言教育中的关键词和语料库分析》(尤其是第58-60页)
感谢您的宝贵时间。
【问题讨论】: