# Using WordNet via SWI-Prolog

**URL:** <https://swi-prolog.discourse.group/t/using-wordnet-via-swi-prolog/5064>\
**Category:** Help!\
**Created:** [February 26, 2022, 7:07am UTC](https://swi-prolog.discourse.group/t/using-wordnet-via-swi-prolog/5064 "2022-02-26T07:07:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![d-vyd](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/d-vyd/32/4191_2.png) [@d-vyd](https://swi-prolog.discourse.group/u/d-vyd)\
**Post date:** [February 26, 2022, 7:07am UTC](https://swi-prolog.discourse.group/t/using-wordnet-via-swi-prolog/5064/1 "2022-02-26T07:07:55Z")

</div>

Can anyone walk me through installing and using WordNet via SWI-Prolog? I’ve tried several sources:

> **[GitHub - ekaf/wordnet-prolog: Prolog versions of the WordNet databases](https://github.com/ekaf/wordnet-prolog)**
>
> Prolog versions of the WordNet databases. Contribute to ekaf/wordnet-prolog development by creating an account on GitHub.

> **[prologdb(5WN)](https://wordnet.princeton.edu/documentation/prologdb5wn)**
>
> Name wn\_pl - description of Prolog database files Description The files wn\_ \* .pl contain the WordNet database in a prolog-readable format. A prolog interface to WordNet is not implemented. The prolog database is very large and...

> [@Wordnet search support?](https://swi-prolog.discourse.group/t/wordnet-search-support/3452):
>
> I got Wordnet package, it seems this is just the database, right ? Is there a interface to search the database similar to wn cmd line OR web tool from Prolog ? On : you forget to mention that we have to manually download the Prolog DB (not what comes as a default package in linux) [http://wordnetcode.princeton.edu/3.0/WNprolog-3.0.tar.gz](http://wordnetcode.princeton.edu/3.0/WNprolog-3.0.tar.gz)

None of these actually shows step by step how to install and query the database. Here is a Python example of what I’m looking for [GitHub - goodmami/wn: A modern, interlingual wordnet interface for Python](https://github.com/goodmami/wn). There are many such examples floating around. How would I do it in Prolog? Do I need to consult every single \*.pl file individually before issuing a query?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [February 26, 2022, 8:00am UTC](https://swi-prolog.discourse.group/t/using-wordnet-via-swi-prolog/5064/2 "2022-02-26T08:00:52Z")

</div>

All I can say is that the package is merely a small layer around the DB that provides lazy loading and compilation to .qlf files to make loading a lot faster. This is barely needed on modern hardware with current SWI-Prolog. It used to be important 🙂 , now it is no longer essential but just a moderate improvement. For the defined relations you must examine the wordnet Prolog documentation. I do not know where it is, but it used to be findable.

---

<div class="post-metadata">

**Author:** ![d-vyd](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/d-vyd/32/4191_2.png) [@d-vyd](https://swi-prolog.discourse.group/u/d-vyd)\
**Post date:** [February 26, 2022, 8:11am UTC](https://swi-prolog.discourse.group/t/using-wordnet-via-swi-prolog/5064/3 "2022-02-26T08:11:45Z")

</div>

From [prologdb(5WN) | WordNet](https://wordnet.princeton.edu/documentation/prologdb5wn) : “The files wn\_ \* .pl contain the WordNet database in a prolog-readable format.”

So, does this mean I need to type `consult(filename)` for each Prolog file in the distribution (see photo below) or put all those consults at the top of a file?

 ![image](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/2X/c/cbb8d0f39c60479a48d8ceec7f965a12dc1abd75.png)

---

<div class="post-metadata">

**Author:** ![CapelliC](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/capellic/32/17_2.png) [@CapelliC](https://swi-prolog.discourse.group/u/CapelliC)\
**Post date:** [February 26, 2022, 9:00am UTC](https://swi-prolog.discourse.group/t/using-wordnet-via-swi-prolog/5064/4 "2022-02-26T09:00:58Z")

</div>

Maybe [this repo](https://github.com/CapelliC/wn-swipl) can help ?

I’m going to try it on my current Windows box…

---

<div class="post-metadata">

**Author:** ![d-vyd](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/d-vyd/32/4191_2.png) [@d-vyd](https://swi-prolog.discourse.group/u/d-vyd)\
**Post date:** [February 26, 2022, 10:14am UTC](https://swi-prolog.discourse.group/t/using-wordnet-via-swi-prolog/5064/5 "2022-02-26T10:14:44Z")

</div>

@CapelliC , maybe my Prolog environment is the problem? I usually run SWI-Prolog via Visual Studio Code’s VSC-Prolog extension. When I open wordnet.pl and right-click to “load document” into SWI-Prolog, I see this in the interpreter:

ERROR: d:/documents/programming/prolog/wn-swipl-master/wordnet.pl:132:48: Syntax error: Operator expected  
ERROR: d:/documents/programming/prolog/wn-swipl-master/wordnet.pl:144:29: Syntax error: Operator expected  
ERROR: d:/documents/programming/prolog/wn-swipl-master/wordnet.pl:157:92: Syntax error: Operator expected  
ERROR: d:/documents/programming/prolog/wn-swipl-master/wordnet.pl:166:34: Syntax error: Operator expected  
ERROR: d:/documents/programming/prolog/wn-swipl-master/wordnet.pl:192:27: Syntax error: Operator expected  
ERROR: d:/documents/programming/prolog/wn-swipl-master/wordnet.pl:200:19: Syntax error: Operator expected  
ERROR: d:/documents/programming/prolog/wn-swipl-master/wordnet.pl:209:49: Syntax error: Operator expected  
ERROR: d:/documents/programming/prolog/wn-swipl-master/wordnet.pl:247:24: Syntax error: Operator expected  
ERROR: Exported procedure wordnet:load\_relation/2 is not defined  
ERROR: Exported procedure wordnet:verb\_tenses\_load/0 is not defined  
ERROR: Exported procedure wordnet:make\_qlf\_full/0 is not defined  
ERROR: Exported procedure wordnet:make\_qlf\_full/1 is not defined  
ERROR: Exported procedure wordnet:load\_full\_db/0 is not defined  
ERROR: Exported procedure wordnet:compare\_subset/0 is not defined  
ERROR: Exported procedure wordnet:load\_all\_relations/1 is not defined  
true.

2 ?-

Here is a snapshot:

 ![image](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/2X/7/7e41dd9a32b626fb977f0458703f65e4a7100a0a.png)

Actually, it looks about the same when I consult the file in the SWI-Prolog IDE:

 ![image](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/2X/3/3e37d964578c17a63c0e3e2390cff5ba53e26d8f.png)

---

<div class="post-metadata">

**Author:** ![CapelliC](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/capellic/32/17_2.png) [@CapelliC](https://swi-prolog.discourse.group/u/CapelliC)\
**Post date:** [February 26, 2022, 12:37pm UTC](https://swi-prolog.discourse.group/t/using-wordnet-via-swi-prolog/5064/6 "2022-02-26T12:37:35Z")

</div>

Sorry, fixing on the go…  
Better to start with the simpler wn3\_db.pl, after defined the search path key **wn3**. For instance

```prolog
 ?- assert(user:file_search_path(wn3, 'C:\\Users\\Carlo\\test\\wordnet\\wn-swipl\\WN')).

```

Then the first run should yield

```prolog
?- gtrace,time(load_wn3_relations).
% load_wn3_relations
% schema_wn3_table(_8646,_8648)
% path_wn3_file(s,_9254)
% path_wn3_files(_9356)
% Compiling s->C:\Users\Carlo\test\wordnet\wn-swipl\WN/wn_s.qlf
Warning: c:/users/carlo/test/wordnet/wn-swipl/wn/wn_s.pl:184585:
Warning: Clauses of wn3_db:s/6 are not together in the source-file
Warning: Earlier definition at c:/users/carlo/test/wordnet/wn-swipl/wn/wn_s.pl:1
Warning: Current predicate: wn3_db:s/4
Warning: Use :- discontiguous wn3_db:s/6. to suppress this message
Warning: c:/users/carlo/test/wordnet/wn-swipl/wn/wn_s.pl:184586:
Warning: Clauses of wn3_db:s/4 are not together in the source-file
Warning: Earlier definition at c:/users/carlo/test/wordnet/wn-swipl/wn/wn_s.pl:184584
Warning: Current predicate: wn3_db:s/6
Warning: Use :- discontiguous wn3_db:s/4. to suppress this message
Warning: c:/users/carlo/test/wordnet/wn-swipl/wn/wn_s.pl:184587:
Warning: Clauses of wn3_db:s/6 are not together in the source-file
Warning: Earlier definition at c:/users/carlo/test/wordnet/wn-swipl/wn/wn_s.pl:1
Warning: Current predicate: wn3_db:s/4
Warning: Use :- discontiguous wn3_db:s/6. to suppress this message
% path_wn3_file(sk,_20248)
% path_wn3_files(_20350)
% Compiling sk->C:\Users\Carlo\test\wordnet\wn-swipl\WN/wn_sk.qlf
% path_wn3_file(g,_22738)
...
% path_wn3_files(_6252)
% Compiling fr->C:\Users\Carlo\test\wordnet\wn-swipl\WN/wn_fr.qlf
% 130,646,623 inferences, 40.375 CPU in 41.123 seconds (98% CPU, 3235830 Lips)
...

```

I’m fixing the wn3 key to default, will push right now…  
_edit_  
Note that the timing of 41 sec is wrong, I started with gtrace, the interactive debugger… it’s 18 sec really.

---

<div class="post-metadata">

**Author:** ![CapelliC](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/capellic/32/17_2.png) [@CapelliC](https://swi-prolog.discourse.group/u/CapelliC)\
**Post date:** [February 26, 2022, 12:50pm UTC](https://swi-prolog.discourse.group/t/using-wordnet-via-swi-prolog/5064/7 "2022-02-26T12:50:33Z")

</div>

Ok, pushed a fix. Note the timing in the following:

```prolog
?- time(load_wn3_relations).
% 11,531 inferences, 0.578 CPU in 0.641 seconds (90% CPU, 19946 Lips)
true.

```

Compilation to QLF lowered the time from 18 sec to 0.65 sec.

---

<div class="post-metadata">

**Author:** ![d-vyd](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/d-vyd/32/4191_2.png) [@d-vyd](https://swi-prolog.discourse.group/u/d-vyd)\
**Post date:** [February 26, 2022, 1:52pm UTC](https://swi-prolog.discourse.group/t/using-wordnet-via-swi-prolog/5064/8 "2022-02-26T13:52:03Z")

</div>

> [@CapelliC](#):
>
> to QLF lowered the time from 18 sec t

Wow! I’ll try it. Thanks!
