英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论