From f5dc31f1da3a5dc60899ef84fd4296c2e51d78d4 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Tue, 10 Jul 2012 22:02:20 -0400 Subject: [PATCH] More support for section headers in !dictionary. --- earwigbot/commands/dictionary.py | 41 ++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/earwigbot/commands/dictionary.py b/earwigbot/commands/dictionary.py index e3ee7e2..8f29fb8 100644 --- a/earwigbot/commands/dictionary.py +++ b/earwigbot/commands/dictionary.py @@ -89,27 +89,31 @@ class Dictionary(Command): def get_definition(self, section, level): parts_of_speech = { - "v.": ["Verb"], - "n.": ["Noun"], - "pron.": ["Pronoun"], - "adj.": ["Adjective"], - "adv.": ["Adverb"], - "prep.": ["Preposition"], - "conj.": ["Conjunction"], - "inter.": ["Interjection"], - "symbol": ["Symbol"], - "suffix": ["Suffix"], - "initialism": ["Initialism"], - "phrase": ["Phrase"], - "proverb": ["Proverb"], - "prop. n.": ["Proper noun"], - "abbr.": ["Abbreviation", "\{\{abbreviation\}\}"], + "v.": "Verb", + "n.": "Noun", + "pron.": "Pronoun", + "adj.": "Adjective", + "adv.": "Adverb", + "prep.": "Preposition", + "conj.": "Conjunction", + "inter.": "Interjection", + "symbol": "Symbol", + "suffix": "Suffix", + "initialism": "Initialism", + "phrase": "Phrase", + "proverb": "Proverb", + "prop. n.": "Proper noun", + "abbr.": "Abbreviation", } blocks = "=" * (level + 1) defs = [] - for part, fullnames in parts_of_speech.iteritems(): - for fullname in fullnames: - regex = "{0}\s*{1}\s*{0}".format(blocks, fullname) + for part, fullname in parts_of_speech.iteritems(): + regexes = [ + "{0}\s*{1}\s*{0}".format(blocks, fullname), + "{0}\s*\{\{{1}\}\}\s*{0}".format(blocks, fullname), + "{0}\s*\{\{{1}\}\}\s*{0}".format(blocks, fullname.lower()), + ] + for regex in regex: if re.search(regex, section): regex = "{0}\s*{1}\s*{0}(.*?)(?:(?:{0})|\Z)" regex = regex.format(blocks, fullname) @@ -128,6 +132,7 @@ class Dictionary(Command): ("", ""), ("\[\[(.*?)\|(.*?)\]\]", r"\2"), ("\{\{(.*?) of\|(.*?)\}\}", r"\1 of \2."), + ("\{\{w\|(.*?)\}\}", r"\1"), ("\{\{surname(.*?)\}\}", r"A surname."), ("\{\{given name\|(.*?)(\||\}\})", r"A \1 given name."), ]