QueryData(3pm)

πŸ“¦
version 1.49 Β· 2 versions
  • 1.49 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 1.49 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

WordNet::QueryData β€” it's a direct perl interface to the WordNet database, bestie. That's the whole vibe. 🫢


✨ SYNOPSIS ✨

use WordNet::QueryData;

my $wn = WordNet::QueryData->new( noload => 1);

print "Synset: ", join(", ", $wn->querySense("cat#n#7", "syns")), "\n";
print "Hyponyms: ", join(", ", $wn->querySense("cat#n#1", "hypo")), "\n";
print "Parts of Speech: ", join(", ", $wn->querySense("run")), "\n";
print "Senses: ", join(", ", $wn->querySense("run#v")), "\n";
print "Forms: ", join(", ", $wn->validForms("lay down#v")), "\n";
print "Noun count: ", scalar($wn->listAllWords("noun")), "\n";
print "Antonyms: ", join(", ", $wn->queryWord("dark#n#1", "ants")), "\n";

🌸 DESCRIPTION 🌸

Okay so WordNet::QueryData is your direct interface to the WordNet database files, no middleman, no nothing. πŸ’…

It needs the WordNet package itself to exist first (http://www.cogsci.princeton.edu/~wn/), so like, get that. Then this module hands you direct access to the full WordNet semantic lexicon and you can do whatever you want with it. Every part of speech is supported, and access is generally very efficient because the index and morphical exclusion tables get loaded at initialization. And if you want extra-fast lookups, you can optionally make it load the indexes into memory. Obsessed. 😌


πŸ¦‹ USAGE πŸ¦‹

πŸŽ€ LOCATING THE WORDNET DATABASE πŸŽ€

So to use QueryData you gotta tell it where your WordNet database lives. Two ways, pick your fighter:

  1. set the appropriate environment variables
  2. pass the location to QueryData when you invoke the new function

QueryData knows about two environment variables: WNHOME and WNSEARCHDIR. Here's the tea β˜•:

  • If WNSEARCHDIR is set, QueryData looks for WordNet data files there.
  • Otherwise, QueryData looks for WordNet data files in WNHOME/dict (WNHOME\dict on a PC).
  • If WNHOME isn't set at all, it defaults to /usr/local/WordNet-3.0 on Unix and C:\Program Files\WordNet\3.0 on a PC.

Normally all you have to do is set the WNHOME variable to wherever you unpacked your WordNet distribution, and the database files are normally unpacked to the dict subdirectory. Easy. ✨

You can also just pass the location of the database files directly to QueryData. To do that, pass the location to new:

my $wn = WordNet::QueryData->new("/usr/local/wordnet/dict");

Or instead, call the constructor with a hash of params, like:

my $wn = WordNet::QueryData->new(
    dir => "/usr/local/wordnet/dict",
    verbose => 0,
    noload => 1
);

When you call new like this, two extra arguments are supported:

  • verbose β€” will output debugging information.
  • noload β€” will cause the object to not load the indexes at startup.

πŸ’… CACHING VERSUS NOLOAD πŸ’…

The noload option means data gets retrieved using a dictionary lookup instead of caching the indexes in RAM. This method gives you an immediate startup time but slightly β€” though less than you might think β€” longer lookup time. πŸ‘€

For the curious, here are some profile data for each method on a duo core intel mac, averaged seconds over 10000 iterations:

Caching versus noload times in seconds

                                          noload => 1  noload => 0
------------------------------------------------------------------
new()                                     0.00001      2.55
queryWord("descending")                   0.0009       0.0001
querySense("sunset#n#1", "hype")          0.0007       0.0001
validForms ("lay down#2")                 0.0004       0.0001

Obviously the new() comparison is not very useful, because nothing is happening with the constructor in the case of noload => 1. Similarly, lookups with caching are basically just hash lookups, and therefore very fast. The lookup times for noload => 1 illustrate the tradeoff between caching at new() time and using dictionary lookups.

Because of the lookup speed increase when noload => 0, many users will find it useful to set noload to 1 during development cycles, and to 0 when RAM is less of a concern than speed. The bottom line, fr fr: noload => 1 saves you over 2 seconds of startup time, and costs you about 0.0005 seconds per lookup. βš–οΈ

πŸ”₯ QUERYING THE DATABASE πŸ”₯

There are two primary query functions: querySense and queryWord. Let's break it down:

  • querySense accesses semantic (sense to sense) relations.
  • queryWord accesses lexical (word to word) relations.

The majority of relations are semantic. Some relations β€” including "also see", antonym, pertainym, "participle of verb", and derived forms β€” are lexical. See the following WordNet documentation for additional information:

http://wordnet.princeton.edu/man/wninput.5WN#sect3

Both functions take as their first argument a query string, and it comes in one of three types:

  1. word (e.g. "dog")
  2. word#pos (e.g. "house#n")
  3. word#pos#sense (e.g. "ghostly#a#1")

Types (1) or (2) passed to querySense or queryWord will return a list of possible query strings at the next level of specificity. When type (3) is passed to querySense or queryWord, it requires a second argument: a relation. Relations generally only work with one function or the other, though some relations can be either semantic or lexical, hence they may work for both functions. ✨

Below is a list of known relations, grouped according to the function they're most likely to work with:

πŸ“ queryWord πŸ“

  • also β€” also see
  • ants β€” antonyms
  • deri β€” derived forms (nouns and verbs only)
  • part β€” participle of verb (adjectives only)
  • pert β€” pertainym (pertains to noun) (adjectives only)
  • vgrp β€” verb group (verbs only)

🧚 querySense 🧚

  • also β€” also see
  • glos β€” word definition
  • syns β€” synset words
  • hype β€” hypernyms
  • inst β€” instance of
  • hypes β€” hypernyms and "instance of"
  • hypo β€” hyponyms
  • hasi β€” has instance
  • hypos β€” hyponums and "has instance"
  • mmem β€” member meronyms
  • msub β€” substance meronyms
  • mprt β€” part meronyms
  • mero β€” all meronyms
  • hmem β€” member holonyms
  • hsub β€” substance holonyms
  • hprt β€” part holonyms
  • holo β€” all holonyms
  • attr β€” attributes (?)
  • sim β€” similar to (adjectives only)
  • enta β€” entailment (verbs only)
  • caus β€” cause (verbs only)
  • domn β€” domain - all
  • dmnc β€” domain - category
  • dmnu β€” domain - usage
  • dmnr β€” domain - region
  • domt β€” member of domain - all (nouns only)
  • dmtc β€” member of domain - category (nouns only)
  • dmtu β€” member of domain - usage (nouns only)
  • dmtr β€” member of domain - region (nouns only)

When called in this manner, querySense and queryWord will return a list of related words/senses. Note that as of WordNet 2.1, many hypernyms have become "instance of" and many hyponyms have become "has instance." πŸ‘‘

Note that querySense and queryWord use type (3) query strings in different ways:

  • A type (3) string passed to querySense specifies a synset.
  • A type (3) string passed to queryWord specifies a specific sense of a specific word.

⭐ OTHER FUNCTIONS ⭐

  • validForms β€” accepts a type (1) or (2) query string. It returns a list of all alternate forms (alternate spellings, conjugations, plural/singular forms, etc.). The type (1) query returns alternates for all parts of speech (noun, verb, adjective, adverb).
WARNING: Only the first argument returned by validForms is certain to be valid (i.e. recognized by WordNet). Remaining arguments may not be valid. 😭
  • listAllWords β€” accepts a part of speech and returns the full list of words in the WordNet database for that part of speech.
  • level β€” accepts a type (3) query string and returns a distance (not necessarily the shortest or longest) to the root in the hypernym directed acyclic graph.
  • offset β€” accepts a type (3) query string and returns the binary offset of that sense's location in the corresponding data file.
  • tagSenseCnt β€” accepts a type (2) query string and returns the tagsense_cnt value for that lemma: "number of senses of lemma that are ranked according to their frequency of occurrence in semantic concordance texts."
  • lexname β€” accepts a type (3) query string and returns the lexname of the sense; see WordNet lexnames man page for more information.
  • frequency β€” accepts a type (3) query string and returns the frequency count of the sense from tagged text; see WordNet cntlist man page for more information.

See test.pl for additional example usage. πŸ“


πŸ’œ NOTES πŸ’œ

Requires access to WordNet database files (data.noun/noun.dat, index.noun/noun.idx, etc.) β€” like, no files, no vibes. 🫠


Copyright 2000-2005 Jason Rennie. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


🌈 SEE ALSO 🌈


Okay that's the whole page, bestie! This one's from perl v5.34.0, dated 2022-06-28, living in the User Contributed Perl Documentation corner of the manual, filed under QueryData(3pm). Slay, go query some words. πŸ’…βœ¨πŸ¦‹

πŸ“¦ where this page comes from