一个更复杂、更真实的例子——
from ngram import NGram
S1 = "bronchopneumonia"
S2 = "pneumonia"
N = 1
print(NGram.compare(S1,S2,N))
结果是 0.5625
公式:
(x-y)/x
在哪里:
x = total number of distinct ngrams across the two strings
y = number of ngrams NOT shared by the two strings
计算,显示 x 和 y 的运行总计:
'b': {'bronchopneumonia': 1} x=1, y=1
'r': {'bronchopneumonia': 1} x=2, y=2
'o': {'bronchopneumonia': 3, 'pneumonia':1} x=5, y=4
'n': {'bronchopneumonia': 3, 'pneumonia':2} x=8, y=5
'c': {'bronchopneumonia': 1} x=9, y=6
'h': {'bronchopneumonia': 1} x=10, y=7
'p': {'bronchopneumonia': 1, 'pneumonia':1} x=11, y=7
'e': {'bronchopneumonia': 1, 'pneumonia':1} x=12, y=7
'u': {'bronchopneumonia': 1, 'pneumonia':1} x=13, y=7
'm': {'bronchopneumonia': 1, 'pneumonia':1} x=14, y=7
'i': {'bronchopneumonia': 1, 'pneumonia':1} x=15, y=7
'a': {'bronchopneumonia': 1, 'pneumonia':1} x=16, y=7
这给了我们:
(16-7)/16 = 9/16 = 0.5625
当您遇到在两个字符串中多次出现的 ngram 时,就会出现复杂性。在此示例中,“o”和“n”都出现了多次。
'o' appears **3 times** in 'bronchopneumonia' and **1 time** in 'pneumonia'
x now moves to 5 (from 2) and y moves to 4 (from 2).
I think, x is incremented by 3 because it is the higher number???
And I think, y is incremented by 2 because 3-1=2???
同样对于'n':
'n' appears **3 times** in 'bronchopneumonia' and **2 times** in 'pneumonia'
x now moves to 8 (from 5) and y moves to 5 (from 4).
I think, x is incremented by 3 because it is the higher number???
And I think, y is incremented by 1 because 3-2=1???
这是我对其工作原理的理解,我不是 Python 专家,所以我可能误解了源代码中的某些内容。