1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- import re
- from typing import Dict
- from million.model.message import Message
- import million.analyze.media_count_mapper as mcm
-
-
- _memoization: Dict[Message, int] = {}
- _dns_solver: mcm.MediaCountMapper = mcm.MediaCountMapper()
-
-
- def get(msg: Message) -> int:
- """
- Returns the estimated value counted in this message
- """
- return _memoization.get(msg, _compute(msg))
-
-
- def reset(msg: Message) -> None:
- """
- Drop memorized value of this Message
- """
- if msg in _memoization:
- _memoization.pop(msg)
-
-
- def reset() -> None:
- """
- Drop every memorized message value
- """
- _memoization.clear()
-
-
- def _compute(msg: Message) -> int:
- value = _dns_solver.solve(msg) or _computeContent(msg) or None
-
- _memoization[msg] = value
- return value
-
-
- def _computeContent(msg: Message) -> int:
- if not msg.content:
- return
-
- s = re.sub(r'[^\s\d.,]|[.,]{2,}',"", msg.content)
- match = re.search(r"\d+", s)
-
- if match:
- return int(match.group())
-
- return None
|