获取列表中最匹配的句子

2024-10-01 02:24:40 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在另一个句子中找到最匹配的句子

matchSentence = ["weather in", "weather at", "weather on"]
sentence = "weather on monday"

for item in matchSentence:
    ''' here get the index of the `item` 
    if all the words are in the `item` is in the `sentence` 
    '''

我正在寻找一个函数,它将检查sentence中是否存在所有单词

Desired result is: 2


Tags: theinforgetindexhereison
3条回答

找到最相似句子的一种方法是计算每个单词在目标句子中出现的次数

matchSentence = ["weather in", "weather at", "weather on"]
targetSentence = "weather on monday"
targetSentence_words = targetSentence.split(" ")

mostSimilarSentence = matchSentence[0]
mostSimilarSentenceScore = 0

for searchSentence in matchSentence:
    similarityScore = 0
    for word in searchSentence.split(" "):
        if word in targetSentence_words:
            similarityScore += 1

    print(f"Sentence: '{searchSentence}' got score: {similarityScore}")

    if similarityScore > mostSimilarSentenceScore:
        mostSimilarSentence = searchSentence
        mostSimilarSentenceScore = similarityScore

print(f"Most similar sentence: {mostSimilarSentence}")
print(f"Most similar sentence score: {mostSimilarSentenceScore}")

您可以使用in运算符:

matchSentence = ["weather in", "weather at", "weather on"]
sentence = "weather on monday"

for item in matchSentence:
    if item in sentence:
        print(matchSentence.index(item))

输出:

2

但它在很多情况下都不起作用,比如

matchSentence = ["weather's on", "weather is very hot at", "leather on"]
sentence = "weather on monday"



您可以将模块^{}用于以下情况:

第一轮:

from difflib import SequenceMatcher

print(SequenceMatcher(None, "abc", "abc").ratio())

输出:

1

第二轮:

from difflib import SequenceMatcher

print(SequenceMatcher(None, "efg", "abc").ratio())

输出:

0

如您所见,1表示尽可能多的相似性(相同),而0表示最少的相似性(完全没有公共字符)

matchSentence = ["weather in", "weather at", "weather on"]
sentence = "weather on monday"

maxCount = 0
maxCntInd = -1
words1 = sentence.split()  # list of all words in sentence
wordSet1 = set(words1)

for item in matchSentence:
    ''' here get the index of the `item`
    if all the words are in the `item.split()` is in the `sentence`
    '''
    words2 = item.split()  # list of all words in item
    wordSet2 = set(words2)

    commonWords = len(wordSet2.intersection(wordSet1))
    if commonWords >= maxCount:
        maxCount = commonWords
        maxCntInd = matchSentence.index(item)

print(maxCntInd)

相关问题 更多 >