2

使用 Python ngram。我的代码如下所示:

from ngram import NGram
print(NGram.compare("coat","hat",N=1))

结果是0.4,计算如下(我认为!):

(a-b)/a

在哪里:

a = total number of distinct ngrams across the two strings
b = number of ngrams NOT shared by the two strings

插入我的代码中的值,这等于:

(5-3)/5 = 2/5 = 0.4

如果我将代码更改为:

from ngram import NGram
print(NGram.compare("coata","hata",N=1))

结果是 0.5,我不确定这个答案是如何从我上面写的公式得出的。

(6-3)/6 

这给出了 0.5,但是两个字符串中真的有 6 个不同的 ngram 吗?

任何人都可以对此有所了解吗?

4

2 回答 2

1

在“hata”、“coat”示例中,克看起来像这样:

{'a': {'hata': 2, 'coata': 2}, 
'h': {'hata': 1}, 
'c': {'coata': 1}, 
't': {'hata': 1, 'coata': 1}, 
'o': {'coata': 1}}

因此,它使'a'.

因此 3 出 6。

于 2012-12-28T00:52:20.593 回答
0

一个更复杂、更真实的例子——

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 专家,所以我可能误解了源代码中的某些内容。

于 2012-12-28T12:42:12.727 回答