Debian Codegraph @

docs/api/wn.ic.rst

raw
1
2 wn.ic
3 =====
4
5 .. automodule:: wn.ic
6
7 The mathematical formulae for information content are defined in
8 `Formal Description`_, and the corresponding Python API function are
9 described in `Calculating Information Content`_. These functions
10 require information content weights obtained either by `computing them
11 from a corpus <Computing Corpus Weights_>`_, or by `loading
12 pre-computed weights from a file <Reading Pre-computed Information
13 Content Files_>`_.
14
15 .. note::
16
17 The term *information content* can be ambiguous. It often, and most
18 accurately, refers to the result of the :func:`information_content`
19 function (:math:`\text{IC}(c)` in the mathematical notation), but
20 is also sometimes used to refer to the corpus frequencies/weights
21 (:math:`\text{freq}(c)` in the mathematical notation) returned by
22 :func:`load` or :func:`compute`, as these weights are the basis of
23 the value computed by :func:`information_content`. The Wn
24 documentation tries to consistently refer to former as the
25 *information content value*, or just *information content*, and the
26 latter as *information content weights*, or *weights*.
27
28
29 Formal Description
30 ------------------
31
32 The Information Content (IC) of a concept (synset) is a measure of its
33 specificity computed from the wordnet's taxonomy structure and corpus
34 frequencies. It is defined by Resnik 1995 ([RES95]_), following
35 information theory, as the negative log-probability of a concept:
36
37 .. math::
38
39 \text{IC}(c) = -\log{p(c)}
40
41 A concept's probability is the empirical probability over a corpus:
42
43 .. math::
44
45 p(c) = \frac{\text{freq}(c)}{N}
46
47 Here, :math:`N` is the total count of words of the same category as
48 concept :math:`c` ([RES95]_ only considered nouns) where each word has
49 some representation in the wordnet, and :math:`\text{freq}` is defined
50 as the sum of corpus counts of words in :math:`\text{words}(c)`, which
51 is the set of words subsumed by concept :math:`c`:
52
53 .. math::
54
55 \text{freq}(c) = \sum_{w \in \text{words}(c)}{\text{count}(w)}
56
57 It is common for :math:`\text{freq}` to not contain actual frequencies
58 but instead weights distributed evenly among the synsets for a
59 word. These weights are calculated as the word frequency divided by
60 the number of synsets for the word:
61
62 .. math::
63
64 \text{freq}_{\text{distributed}}(c)
65 = \sum_{w \in \text{words}(c)}{\frac{\text{count}(w)}{|\text{synsets}(w)|}}
66
67 .. [RES95] Resnik, Philip. "Using information content to evaluate
68 semantic similarity." In Proceedings of the 14th International
69 Joint Conference on Artificial Intelligence (IJCAI-95), Montreal,
70 Canada, pp. 448-453. 1995.
71
72
73 Example
74 -------
75
76 In the Princeton WordNet 3.0 (hereafter *WordNet*, but note that the
77 equivalent lexicon in Wn is the *OMW English Wordnet based on WordNet
78 3.0* with specifier ``omw-en:1.4``), the frequency of a concept like
79 **stone fruit** is not just the number of occurrences of *stone
80 fruit*, but also includes the counts of the words for its hyponyms
81 (*almond*, *olive*, etc.) and other taxonomic descendants (*Jordan
82 almond*, *green olive*, etc.). The word *almond* has two synsets: one
83 for the fruit or nut, another for the plant. Thus, if the word
84 *almond* is encountered :math:`n` times in a corpus, then the weight
85 (either the frequency :math:`n` or distributed weight
86 :math:`\frac{n}{2}`) is added to the total weights for both synsets
87 and to those of their ancestors, but not for descendant synsets, such
88 as for **Jordan almond**. The fruit/nut synset of almond has two
89 hypernym paths which converge on **fruit**:
90
91 1. **almond** ⊃ **stone fruit** ⊃ **fruit**
92 2. **almond** ⊃ **nut** ⊃ **seed** ⊃ **fruit**
93
94 The weight is added to each ancestor (**stone fruit**, **nut**,
95 **seed**, **fruit**, ...) once. That is, the weight is not added to
96 the convergent ancestor for **fruit** twice, but only once.
97
98
99 Calculating Information Content
100 -------------------------------
101
102 .. autofunction:: information_content
103 .. autofunction:: synset_probability
104
105
106 Computing Corpus Weights
107 ------------------------
108
109 If pre-computed weights are not available for a wordnet or for some
110 domain, they can be computed given a corpus and a wordnet.
111
112 The corpus is an iterable of words. For large corpora it may help to
113 use a generator for this iterable, but the entire vocabulary (i.e.,
114 unique words and counts) will be held at once in memory. Multi-word
115 expressions are also possible if they exist in the wordnet. For
116 instance, WordNet has *stone fruit*, with a single space delimiting
117 the words, as an entry.
118
119 The :class:`wn.Wordnet` object must be instantiated with a single
120 lexicon, although it may have expand-lexicons for relation
121 traversal. For best results, the wordnet should use a lemmatizer to
122 help it deal with inflected wordforms from running text.
123
124 .. autofunction:: compute
125
126
127 Reading Pre-computed Information Content Files
128 ----------------------------------------------
129
130 The :func:`load` function reads pre-computed information content
131 weights files as used by the `WordNet::Similarity
132 <http://wn-similarity.sourceforge.net/>`_ Perl module or the `NLTK
133 <http://www.nltk.org/>`_ Python package. These files are computed for
134 a specific version of a wordnet using the synset offsets from the
135 `WNDB <https://wordnet.princeton.edu/documentation/wndb5wn>`_ format,
136 which Wn does not use. These offsets therefore must be converted into
137 an identifier that matches those used by the wordnet. By default,
138 :func:`load` uses the lexicon identifier from its *wordnet* argument
139 with synset offsets (padded with 0s to make 8 digits) and
140 parts-of-speech from the weights file to format an identifier, such as
141 ``omw-en-00001174-n``. For wordnets that use a different identifier
142 scheme, the *get_synset_id* parameter of :func:`load` can be given a
143 callable created with :func:`wn.util.synset_id_formatter`. It can also
144 be given another callable with the same signature as shown below:
145
146 .. code-block:: python
147
148 get_synset_id(*, offset: int, pos: str) -> str
149
150
151 When loading pre-computed information content files, it is recommended
152 to use the ones with smoothing (i.e., ``*-add1.dat`` or
153 ``*-resnik-add1.dat``) to avoid math domain errors when computing the
154 information content value.
155
156 .. warning::
157
158 The weights files are only valid for the version of wordnet for
159 which they were created. Files created for WordNet 3.0 do not work
160 for WordNet 3.1 because the offsets used in its identifiers are
161 different, although the *get_synset_id* parameter of :func:`load`
162 could be given a function that performs a suitable mapping. Some
163 `Open Multilingual Wordnet <https://github.com/omwn/omw-data>`_
164 wordnets use the WordNet 3.0 offsets in their identifiers and can
165 therefore technically use the weights, but this usage is
166 discouraged because the distributional properties of text in
167 another language and the structure of the other wordnet will not be
168 compatible with that of the English WordNet. For these cases, it is
169 recommended to compute new weights using :func:`compute`.
170
171 .. autofunction:: load