python - 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)
解决方案
推荐阅读
- python - 使用 SVM 的 coef_ 函数的特征重要性
- python - Kivy 屏幕管理器无法在屏幕之间切换
- firebase - 执行 Firebase 规则时遇到问题
- javascript - Javascript 和 Django Rest Framework - 无法读取属性
- spring-boot - JdbcBatchItemWriter - setSql - 如何修复时间戳类型不匹配
- javascript - 在 nodejs 中操作 html 文档服务器端?
- variables - Ansible 动态可变分辨率
- ios - UITableViewCell 中的平移手势,可以将图像视图移动到整个 tableview 而不进入单元格下方
- php - PHP:寻求有效的结构来遍历数组以进行匹配
- c++ - 如何将 clang-tidy 集成到 CMake 和 GCC?