首页 > 解决方案 > NLP - 从句子中查找关键字

问题描述

我正在研究 NLP 项目,来自下面的附加代码。我只能匹配并找到列表中给出的单词的相似度分数。

谁能帮助我如何从段落中找到相似之处?假设列表 A 包含一个段落,列表 B 包含一个关键字。如何在列表 A 中找到关键字?

from collections import Counter
from math import sqrt
import eng_to_ipa as ipa

def word2vec(word):
    cw = Counter(word)
    sw = set(cw)
    lw = sqrt(sum(c*c for c in cw.values()))
    return cw, sw, lw

def cosdis(v1, v2):
    common = v1[1].intersection(v2[1])
    return sum(v1[0][ch]*v2[0][ch] for ch in common)/v1[2]/v2[2]

list_A = ['e-commerce', 'ecomme', 'e-commercy', 'ecomacy', 'E-Commerce', 'E Commerce']

list_B = ['E-Commerce']

IPA_list_a = []
IPA_list_b = []
for each in list_A:
    IPA_list_a.append(ipa.convert(each))
for each in list_B:
    IPA_list_b.append(ipa.convert(each))

for word in IPA_list_a:
    for key in IPA_list_b:
            res = cosdis(word2vec(word), word2vec(key))
            print(res)

标签: pythonpython-3.xnlpnltk

解决方案


推荐阅读