Skip to content

Commit 1a40be4

Browse files
committed
Add documentation for wn.compat.sensekey
1 parent 565c8e6 commit 1a40be4

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

docs/api/wn.compat.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
wn.compat
2+
=========
3+
4+
Compatibility modules for Wn.
5+
6+
This subpackage is a namespace for compatibility modules when working
7+
with particular lexicons. Wn is designed to be agnostic to the
8+
language or lexicon and not favor one over the other (with the
9+
exception of :mod:`wn.morphy`, which is English-specific). However,
10+
there are some kinds of functionality that would be useful to
11+
include in Wn, even if they don't generalize to all lexicons.
12+
13+
Included modules
14+
----------------
15+
16+
.. toctree::
17+
:maxdepth: 1
18+
19+
wn.compat.sensekey.rst
20+

docs/api/wn.compat.sensekey.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
wn.compat.sensekey
2+
==================
3+
4+
.. automodule:: wn.compat.sensekey
5+
6+
.. autofunction:: sense_key_getter
7+
.. autofunction:: sense_getter

docs/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ Contents
5757
:hidden:
5858

5959
api/wn.rst
60+
api/wn.compat.rst
61+
api/wn.compat.sensekey.rst
6062
api/wn.constants.rst
6163
api/wn.ic.rst
6264
api/wn.lmf.rst

wn/compat/sensekey.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,27 @@ def _escape_oewn_sense_key(sense_key: str) -> str:
6666

6767

6868
def sense_key_getter(lexicon: str) -> SensekeyGetter:
69+
"""Return a function that gets sense keys from senses.
70+
71+
The *lexicon* argument determines how the function will retrieve
72+
the sense key. For OMW and EWN 2019/2020 lexicons, the sense key
73+
is in the ``identifier`` metadata. For OEWN 2021+ lexicons, the
74+
sense ID is an escaped form of the sense key. For any unsupported
75+
lexicon, an error is raised.
76+
77+
The function that is returned accepts one argument, a
78+
:class:`wn.Sense` (ideally from the same lexicon specified in the
79+
*lexicon* argument), and returns a :class:`str` if the sense key
80+
exists in the lexicon or :data:`None` otherwise.
81+
82+
>>> import wn
83+
>>> from wn.compat import sensekey
84+
>>> oewn = wn.Wordnet("oewn:2024")
85+
>>> get_sense_key = sensekey.sense_key_getter("oewn:2024")
86+
>>> get_sense_key(oewn.senses("alabaster")[0])
87+
'alabaster%3:01:00::'
88+
89+
"""
6990
if lexicon in METADATA_LEXICONS:
7091

7192
def getter(sense: wn.Sense) -> Optional[str]:
@@ -89,6 +110,32 @@ def getter(sense: wn.Sense) -> Optional[str]:
89110

90111

91112
def sense_getter(lexicon: str, wordnet: Optional[wn.Wordnet] = None) -> SenseGetter:
113+
"""Return a function that gets the sense for a sense key.
114+
115+
The *lexicon* argument determines how the function will retrieve
116+
the sense. For OMW and EWN 2019/2020 lexicons, the sense key is in
117+
the ``identifier`` metadata and a mapping of sense keys to senses
118+
will be created internally. For OEWN 2021+ lexicons the sense ID
119+
is an escaped form of the sense key, so the given sense key is
120+
escaped, then the sense is retrieved by :meth:`wn.Wordnet.sense`.
121+
For any unsupported lexicon, an error is raised.
122+
123+
The optional *wordnet* object is used as the source of the
124+
returned :class:`wn.Sense` objects. If none is provided, a new
125+
:class:`wn.Wordnet` object is created using the *lexicon*
126+
argument.
127+
128+
The function that is returned accepts one argument, a :class:`str`
129+
of the sense key, and returns a :class:`wn.Sense` if the sense key
130+
exists in the lexicon or :data:`None` otherwise.
131+
132+
>>> import wn
133+
>>> from wn.compat import sensekey
134+
>>> get_sense = sensekey.sense_getter("oewn:2024")
135+
>>> get_sense("alabaster%3:01:00::")
136+
Sense('oewn-alabaster__3.01.00..')
137+
138+
"""
92139
if wordnet is None:
93140
wordnet = wn.Wordnet(lexicon)
94141

0 commit comments

Comments
 (0)