在Python中找到句子中单词数量的差异。

huangapple go评论62阅读模式
英文:

Finding the difference in number of words in a sentence in python

问题

在这里,我们可以看到“Ref”和“Hyp”有3个不同的单词,因为“Q R Code”在“Hyp”中不存在。如果Python中有任何内置函数可以检查这一点并输出3作为结果,那么可以吗?

英文:

Say I have 2 sentences,

Ref: Q R CODE SCANNER APP EXIT KARE 
Hyp: WORKOUTS SCANNER APP EXIT KARE 

Here we can see that the Ref has 3 different words from the Hyp, since the Q R Code is not present in Hyp. If there is any built-in function in Python that will check this and output 3 as a result?

答案1

得分: 1

这里是一个使用集合的简单示例:

ref = "Q R CODE SCANNER APP EXIT KARE"
hyp = "WORKOUTS SCANNER APP EXIT KARE"

ref_set = set(ref.split())
hyp_set = set(hyp.split())

print(len(ref_set - hyp_set))  # 3

请注意,这忽略了单词的顺序,以及忽略了重复的单词。

英文:

Here's a simple example using sets:

ref = "Q R CODE SCANNER APP EXIT KARE"
hyp = "WORKOUTS SCANNER APP EXIT KARE"

ref_set = set(ref.split())
hyp_set = set(hyp.split())

print(len(ref_set - hyp_set))  # 3

Note that this ignores the order of words, as well as ignoring duplicate words.

huangapple
  • 本文由 发表于 2023年2月16日 16:21:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定