From a06ea9e4ba89db0163fa344754fad44eccc993ee Mon Sep 17 00:00:00 2001 From: "Sean B. Palmer" Date: Mon, 19 Jan 2009 16:47:28 +0000 Subject: [PATCH] Addin the new Wiktionary module. --- modules/wiktionary.py | 94 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100755 modules/wiktionary.py diff --git a/modules/wiktionary.py b/modules/wiktionary.py new file mode 100755 index 0000000..ede60d0 --- /dev/null +++ b/modules/wiktionary.py @@ -0,0 +1,94 @@ +#!/usr/bin/env python +""" +wiktionary.py - Phenny Wiktionary Module +Copyright 2009, Sean B. Palmer, inamidst.com +Licensed under the Eiffel Forum License 2. + +http://inamidst.com/phenny/ +""" + +import re +import web + +uri = 'http://en.wiktionary.org/w/index.php?title=%s&printable=yes' +r_tag = re.compile(r'<[^>]+>') +r_ul = re.compile(r'(?ims)') + +def text(html): + text = r_tag.sub('', html).strip() + text = text.replace('\n', ' ') + text = text.replace('\r', '') + text = text.replace('(intransitive', '(intr.') + text = text.replace('(transitive', '(trans.') + return text + +def wiktionary(word): + bytes = web.get(uri % word) + bytes = r_ul.sub('', bytes) + + mode = None + etymology = None + definitions = {} + for line in bytes.splitlines(): + if 'id="Etymology"' in line: + mode = 'etymology' + elif 'id="Noun"' in line: + mode = 'noun' + elif 'id="Verb"' in line: + mode = 'verb' + elif 'id="Adjective"' in line: + mode = 'adjective' + elif 'id="Adverb"' in line: + mode = 'adverb' + elif 'id="Interjection"' in line: + mode = 'interjection' + elif 'id="Particle"' in line: + mode = 'particle' + elif 'id="Preposition"' in line: + mode = 'preposition' + elif 'id="' in line: + mode = None + + elif (mode == 'etmyology') and ('

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

  • ' in line): + definitions.setdefault(mode, []).append(text(line)) + + if ' 300: + result = result[:295] + '[...]' + phenny.say(result) +w.commands = ['w'] +w.example = '.w bailiwick' + +if __name__ == '__main__': + print __doc__.strip()