以下是不理想,但它應該讓你開始。它首先使用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
該腳本提供兩個文本,一個通行證和其他沒有,它產生以下輸出向您展示它在做什麼:
Query: set([u'prognosi', u'engag', u'surviv'])
Test: set([u'medicin', u'prevent', u'effici', u'engag', u'Her', u'process', u'within', u'surviv', u'high', u'pressur', u'result', u'framework', u'diuret', u')', u'(', u',', u'/', u'.', u'numer', u'Hi', u'treatment', u'import', u'complic', u'altern', u'patient', u'relationship', u'may', u'arteri', u'effect', u'prognosi', u'intent', u'blood', u'report', u'The', u'TENSTATEN', u'unwant', u'It', u'therapeut', u'enter', u'first'])
Intersection: set([u'prognosi', u'engag', u'surviv'])
Match: True
Query: set([u'prognosi', u'engag', u'surviv'])
Test: set([u'medicin', u'prevent', u'effici', u'engag', u'Her', u'process', u'within', u'surviv', u'high', u'pressur', u'result', u'diuret', u')', u'(', u',', u'/', u'.', u'numer', u'Hi', u'treatment', u'import', u'complic', u'altern', u'patient', u'relationship', u'may', u'arteri', u'effect', u'framework', u'intent', u'blood', u'report', u'The', u'TENSTATEN', u'unwant', u'It', u'therapeut', u'enter', u'first'])
Intersection: set([u'engag', u'surviv'])
Match: False
即時通訊新的在這裏,但我認爲這應該解決您的問題:http://stackoverflow.com/questions/30449452/python-fuzzy-text-search?rq=1 –
看看[gensim](https:/ /radimrehurek.com/gensim/index.html),特別是[相似部分](https://radimrehurek.com/gensim/tut3.html)。 – Jan