ashok has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
Is there any way to find an antonym for a given word in Perl? I want this to work on both on unix and on Windows. How to use Word library in case of later?
Thanks in advance.
Ashok
Re: using Word dictionary in Perl
by t0mas (Priest) on Dec 01, 2000 at 12:50 UTC
|
Hi, ashok
This is how you can use the MSWord SynonymInfo method from perl using Win32::OLE.
Need I say that it is not portable, so you'll have to find another
way to do it on *nix boxes :)
#!/usr/bin/perl -w
# Uses
use strict;
use Win32::OLE;
use Win32::OLE::Const;
# Create MSWord object and load constants
my $MSWord = Win32::OLE->new('Word.Application', 'Quit') or
die "Could not load MS Word\n";
my $wd=Win32::OLE::Const->Load($MSWord);
# Word and language to search for
my $word="big";
my $language=$wd->{wdEnglishUS};
# Get antonyms from MSWord
my $antonyms=$MSWord->SynonymInfo(
{Word => $word, LanguageID => $language})->AntonymList;
# Print them out...
foreach (@{ $antonyms }) { print $_."\n" };
Anoter way might be to use LWP and search an online dictionary
(like Lexical FreeNet) and
parse the returned page. The url to get the antonyms for "big" on
lexfn is
http://www.lexfn.com/l/lexfn-cuff.cgi?sWord=big&tWord=&query=show&maxReach=2&Aant=on
/brother t0mas
| [reply] [d/l] |
|
Hi ,
It's great. It really helped me a lot. Can you pl. give me the code for finding Synonym also? I tried to use your code but I failed. I thought it will be like this
# Get synonyms from MSWord
my $synonyms=$MSWord->SynonymInfo(
{Word => $word, LanguageID => $language})->SynonymList;
# Print them out...
foreach (@{ $synonyms }) { print $_."\n" };
But it did not work.
Thanks
Ashok
| [reply] [d/l] |
|
Your'e just (1) from correct. :)
#!/usr/bin/perl -w
# Uses
use strict;
use Win32::OLE;
use Win32::OLE::Const;
# Create MSWord object and load constants
my $MSWord = Win32::OLE->new('Word.Application', 'Quit') or
die "Could not load MS Word\n";
my $wd=Win32::OLE::Const->Load($MSWord);
# Word and language to search for
my $word="big";
my $language=$wd->{wdEnglishUS};
# Get synonyms from MSWord
my $synonyms=$MSWord->SynonymInfo(
{Word => $word, LanguageID => $language})->SynonymList(1);
# Print them out...
foreach (@{ $synonyms }) { print $_."\n" };
The (1) returns the synonymlist for the first meaning of the
word. If you have a word that can mean many things, you could
end up with many synonymlists. You'll then have to check the
MeaningCount property and loop all the synonymlists for each
meaning.
/brother t0mas
| [reply] [d/l] |
|
|
Re: using Word dictionary in Perl
by clemburg (Curate) on Dec 01, 2000 at 20:25 UTC
|
Look at issue #18 of The Perl Journal,
Volume 5, Number 2 (#18), Summer 2000.
It has an article on Lingua::Wordnet, which can
do what you want (actually, it can do much more) by searching the Wordnet database.
WordNet® is an on-line lexical reference system whose design is inspired by current psycholinguistic theories of human lexical
memory. English nouns, verbs, adjectives and adverbs are organized into synonym sets, each representing one underlying lexical
concept. Different relations link the synonym sets.
WordNet was developed by the Cognitive Science Laboratory at Princeton University under the direction of Professor George A.
Miller (Principal Investigator).
As a demo, this is the Wordnet output on a query of "Pearl" (no, it won't find Perl).
BTW, this Wordnet stuff is a real gem - it comprises many, many years
of expert linguist's work, and it's free, too: "WordNet® is unencumbered, and may be used in commercial applications in accordance with the following license agreement. An
attorney representing the commercial interest should review this WordNet license with respect to the intended use. ( ... snip ... ) Permission to use, copy, modify and distribute this software and database and its documentation for any purpose and without fee or
royalty is hereby granted, (... snip ...)"
Christian Lemburg
Brainbench MVP for Perl
http://www.brainbench.com
| [reply] |
|
Yep, that's where I saw it.
Still waiting for TPJ to accept international creditcards for online access to the magazines, which means that my hard copy is never with me when I need it (either at home or work)!.
It is a great magazine though. Maybe we can manage to get a discount subscription set up for PerlMonks users - or a referral link so PerlMonks can get a little money? Does anyone have any contacts there?
| [reply] |
Re: using Word dictionary in Perl
by $code or die (Deacon) on Dec 01, 2000 at 18:07 UTC
|
That looks right, bear in mind you need to have Word 'installed' on the machine this runs on. Also IIRC, this month's Perl Journal has some examples of this. I think I read somewhere that there is going to be a port of the OLE module to Unix machines, but maybe I dreamt this.
Actually, I may have read about use OLE for Word dictionary\thesaurus in a VBScript\ASP book I flicked through.
Although it's not very portable, it is definitely worth familiarising yourself with Win32::OLE. It makes a lot of very difficult PerlWin32 programming tasks much easier. As always you need to weigh up portability vs. complexity and time of development. | [reply] |
|
|