Using Word to do a Thesaurus lookup

Posted by Nick Gammon on Tue 14 Dec 2004 04:29 AM — 4 posts, 27,412 views.

Australia Forum Administrator #0
I don't have this working perfectly yet, but enough to be worth documenting. It might save someone (like me, in a month) half a day's work working out the details.

The intention was to get MUSHclient to lookup a word, as in using a thesaurus, using Word as the thesaurus engine, as a lot of people would have Word installed.

For a start, to do this you will need a COM object. I have tried to do it in Lua, with partial success. Maybe the problem is the Lua COM implementation. ;)

Here is the code for a proposed "lookup word" alias:


require "luacom"

function GetSynonym (name, line, wildcards)

word = luacom.GetObject("Word.Application")

if not word then
  ColourNote ("white", "red", "Word is not running")
  return
end -- if

word.Visible = true  -- make Word visible
word:Activate ()  -- bring Word to the front

if word.Documents.count == 0 then
  ColourNote ("white", "red", "No document open in Word")
  return
end -- if

doc = word.ActiveDocument  -- get active document

doc.Words (1):InsertBefore (wildcards [1] .. " ")  
doc.Words (1):CheckSynonyms ()

-- show resulting word
ColourNote ("white", "blue", doc.Words (1).Text)

ActivateClient ()  -- reactivate MUSHclient

-- release COM objects
doc = nil
word = nil

end -- function 


I couldn't get the COM interface to create a new object and add a new Word document, so for this to work you need to start Word yourself, and have an empty document open.

Then this should insert the wanted word as word 1 in the document (InsertBefore) and look up the thesaurus (CheckSynonyms). Then the word you replace it with is recovered (doc.Words (1).Text) and displayed in the output window.

Apart from the annoyance of having to start Word, and the possibility this would corrupt any existing Word document you might have open, it also has the annoyance that it only works once. Trying it a second time seems to hang Word, or at least it does for my version.

Maybe some COM expert out there can see what I am doing wrong. However it is a starting point for the general idea.
#1
I got an online thesaurus working using WordNet and LuaSocket.

About WordNet
http://wordnet.princeton.edu/

WordNet Search
http://wordnetweb.princeton.edu/perl/webwn

Communicating with the Internet (with LuaSocket)
http://mushclient.com/forum/?id=8319

WordNet Search: bash
 (n) knock, bash, bang, smash, belt (a vigorous blow) 
 (n) bash, do, brawl (an uproarious party) 
 (v) sock, bop, whop, whap, bonk, bash (hit hard) 


<aliases>
  <alias
   match="wordnet *"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>local http = require "socket.http"
local url = "http://wordnetweb.princeton.edu/perl/webwn"
local search = "?c=0&amp;s=" .. string.gsub("%1", " ", "+")
local html = http.request(url .. search)
AnsiNote("\\n" .. ANSI(37) .. "WordNet Search: %1")
for m in string.gmatch(html, "S:&lt;/a&gt;([^\\n]*)\\n") do
  local s = string.gsub(m, "&lt;[^&gt;]*&gt;", "")
  AnsiNote(ANSI(32) .. s)
end</send>
  </alias>
</aliases>


The only problem is the script hangs if princeton.edu is too slow to respond.
Amended on Mon 04 Jan 2010 11:07 AM by Morat
USA #2
First thing you are doing wrong is assuming that sane people that use your client have word installed, instead of Open Office. lol

Seriously though, this snippet of code, for VB, implies you don't need to go as far as opening a document:

Dim si As SynonymInfo
Dim wordApp As Word._Application
wordApp = CreateObject("Word.Application")
si = wordApp.SynonymInfo("good")


No idea if there is one for Antonyms too.
Amended on Mon 04 Jan 2010 05:43 PM by Shadowfyr
Australia Forum Administrator #3
In response to your other post, I have found out why the plugin doesn't work, so you may want to go back to using that.