A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

85 řádky
3.2 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 by 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. from collections import defaultdict
  23. from re import sub, UNICODE
  24. __all__ = ["MarkovChain", "MarkovChainIntersection"]
  25. class MarkovChain(object):
  26. START = -1
  27. END = -2
  28. def __init__(self, text):
  29. self.text = text
  30. self.chain = defaultdict(lambda: defaultdict(lambda: 0))
  31. words = sub("[^\w\s-]", "", text.lower(), flags=UNICODE).split()
  32. prev = self.START
  33. for word in words:
  34. self.chain[prev][word] += 1
  35. prev = word
  36. try: # This won't work if the source text is completely blank
  37. self.chain[word][self.END] += 1
  38. except KeyError:
  39. pass
  40. def __repr__(self):
  41. """Return the canonical string representation of the MarkovChain."""
  42. return "MarkovChain(text={0!r})".format(self.text)
  43. def __str__(self):
  44. """Return a nice string representation of the MarkovChain."""
  45. return "<MarkovChain of size {0}>".format(self.size())
  46. def size(self):
  47. count = 0
  48. for node in self.chain.itervalues():
  49. for hits in node.itervalues():
  50. count += hits
  51. return count
  52. class MarkovChainIntersection(MarkovChain):
  53. def __init__(self, mc1, mc2):
  54. self.chain = defaultdict(lambda: defaultdict(lambda: 0))
  55. self.mc1, self.mc2 = mc1, mc2
  56. c1 = mc1.chain
  57. c2 = mc2.chain
  58. for word, nodes1 in c1.iteritems():
  59. if word in c2:
  60. nodes2 = c2[word]
  61. for node, count1 in nodes1.iteritems():
  62. if node in nodes2:
  63. count2 = nodes2[node]
  64. self.chain[word][node] = min(count1, count2)
  65. def __repr__(self):
  66. """Return the canonical string representation of the intersection."""
  67. res = "MarkovChainIntersection(mc1={0!r}, mc2={1!r})"
  68. return res.format(self.mc1, self.mc2)
  69. def __str__(self):
  70. """Return a nice string representation of the intersection."""
  71. res = "<MarkovChainIntersection of size {0} ({1} ^ {2})>"
  72. return res.format(self.size(), self.mc1, self.mc2)