python模糊搜索列表对象_模糊搜索Python

以下是不理想的,但它应该让你开始。它使用nltk首先将文本拆分为单词,然后生成一个包含所有单词词干的集合,过滤任何停止词。它对示例文本和示例查询都是这样做的。在

如果两个集合的交集包含查询中的所有单词,则将其视为匹配项。在import nltk

from nltk.stem import PorterStemmer

from nltk.tokenize import word_tokenize

from nltk.corpus import stopwords

stop_words = stopwords.words('english')

ps = PorterStemmer()

def get_word_set(text):

return set(ps.stem(word) for word in word_tokenize(text) if word not in stop_words)

text1 = "The arterial high blood pressure may engage the prognosis for survival of the patient as a result of complications. TENSTATEN enters within the framework of a preventive treatment(processing). His(Her,Its) report(relationship) efficiency / effects unwanted is important. diuretics, medicine of first intention of which TENSTATEN, is. The therapeutic alternatives are very numerous."

text2 = "The arterial high blood pressure may engage the for survival of the patient as a result of complications. TENSTATEN enters within the framework of a preventive treatment(processing). His(Her,Its) report(relationship) efficiency / effects unwanted is important. diuretics, medicine of first intention of which TENSTATEN, is. The therapeutic alternatives are very numerous."

query = "engage the prognosis for survival"

set_query = get_word_set(query)

for text in [text1, text2]:

set_text = get_word_set(text)

intersection = set_query & set_text

print "Query:", set_query

print "Test:", set_text

print "Intersection:", intersection

print "Match:", len(intersection) == len(set_query)

print

脚本提供两个文本,一个通过,另一个不通过,它生成以下输出以显示它在做什么:

^{pr2}$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值