1234567891011121314151617181920212223242526 |
- from math import floor
- from typing import Dict
- from million.model.message import Message
-
- memoization: Dict[Message, int] = {}
-
- # TODO WIP
- # - DNS to resolve audio, gif, pictures with counts
- def __compute__(msg: Message) -> int:
- value = None
- # Remove any number that is not a digit
- # TODO parse potential math expressions in content
- cleaned_content = ''.join([c for c in msg.content if c.isdigit()])
- try:
- value = floor(float(cleaned_content))
- except Exception as e:
- raise ValueError(
- f"Message {cleaned_content} does not contain a number ({e})")
-
- memoization[msg] = value
- return value
-
- def get(msg: Message) -> int:
- """ Returns the estimated value counted in this message
- """
- return memoization.get(msg, __compute__(msg))
|