您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

message_evaluation.py 1.0KB

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