Debian Codegraph @

docs/guides/nltk-migration.rst

raw
1 Migrating from the NLTK
2 =======================
3
4 This guide is for users of the `NLTK <https://www.nltk.org/>`_\ 's
5 ``nltk.corpus.wordnet`` module who are migrating to Wn. It is not
6 guaranteed that Wn will produce the same results as the NLTK's module,
7 but with some care its behavior can be very similar.
8
9 Overview
10 --------
11
12 One important thing to note is that Wn will search all wordnets in the
13 database by default where the NLTK would only search the English.
14
15 >>> from nltk.corpus import wordnet as nltk_wn
16 >>> nltk_wn.synsets('chat') # only English
17 >>> nltk_wn.synsets('chat', lang='fra') # only French
18 >>> import wn
19 >>> wn.synsets('chat') # all wordnets
20 >>> wn.synsets('chat', lang='fr') # only French
21
22 With Wn it helps to create a :class:`wn.Wordnet` object to pre-filter
23 the results by language or lexicon.
24
25 >>> en = wn.Wordnet('omw-en:1.4')
26 >>> en.synsets('chat') # only the OMW English Wordnet
27
28 Equivalent Operations
29 ---------------------
30
31 The following table lists equivalent API calls for the NLTK's wordnet
32 module and Wn assuming the respective modules have been instantiated
33 (in separate Python sessions) as follows:
34
35 NLTK:
36
37 >>> from nltk.corpus import wordnet as wn
38 >>> ss = wn.synsets("chat", pos="v")[0]
39
40 Wn:
41
42 >>> import wn
43 >>> en = wn.Wordnet('omw-en:1.4')
44 >>> ss = en.synsets("chat", pos="v")[0]
45
46 .. default-role:: python
47
48 Primary Queries
49 '''''''''''''''
50
51 ========================================= ===============================================
52 NLTK Wn
53 ========================================= ===============================================
54 `wn.langs()` `[lex.language for lex in wn.lexicons()]`
55 `wn.lemmas("chat")` --
56 -- `en.words("chat")`
57 -- `en.senses("chat")`
58 `wn.synsets("chat")` `en.synsets("chat")`
59 `wn.synsets("chat", pos="v")` `en.synsets("chat", pos="v")`
60 `wn.all_synsets()` `en.synsets()`
61 `wn.all_synsets(pos="v")` `en.synsets(pos="v")`
62 ========================================= ===============================================
63
64 Synsets -- Basic
65 ''''''''''''''''
66
67 =================== =================
68 NLTK Wn
69 =================== =================
70 `ss.lemmas()` --
71 -- `ss.senses()`
72 -- `ss.words()`
73 `ss.lemmas_names()` `ss.lemmas()`
74 `ss.definition()` `ss.definition()`
75 `ss.examples()` `ss.examples()`
76 `ss.pos()` `ss.pos`
77 =================== =================
78
79 Synsets -- Relations
80 ''''''''''''''''''''
81
82 ========================================== =====================================
83 NLTK Wn
84 ========================================== =====================================
85 `ss.hypernyms()` `ss.get_related("hypernym")`
86 `ss.instance_hypernyms()` `ss.get_related("instance_hypernym")`
87 `ss.hypernyms() + ss.instance_hypernyms()` `ss.hypernyms()`
88 `ss.hyponyms()` `ss.get_related("hyponym")`
89 `ss.member_holonyms()` `ss.get_related("holo_member")`
90 `ss.member_meronyms()` `ss.get_related("mero_member")`
91 `ss.closure(lambda x: x.hypernyms())` `ss.closure("hypernym")`
92 ========================================== =====================================
93
94 Synsets -- Taxonomic Structure
95 ''''''''''''''''''''''''''''''
96
97 ================================ =========================================================
98 NLTK Wn
99 ================================ =========================================================
100 `ss.min_depth()` `ss.min_depth()`
101 `ss.max_depth()` `ss.max_depth()`
102 `ss.hypernym_paths()` `[list(reversed([ss] + p)) for p in ss.hypernym_paths()]`
103 `ss.common_hypernyms(ss)` `ss.common_hypernyms(ss)`
104 `ss.lowest_common_hypernyms(ss)` `ss.lowest_common_hypernyms(ss)`
105 `ss.shortest_path_distance(ss)` `len(ss.shortest_path(ss))`
106 ================================ =========================================================
107
108 .. reset default role
109 .. default-role::
110
111 (these tables are incomplete)