| 1 |
wn.similarity |
| 2 |
============= |
| 3 |
|
| 4 |
.. automodule:: wn.similarity |
| 5 |
|
| 6 |
Taxonomy-based Metrics |
| 7 |
---------------------- |
| 8 |
|
| 9 |
The `Path <Path Similarity_>`_, `Leacock-Chodorow <Leacock-Chodorow |
| 10 |
Similarity_>`_, and `Wu-Palmer <Wu-Palmer Similarity_>`_ similarity |
| 11 |
metrics work by finding path distances in the hypernym/hyponym |
| 12 |
taxonomy. As such, they are most useful when the synsets are, in fact, |
| 13 |
arranged in a taxonomy. For the Princeton WordNet and derivative |
| 14 |
wordnets, such as the `Open English Wordnet`_ and `OMW English Wordnet |
| 15 |
based on WordNet 3.0`_ available to Wn, synsets for nouns and verbs |
| 16 |
are arranged taxonomically: the nouns mostly form a single structure |
| 17 |
with a single root while verbs form many smaller structures with many |
| 18 |
roots. Synsets for the other parts of speech do not use |
| 19 |
hypernym/hyponym relations at all. This situation may be different for |
| 20 |
other wordnet projects or future versions of the English wordnets. |
| 21 |
|
| 22 |
.. _Open English Wordnet: https://en-word.net |
| 23 |
.. _OMW English Wordnet based on WordNet 3.0: https://github.com/omwn/omw-data |
| 24 |
|
| 25 |
The similarity metrics tend to fail when the synsets are not connected |
| 26 |
by some path. When the synsets are in different parts of speech, or |
| 27 |
even in separate lexicons, this failure is acceptable and |
| 28 |
expected. But for cases like the verbs in the Princeton WordNet, it |
| 29 |
might be more useful to pretend that there is some unique root for all |
| 30 |
verbs so as to create a path connecting any two of them. For this |
| 31 |
purpose, the *simulate_root* parameter is available on the |
| 32 |
:func:`path`, :func:`lch`, and :func:`wup` functions, where it is |
| 33 |
passed on to calls to :meth:`wn.Synset.shortest_path` and |
| 34 |
:meth:`wn.Synset.lowest_common_hypernyms`. Setting *simulate_root* to |
| 35 |
:python:`True` can, however, give surprising results if the words are |
| 36 |
from a different lexicon. Currently, computing similarity for synsets |
| 37 |
from a different part of speech raises an error. |
| 38 |
|
| 39 |
|
| 40 |
Path Similarity |
| 41 |
''''''''''''''' |
| 42 |
|
| 43 |
When :math:`p` is the length of the shortest path between two synsets, |
| 44 |
the path similarity is: |
| 45 |
|
| 46 |
.. math:: |
| 47 |
|
| 48 |
\frac{1}{p + 1} |
| 49 |
|
| 50 |
The similarity score ranges between 0.0 and 1.0, where the higher the |
| 51 |
score is, the more similar the synsets are. The score is 1.0 when a |
| 52 |
synset is compared to itself, and 0.0 when there is no path between |
| 53 |
the two synsets (i.e., the path distance is infinite). |
| 54 |
|
| 55 |
.. autofunction:: path |
| 56 |
|
| 57 |
|
| 58 |
.. _leacock-chodorow-similarity: |
| 59 |
|
| 60 |
Leacock-Chodorow Similarity |
| 61 |
''''''''''''''''''''''''''' |
| 62 |
|
| 63 |
When :math:`p` is the length of the shortest path between two synsets |
| 64 |
and :math:`d` is the maximum taxonomy depth, the Leacock-Chodorow |
| 65 |
similarity is: |
| 66 |
|
| 67 |
.. math:: |
| 68 |
|
| 69 |
-\text{log}\left(\frac{p + 1}{2d}\right) |
| 70 |
|
| 71 |
.. autofunction:: lch |
| 72 |
|
| 73 |
|
| 74 |
Wu-Palmer Similarity |
| 75 |
'''''''''''''''''''' |
| 76 |
|
| 77 |
When *LCS* is the lowest common hypernym (also called "least common |
| 78 |
subsumer") between two synsets, :math:`i` is the shortest path |
| 79 |
distance from the first synset to *LCS*, :math:`j` is the shortest |
| 80 |
path distance from the second synset to *LCS*, and :math:`k` is the |
| 81 |
number of nodes (distance + 1) from *LCS* to the root node, then the |
| 82 |
Wu-Palmer similarity is: |
| 83 |
|
| 84 |
.. math:: |
| 85 |
|
| 86 |
\frac{2k}{i + j + 2k} |
| 87 |
|
| 88 |
.. autofunction:: wup |
| 89 |
|
| 90 |
|
| 91 |
Information Content-based Metrics |
| 92 |
--------------------------------- |
| 93 |
|
| 94 |
The `Resnik <Resnik Similarity_>`_, `Jiang-Conrath <Jiang-Conrath |
| 95 |
Similarity_>`_, and `Lin <Lin Similarity_>`_ similarity metrics work |
| 96 |
by computing the information content of the synsets and/or that of |
| 97 |
their lowest common hypernyms. They therefore require information |
| 98 |
content weights (see :mod:`wn.ic`), and the values returned |
| 99 |
necessarily depend on the weights used. |
| 100 |
|
| 101 |
|
| 102 |
Resnik Similarity |
| 103 |
''''''''''''''''' |
| 104 |
|
| 105 |
The Resnik similarity (`Resnik 1995 |
| 106 |
<https://arxiv.org/pdf/cmp-lg/9511007.pdf>`_) is the maximum |
| 107 |
information content value of the common subsumers (hypernym ancestors) |
| 108 |
of the two synsets. Formally it is defined as follows, where |
| 109 |
:math:`c_1` and :math:`c_2` are the two synsets being compared. |
| 110 |
|
| 111 |
.. math:: |
| 112 |
|
| 113 |
\text{max}_{c \in \text{S}(c_1, c_2)} \text{IC}(c) |
| 114 |
|
| 115 |
Since a synset's information content is always equal or greater than |
| 116 |
the information content of its hypernyms, :math:`S(c_1, c_2)` above is |
| 117 |
more efficiently computed using the lowest common hypernyms instead of |
| 118 |
all common hypernyms. |
| 119 |
|
| 120 |
.. autofunction:: res |
| 121 |
|
| 122 |
|
| 123 |
Jiang-Conrath Similarity |
| 124 |
'''''''''''''''''''''''' |
| 125 |
|
| 126 |
The Jiang-Conrath similarity metric (`Jiang and Conrath, 1997 |
| 127 |
<https://www.aclweb.org/anthology/O97-1002.pdf>`_) combines the ideas |
| 128 |
of the taxonomy-based and information content-based metrics. It is |
| 129 |
defined as follows, where :math:`c_1` and :math:`c_2` are the two |
| 130 |
synsets being compared and :math:`c_0` is the lowest common hypernym |
| 131 |
of the two with the highest information content weight: |
| 132 |
|
| 133 |
.. math:: |
| 134 |
|
| 135 |
\frac{1}{\text{IC}(c_1) + \text{IC}(c_2) - 2(\text{IC}(c_0))} |
| 136 |
|
| 137 |
This equation is the simplified form given in the paper were several |
| 138 |
parameterized terms are cancelled out because the full form is not |
| 139 |
often used in practice. |
| 140 |
|
| 141 |
There are two special cases: |
| 142 |
|
| 143 |
1. If the information content of :math:`c_0`, :math:`c_1`, and |
| 144 |
:math:`c_2` are all zero, the metric returns zero. This occurs when |
| 145 |
both :math:`c_1` and :math:`c_2` are the root node, but it can also |
| 146 |
occur if the synsets did not occur in the corpus and the smoothing |
| 147 |
value was set to zero. |
| 148 |
|
| 149 |
2. Otherwise if :math:`c_1 + c_2 = 2c_0`, the metric returns |
| 150 |
infinity. This occurs when the two synsets are the same, one is a |
| 151 |
descendant of the other, etc., such that they have the same |
| 152 |
frequency as each other and as their lowest common hypernym. |
| 153 |
|
| 154 |
.. autofunction:: jcn |
| 155 |
|
| 156 |
|
| 157 |
Lin Similarity |
| 158 |
'''''''''''''' |
| 159 |
|
| 160 |
Another formulation of information content-based similarity is the Lin |
| 161 |
metric (`Lin 1997 <https://www.aclweb.org/anthology/P97-1009.pdf>`_), |
| 162 |
which is defined as follows, where :math:`c_1` and :math:`c_2` are the |
| 163 |
two synsets being compared and :math:`c_0` is the lowest common |
| 164 |
hypernym with the highest information content weight: |
| 165 |
|
| 166 |
.. math:: |
| 167 |
|
| 168 |
\frac{2(\text{IC}(c_0))}{\text{IC}(c_1) + \text{IC}(c_0)} |
| 169 |
|
| 170 |
One special case is if either synset has an information content value |
| 171 |
of zero, in which case the metric returns zero. |
| 172 |
|
| 173 |
.. autofunction:: lin |