Skip to content
clbecker edited this page Jul 13, 2012 · 4 revisions

Home

Synonyms, Hyponyms, Antonyms, etc

In this section I'll cover the methods for accessing all these categories of related words. The code for this can be found in /bin/get_synonyms.pl

use Wiktionary::Parser;

my $parser = Wiktionary::Parser->new();
my $document = $parser->get_document(title => 'dog');

There are several methods you can call to get various categories of related words. Each of these returns the same sort of structure, mapping language => word sense => term list.

my $hyponyms  = $document->get_hyponyms()
my $hypernyms = $document->get_hypernyms()
my $antonyms  = $document->get_antonyms();
my $synonyms  = $document->get_synonyms();

The following code snippet will walk through the synonym data structure and print out each piece of information.

my $synonyms = $document->get_synonyms();


for my $language_code (sort keys %{ $synonyms || {} }) {

    print "$synonyms->{$language_code}{language}; $language_code\n";

    for my $word_sense (sort keys %{ $synonyms->{$language_code}{sense} }) {

        print "\t$word_sense\n";

        my @words = @{ $synonyms->{$language_code}{sense}{$word_sense} };

        for my $word (@words) {
            print "\t\t$word\n";
        }
    }
}

Home

Clone this wiki locally