Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

message_evaluation.py 983B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import re
  2. from typing import Dict
  3. from million.model.message import Message
  4. _memoization: Dict[Message, int] = {}
  5. def get(msg: Message) -> int:
  6. """
  7. Returns the estimated value counted in this message
  8. """
  9. return _memoization.get(msg, _compute(msg))
  10. def reset(msg: Message) -> None:
  11. """
  12. Drop memorized value of this Message
  13. """
  14. if msg in _memoization:
  15. _memoization.pop(msg)
  16. def reset() -> None:
  17. """
  18. Drop every memorized message value
  19. """
  20. _memoization.clear()
  21. def _compute(msg: Message) -> int:
  22. # TODO WIP - DNS to resolve audio, gif, pictures with counts
  23. value = _computeContent(msg) or \
  24. None
  25. _memoization[msg] = value
  26. return value
  27. def _computeContent(msg: Message) -> int:
  28. # TODO parse potential math expressions in content
  29. match = msg.content and re.search(r"\d+", msg.content)
  30. if match:
  31. value = int(match.group())
  32. else:
  33. value = None
  34. return value