【问题标题】:Match the content of sentence/sequence in python在python中匹配句子/序列的内容
【发布时间】:2022-11-25 00:05:09
【问题描述】:

假设我们有 2 个单词序列

sentence1 = 'Ram is eating'

sentence2 = 'is Ram  eating'

如何获得这样的 2 个序列的匹配百分比。 difflib sequenceMatcher 逐个字母匹配。在这些情况下找到匹配百分比的任何方法。

like match % in above case should be 100% as each word of 2 sentences 是平等的。 如果sentence2 = 'is Ram' 那么 sentence1 和 sentence 2 之间的 match% 将是 2/3 = 66.66%

match% = (number of words matching in sentence1 and sentence2 irrespective of position/total number of words in sentence1)*100

【问题讨论】:

  • 请定义匹配百分比,它是什么?
  • @svfat 我已经用示例进行了编辑,希望能清除匹配%

标签: python nlp sequence sentence difflib


【解决方案1】:

如何将字符串转换为列表并找到匹配百分比。

sentence1 = 'Ram is eating'
sentence2 = 'is Ram  eating'

sentence1 = sentence1.split()
sentence2 = sentence2.split()

per = len(set(sentence1) & set(sentence2))  
result = per/len(sentence1)
print (f'{result *100}% matched')
   

给#

100.0% matched

案例二

sentence1 = 'Ram is eating' 
sentence2 = 'is Ram'

sentence1 = sentence1.split()
sentence2 = sentence2.split()

per = len(set(sentence1) & set(sentence2))
result = per/len(sentence1)
print (f'{result *100}% matched')
       

给#

66.66666666666666% matched

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多