From da91076417f14fda2d7b92d4f4fc5d37a69c2c69 Mon Sep 17 00:00:00 2001 From: dgw Date: Sun, 6 May 2018 07:29:35 -0500 Subject: [PATCH 1/2] etymology: delete broken module, move .ety cmd to wiktionary --- sopel/modules/etymology.py | 98 ------------------------------------- sopel/modules/wiktionary.py | 25 +++++++++- 2 files changed, 23 insertions(+), 100 deletions(-) delete mode 100644 sopel/modules/etymology.py diff --git a/sopel/modules/etymology.py b/sopel/modules/etymology.py deleted file mode 100644 index 22abff51b2..0000000000 --- a/sopel/modules/etymology.py +++ /dev/null @@ -1,98 +0,0 @@ -# coding=utf-8 -""" -etymology.py - Sopel Etymology Module -Copyright 2007-9, Sean B. Palmer, inamidst.com -Licensed under the Eiffel Forum License 2. - -https://sopel.chat -""" -from __future__ import unicode_literals, absolute_import, print_function, division - -try: - from html import unescape -except ImportError: - from HTMLParser import HTMLParser - - # pep8 dictates a blank line here... - def unescape(s): - return HTMLParser.unescape.__func__(HTMLParser, s) -import re -import requests -from sopel.module import commands, example, NOLIMIT - -etyuri = 'http://etymonline.com/?term=%s' -etysearch = 'http://etymonline.com/?search=%s' - -r_definition = re.compile(r'(?ims)]*>.*?') -r_tag = re.compile(r'<(?!!)[^>]+>') -r_whitespace = re.compile(r'[\t\r\n ]+') - -abbrs = [ - 'cf', 'lit', 'etc', 'Ger', 'Du', 'Skt', 'Rus', 'Eng', 'Amer.Eng', 'Sp', - 'Fr', 'N', 'E', 'S', 'W', 'L', 'Gen', 'J.C', 'dial', 'Gk', - '19c', '18c', '17c', '16c', 'St', 'Capt', 'obs', 'Jan', 'Feb', 'Mar', - 'Apr', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'c', 'tr', 'e', 'g' -] -t_sentence = r'^.*?(? sbp, would it be possible to have a flag for .ety to get 2nd/etc - # entries? - http://swhack.com/logs/2006-07-19#T15-05-29 - - if len(word) > 25: - raise ValueError("Word too long: %s[...]" % word[:10]) - word = {'axe': 'ax/axe'}.get(word, word) - - bytes = requests.get(etyuri % word).text - definitions = r_definition.findall(bytes) - - if not definitions: - return None - - defn = text(definitions[0]) - m = r_sentence.match(defn) - if not m: - return None - sentence = m.group(0) - - maxlength = 275 - if len(sentence) > maxlength: - sentence = sentence[:maxlength] - words = sentence[:-5].split(' ') - words.pop() - sentence = ' '.join(words) + ' [...]' - - sentence = '"' + sentence.replace('"', "'") + '"' - return sentence + ' - ' + (etyuri % word) - - -@commands('ety') -@example('.ety word') -def f_etymology(bot, trigger): - """Look up the etymology of a word""" - word = trigger.group(2) - - try: - result = etymology(word) - except IOError: - msg = "Can't connect to etymonline.com (%s)" % (etyuri % word) - bot.msg(trigger.sender, msg) - return NOLIMIT - except (AttributeError, TypeError): - result = None - - if result is not None: - bot.msg(trigger.sender, result) - else: - uri = etysearch % word - msg = 'Can\'t find the etymology for "%s". Try %s' % (word, uri) - bot.msg(trigger.sender, msg) - return NOLIMIT diff --git a/sopel/modules/wiktionary.py b/sopel/modules/wiktionary.py index b6027ecdd9..cc2f1d0606 100644 --- a/sopel/modules/wiktionary.py +++ b/sopel/modules/wiktionary.py @@ -36,7 +36,7 @@ def wikt(word): etymology = None definitions = {} for line in bytes.splitlines(): - if 'id="Etymology"' in line: + if 'id="Etymology"' in line or 'id="Etymology_1"' in line: mode = 'etymology' elif 'id="Noun"' in line: mode = 'noun' @@ -55,7 +55,7 @@ def wikt(word): elif 'id="' in line: mode = None - elif (mode == 'etmyology') and ('

' in line): + elif (mode == 'etymology') and ('

' in line): etymology = text(line) elif (mode is not None) and ('

  • ' in line): definitions.setdefault(mode, []).append(text(line)) @@ -102,3 +102,24 @@ def wiktionary(bot, trigger): if len(result) > 300: result = result[:295] + '[...]' bot.say(result) + + +@commands('ety') +@example('.ety bailiwick') +def wiktionary_ety(bot, trigger): + """Look up a word's etymology on Wiktionary.""" + word = trigger.group(2) + if word is None: + bot.reply('You must give me a word!') + return + + etymology, _definitions = wikt(word) + if not etymology: + bot.say("Couldn't get the etymology for %s." % word) + return + + result = "{}: {}".format(word, etymology) + + if len(result) > 300: + result = result[:295] + '[...]' + bot.say(result) From bb9fb4b03f5c8e2d30309ae1cc8a71cc321090da Mon Sep 17 00:00:00 2001 From: dgw Date: Mon, 7 May 2018 12:25:05 -0500 Subject: [PATCH 2/2] wiktionary: Add example tests These are at the mercy of Wiktionary editors changing the page content, but it doesn't look like the entry for "bailiwick" is edited much. --- sopel/modules/wiktionary.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sopel/modules/wiktionary.py b/sopel/modules/wiktionary.py index cc2f1d0606..843f269ff1 100644 --- a/sopel/modules/wiktionary.py +++ b/sopel/modules/wiktionary.py @@ -80,7 +80,7 @@ def format(result, definitions, number=2): @commands('wt', 'define', 'dict') -@example('.wt bailiwick') +@example('.wt bailiwick', "bailiwick — noun: 1. The district within which a bailie or bailiff has jurisdiction, 2. A person's concern or sphere of operations, their area of skill or authority") def wiktionary(bot, trigger): """Look up a word on Wiktionary.""" word = trigger.group(2) @@ -105,7 +105,7 @@ def wiktionary(bot, trigger): @commands('ety') -@example('.ety bailiwick') +@example('.ety bailiwick', "bailiwick: From bailie (“bailiff”) and wick (“dwelling”), from Old English wīc.") def wiktionary_ety(bot, trigger): """Look up a word's etymology on Wiktionary.""" word = trigger.group(2)