2

たとえば、大きなサンプル テキストがあります。

「動脈性高血圧は、合併症の結果、患者の生存予後を左右する可能性があります。テンスタテンは、予防的治療(処理)の枠組みに入ります。彼(彼女、その)報告(関係)の効率/不要な影響が重要です。 . 利尿薬、テンスタテンが最初に意図した薬. 治療上の代替手段は非常に多数あります.

そして、テキストで「生存の予後を確認する」かどうかを検出しようとしていますが、あいまいな方法です。たとえば、「生存の予言に関与している」も肯定的な答えを返さなければなりません。

fuzzywuzzy、nltk、および新しい正規表現ファジー関数を調べましたが、実行する方法が見つかりませんでした:

if [anything similar (>90%) to "that sentence"] in mybigtext:
    print True
4

3 に答える 3

1

モジュールを使用してregex、まず文で分割し、ファジー パターンが文に含まれているかどうかをテストします。

tgt="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."

for sentence in regex.split(r'(?<=[.?!;])\s+(?=\p{Lu})', tgt):
    pat=r'(?e)((?:has engage the progronosis of survival){e<%i})' 
    pat=pat % int(len(pat)/5)
    m=regex.search(pat, sentence)
    if m:
        print "'{}'\n\tfuzzy matches\n'{}'\n\twith \n{} substitutions, {} insertions, {} deletions".format(pat,m.group(1), *m.fuzzy_counts)

版画:

'(?e)((?:has engage the progronosis of survival){e<10})'
    fuzzy matches
'may engage the prognosis for survival'
    with 
3 substitutions, 1 insertions, 2 deletions
于 2016-02-29T21:41:19.050 に答える
1

以下は理想的ではありませんが、開始する必要があります。最初にnltkテキストを単語に分割し、次にすべての単語の語幹を含むセットを生成して、ストップ ワードをフィルタリングします。これは、サンプル テキストとサンプル クエリの両方に対して行われます。

2 つのセットの共通部分にクエリ内のすべての単語が含まれている場合、一致したと見なされます。

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

スクリプトは 2 つのテキストを提供します。1 つはパスし、もう 1 つはパスしません。実行内容を示す次の出力が生成されます。

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
于 2016-02-29T20:32:08.403 に答える
0

その下に、テキスト内に単語が含まれている場合に一致を表示する関数があります。テキスト内の完全なフレーズをチェックするように即興で作成できます。

ここに私が作った関数があります:

def FuzzySearch(text, phrase):
    """Check if word in phrase is contained in text"""
    phrases = phrase.split(" ")

    for x in range(len(phrases)):
        if phrases[x] in text:
            print("Match! Found " + phrases[x] + " in text")
        else:
            continue
于 2016-02-29T17:52:16.980 に答える