Не должно быть никакого «сбоя», как при возникновении подобной ошибки.
Тем не менее, он возвращает None
, например,
from nltk.corpus import wordnet as wn
sl1 = wn.synsets("dog")
sl2 = wn.synsets("cat")
for x in sl1:
for y in sl2:
print(x, y, x.wup_similarity(y))
[out]:
Synset('dog.n.01') Synset('cat.n.01') 0.8571428571428571
Synset('dog.n.01') Synset('guy.n.01') 0.631578947368421
Synset('dog.n.01') Synset('cat.n.03') 0.631578947368421
Synset('dog.n.01') Synset('kat.n.01') 0.25
Synset('dog.n.01') Synset('cat-o'-nine-tails.n.01') 0.42105263157894735
Synset('dog.n.01') Synset('caterpillar.n.02') 0.4
Synset('dog.n.01') Synset('big_cat.n.01') 0.8571428571428571
Synset('dog.n.01') Synset('computerized_tomography.n.01') 0.1
Synset('dog.n.01') Synset('cat.v.01') None
Synset('dog.n.01') Synset('vomit.v.01') None
Synset('frump.n.01') Synset('cat.n.01') 0.48
Synset('frump.n.01') Synset('guy.n.01') 0.5714285714285714
Synset('frump.n.01') Synset('cat.n.03') 0.5714285714285714
Synset('frump.n.01') Synset('kat.n.01') 0.4
...
Вы можете просто проверить None и присвоить его нулю, например,
sl1 = wn.synsets("dog")
sl2 = wn.synsets("cat")
for x in sl1:
for y in sl2:
score = x.wup_similarity(y)
score = score if score else 0
print(x, y, score)
[out]:
Synset('dog.n.01') Synset('cat.n.01') 0.8571428571428571
Synset('dog.n.01') Synset('guy.n.01') 0.631578947368421
Synset('dog.n.01') Synset('cat.n.03') 0.631578947368421
Synset('dog.n.01') Synset('kat.n.01') 0.25
Synset('dog.n.01') Synset('cat-o'-nine-tails.n.01') 0.42105263157894735
Synset('dog.n.01') Synset('caterpillar.n.02') 0.4
Synset('dog.n.01') Synset('big_cat.n.01') 0.8571428571428571
Synset('dog.n.01') Synset('computerized_tomography.n.01') 0.1
Synset('dog.n.01') Synset('cat.v.01') 0
Synset('dog.n.01') Synset('vomit.v.01') 0
Synset('frump.n.01') Synset('cat.n.01') 0.48
Synset('frump.n.01') Synset('guy.n.01') 0.5714285714285714
Synset('frump.n.01') Synset('cat.n.03') 0.5714285714285714
Synset('frump.n.01') Synset('kat.n.01') 0.4