A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

150 строки
5.2 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 Ben Kurtovic <ben.kurtovic@verizon.net>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. import re
  23. from earwigbot import exceptions
  24. from earwigbot.commands import Command
  25. class Dictionary(Command):
  26. """Define words and stuff."""
  27. name = "dictionary"
  28. commands = ["dict", "dictionary", "define"]
  29. def process(self, data):
  30. if not data.args:
  31. self.reply(data, "what do you want me to define?")
  32. return
  33. term = " ".join(data.args)
  34. lang = self.bot.wiki.get_site().lang
  35. try:
  36. defined = self.define(term, lang)
  37. except exceptions.APIError:
  38. msg = "cannot find a {0}-language Wiktionary."
  39. self.reply(data, msg.format(lang))
  40. else:
  41. self.reply(data, defined.encode("utf8"))
  42. def define(self, term, lang):
  43. try:
  44. site = self.bot.wiki.get_site(project="wiktionary", lang=lang)
  45. except exceptions.SiteNotFoundError:
  46. site = self.bot.wiki.add_site(project="wiktionary", lang=lang)
  47. page = site.get_page(term)
  48. try:
  49. entry = page.get()
  50. except (exceptions.PageNotFoundError, exceptions.InvalidPageError):
  51. return "no definition found."
  52. languages = self.get_languages(entry)
  53. if not languages:
  54. return u"couldn't parse {0}!".format(page.url)
  55. result = []
  56. for lang, section in sorted(languages.items()):
  57. this = u"({0}) {1}".format(lang, self.get_definition(section))
  58. result.append(this)
  59. return u"; ".join(result)
  60. def get_languages(self, entry):
  61. regex = r"(?:\A|\n)==\s*([a-zA-Z0-9_ ]*?)\s*==(?:\Z|\n)"
  62. split = re.split(regex, entry)
  63. if len(split) % 2 == 0:
  64. return None
  65. split.pop(0)
  66. languages = {}
  67. for i in xrange(0, len(split), 2):
  68. languages[split[i]] = split[i + 1]
  69. return languages
  70. def get_definition(self, section):
  71. parts_of_speech = {
  72. "v.": "Verb",
  73. "n.": "Noun",
  74. "pron.": "Pronoun",
  75. "adj.": "Adjective",
  76. "adv.": "Adverb",
  77. "prep.": "Preposition",
  78. "conj.": "Conjunction",
  79. "inter.": "Interjection",
  80. "symbol": "Symbol",
  81. "suffix": "Suffix",
  82. "initialism": "Initialism",
  83. "phrase": "Phrase",
  84. "proverb": "Proverb",
  85. }
  86. defs = []
  87. for part, fullname in parts_of_speech.iteritems():
  88. if re.search("===\s*" + fullname + "\s*===", section):
  89. regex = "===\s*" + fullname + "\s*===(.*?)(?:(?:===)|\Z)"
  90. body = re.findall(regex, section, re.DOTALL)
  91. if body:
  92. definition = self.parse_body(body[0])
  93. if definition:
  94. defs.append("\x02{0}\x0F {1}".format(part, definition))
  95. return "; ".join(defs)
  96. def parse_body(self, body):
  97. senses = []
  98. for line in body.splitlines():
  99. line = line.strip()
  100. if re.match("#\s*[^:*]", line):
  101. line = re.sub("\[\[(.*?)\|(.*?)\]\]", r"\2", line)
  102. line = self.strip_templates(line)
  103. line = line[1:].replace("'''", "").replace("''", "")
  104. line = line.replace("[[", "").replace("]]", "")
  105. senses.append(line.strip())
  106. if not senses:
  107. return None
  108. if len(senses) == 1:
  109. return senses[0]
  110. result = [] # Number the senses incrementally
  111. for i, sense in enumerate(senses):
  112. result.append(u"{0}. {1}".format(i + 1, sense))
  113. return " ".join(result)
  114. def strip_templates(self, line):
  115. line = list(line)
  116. stripped = ""
  117. depth = 0
  118. while line:
  119. this = line.pop(0)
  120. if line:
  121. next = line[0]
  122. else:
  123. next = ""
  124. if this == "{" and next == "{":
  125. line.pop(0)
  126. depth += 1
  127. elif this == "}" and next == "}":
  128. line.pop(0)
  129. depth -= 1
  130. elif depth == 0:
  131. stripped += this
  132. return stripped