add a random-synonym getting function to Dict, add tests for it.

using the moby-thes database from dict.org.
This commit is contained in:
Daniel Folkinshteyn 2010-04-08 00:04:44 -04:00
parent 5d9273cd5a
commit afe1a2124d
2 changed files with 34 additions and 0 deletions

View File

@ -36,6 +36,8 @@ from supybot.commands import *
import supybot.ircutils as ircutils
import supybot.callbacks as callbacks
import random
try:
dictclient = utils.python.universalImport('dictclient', 'local.dictclient')
except ImportError:
@ -123,6 +125,33 @@ class Dict(callbacks.Plugin):
irc.reply(s)
dict = wrap(dict, [many('something')])
def synonym(self, irc, msg, args, words):
"""<word> [<word> ...]
Gets a random synonym from the Moby Thesaurus (moby-thes) database.
If given many words, gets a random synonym for each of them.
Quote phrases to have them treated as one lookup word.
"""
try:
server = conf.supybot.plugins.Dict.server()
conn = dictclient.Connection(server)
except socket.error, e:
irc.error(utils.web.strError(e), Raise=True)
dictionary = 'moby-thes'
response = []
for word in words:
definitions = conn.define(dictionary, word)
if not definitions:
asynonym = word
else:
defstr = definitions[0].getdefstr()
synlist = ' '.join(defstr.split('\n')).split(': ', 1)[1].split(',')
asynonym = random.choice(synlist).strip()
response.append(asynonym)
irc.reply(' '.join(response))
synonym = wrap(synonym, [many('something')])
Class = Dict

View File

@ -43,5 +43,10 @@ class DictTestCase(PluginTestCase):
def testRandomDictionary(self):
self.assertNotError('random')
self.assertNotError('dict [random] moo')
def testSynonym(self):
self.assertNotError('synonym stuff')
self.assertNotError('synonym someone goes home')
self.assertRegexp('synonym nanotube', 'nanotube')
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: