Сопоставление набора слов с набором предложений в Python NLP - PullRequest
0 голосов
/ 15 октября 2019

У меня есть сценарий использования, в котором я хочу сопоставить один список слов со списком предложений и вывести наиболее релевантные предложения

Я работаю в Python. Я уже пробовал использовать KMeans, где мы группируем наш набор документов в кластеры, а затем предсказываем предложение, в какой структуре он находится. Но в моем случае у меня уже есть список доступных слов.

def getMostRelevantSentences():
    Sentences = ["This is the most beautiful place in the world.",
            "This man has more skills to show in cricket than any other game.",
            "Hi there! how was your ladakh trip last month?",
            "Isn’t cricket supposed to be a team sport? I feel people should decide first whether cricket is a team game or an individual sport."]

    words = ["cricket","sports","team","play","match"]

    #TODO: now this should return me the 2nd and last item from the Sentences list as the words list mostly matches with them

Так что из приведенного выше кода я хочу вернуть предложения, которые точно соответствуют предоставленным словам. Я не хочу использовать контролируемое машинное обучение здесь. Любая помощь будет оценена.

1 Ответ

0 голосов
/ 15 октября 2019

Итак, наконец, я использовал эту супер-библиотеку под названием gensim для генерации подобия.

import gensim
from nltk.tokenize import word_tokenize

def getSimilarityScore(raw_documents, words):
    gen_docs = [[w.lower() for w in word_tokenize(text)] 
            for text in raw_documents]
    dictionary = gensim.corpora.Dictionary(gen_docs)
    corpus = [dictionary.doc2bow(gen_doc) for gen_doc in gen_docs]
    tf_idf = gensim.models.TfidfModel(corpus)
    sims = gensim.similarities.Similarity('/usr/workdir',tf_idf[corpus],
                                      num_features=len(dictionary))

    query_doc_bow = dictionary.doc2bow(words)
    query_doc_tf_idf = tf_idf[query_doc_bow]

    return sims[query_doc_tf_idf]

Вы можете использовать этот метод как:


Sentences = ["This is the most beautiful place in the world.",
            "This man has more skills to show in cricket than any other game.",
            "Hi there! how was your ladakh trip last month?",
            "Isn’t cricket supposed to be a team sport? I feel people should decide first whether cricket is a team game or an individual sport."]

words = ["cricket","sports","team","play","match"]

words_lower = [w.lower() for w in words]

getSimilarityScore(Sentences,words_lower)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...