我浏览过许多库(例如 whoosh/nltk)和概念(例如 word net)。
但是我无法解决我的问题。我不确定是否可以找到一个库,或者我必须使用上述资源来构建它。
问题: 我的场景是我必须搜索关键词。 假设我有“销售文档”/“采购文档”等关键词,并且必须在一本 10-15 页的小书中搜索它们。
问题是: 现在它们也可以写成“销售应该记录在案”或“公司销售应该写在文本文件中”。 (对于销售文档 - 关键字)这里有没有办法或者我必须构建一些东西?
POS标签的代码如下。如果没有可用的库,我将不得不继续这样做。
from nltk.tag import pos_tag
from nltk.tokenize import word_tokenize
from pandas import Series
import nltk
from nltk.corpus import wordnet
def tag(x):
return pos_tag(word_tokenize(x))
synonyms = []
antonyms = []
for syn in wordnet.synsets("Sales document"):
#print("Down2")
print (syn)
#print("Down")
for l in syn.lemmas():
print(" \n")
print(l)
synonyms.append(l.name())
if l.antonyms():
antonyms.append(l.antonyms()[0].name())
print(set(synonyms))
print(set(antonyms))
for i in synonyms:
print(tag(i))
更新: 我们继续制作了一个 python 程序 - 请随意 fork 它。 (双关语) 此外,Git Dhund 现在非常凌乱,一旦完成就会清理它。 目前它仍处于开发阶段。
这是link 。
最佳答案
要匹配诸如“应记录销售”之类的情况,可以通过增加 Whoosh 的 Phrase 查询对象中的 slop
参数来完成。
whoosh.query.Phrase(fieldname, words, slop=1, boost=1.0, char_ranges=None) slop – the number of words allowed between each “word” in the phrase; the default of 1 means the phrase must match exactly.
您还可以在查询中定义斜率,如下所示:“销售应记录在案”~5
为了匹配第二个示例“公司销售应写入文本文件”,这需要对您的文本进行语义处理。 Whoosh 有一个低级实现 wordnet thesaurus允许您索引同义词,但它只有一个单词的同义词。
关于python - Python 中的 NLP 短语搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50591856/