A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
3.5 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. try:
  23. import mwparserfromhell
  24. except ImportError:
  25. mwparserfromhell = None
  26. __all__ = ["BaseTextParser", "ArticleTextParser", "HTMLTextParser"]
  27. class BaseTextParser(object):
  28. """Base class for a parser that handles text."""
  29. def __init__(self, text):
  30. self.text = text
  31. def __repr__(self):
  32. """Return the canonical string representation of the text parser."""
  33. return "{0}(text={1!r})".format(self.__class__.__name__, self.text)
  34. def __str__(self):
  35. """Return a nice string representation of the text parser."""
  36. name = self.__class__.__name__
  37. return "<{0} of text with size {1}>".format(name, len(text))
  38. class ArticleTextParser(BaseTextParser):
  39. """A parser that can strip and chunk wikicode article text."""
  40. def strip(self):
  41. """Clean the page's raw text by removing templates and formatting.
  42. Return the page's text with all HTML and wikicode formatting removed,
  43. including templates, tables, and references. It retains punctuation
  44. (spacing, paragraphs, periods, commas, (semi)-colons, parentheses,
  45. quotes), original capitalization, and so forth. HTML entities are
  46. replaced by their unicode equivalents.
  47. The actual stripping is handled by :py:mod:`mwparserfromhell`.
  48. """
  49. wikicode = mwparserfromhell.parse(self.text)
  50. self.clean = u" ".join(wikicode.normalize().ifilter_text())
  51. return self.clean
  52. def chunk(self, max_chunks):
  53. """Convert the clean article text into a list of web-searchable chunks.
  54. No greater than *max_chunks* will be returned. Each chunk will only be
  55. a couple sentences long at most. The idea here is to return a
  56. representative sample of the article text rather than the entire
  57. article, so we'll probably pick and choose from its introduction, body,
  58. and conclusion, especially if the article is large and *max_chunks* is
  59. low, so we don't end up just searching for the first paragraph.
  60. """
  61. return [self.text] # TODO: NotImplemented
  62. class HTMLTextParser(BaseTextParser):
  63. """A parser that can extract the text from an HTML document."""
  64. def strip(self):
  65. return self.text # TODO: NotImplemented