使用 python 中的短语和词干进行快速词典查找

Fast lexicon lookup with phrases and stemming in python

我正在 python 中构建文本 classifier,并且我有每个 class 的关键短语列表。例如,classes 可以是 "travel" 和 "science",列表可以包含:

我正在寻找从 python 中的此类列表中匹配短语的最佳方法。

例如,文档的结果:

A famous scientist traveled from New York to Seoul, South Korea

应该是: "science": 1 "travel": 3

即使字符串的 "in" 运算符优化得很好,也有一些情况需要处理:

是否有 python 库可以有效地处理这个问题?如果我需要从头开始实施,在性能方面处理上述问题的最佳方法是什么?

在这种情况下,一个简单的解决方案是使用字典理解:

s = "A famous scientist traveled from New York to Seoul, South Korea"
d = {"travel":["New York", "South Korea", "Seoul"], "science": ["scientist", "chemical"]}
final_results = {a:sum(i in s for i in b) for a, b in d.items()}

输出:

{'science': 1, 'travel': 3}

您尝试实现的是对词干的短语搜索。这是文本挖掘 我认为并在搜索引擎中实现的任务。

首先你需要 tokenizestemmer 函数。标记化可以 就像:

def tokenize(string):
    return fiter(lambda x: len(x) < 1, remove_punctuation(string).split())

pypi 上有各种词干提取器。

您最终将得到如下函数:

def preprocess(string):
    return [stemmer(word) for word in tokenize(string)]

那么您要查找的函数如下所示:

from collections import Counter


def count(dictionary, phrase):
    counter = Count()
    phrase = preprocess(phrase)
    for topic, string in dictionary.items():
        stems = preprocess(string)
        indices = find(phrase, stem[0])
        for index in indices:
            found = True
            for stem in stems[1:]:
                if phrase[index + 1] == stem:
                   continue
                else:
                   found = False
                   break
            if found:
               counter[topic] +=1
    return counter

dictionary 变量包含以下信息:

  • 旅行:"New York"、"South Korea"、"Seoul"等
  • 科学:"scientist"、"chemical"等