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