Browse Source

ajout de memoization

pull/5/head
Figg 9 months ago
parent
commit
21a75b1e5b

+ 3
- 3
million/analyze/find_holes.py View File

4
 from million.model.hole import Hole
4
 from million.model.hole import Hole
5
 from million.model.message import Message
5
 from million.model.message import Message
6
 from million.model.sequence import Sequence
6
 from million.model.sequence import Sequence
7
-import million.analyze.message_evaluation as msg_ev
7
+import million.analyze.message_evaluation as msg_val
8
 
8
 
9
 
9
 
10
 def compute_sequences(messages: List[Message], accepted_max: int = 1_000_000) -> List[Sequence]:
10
 def compute_sequences(messages: List[Message], accepted_max: int = 1_000_000) -> List[Sequence]:
12
     current = Sequence(start_message=messages[0])
12
     current = Sequence(start_message=messages[0])
13
     
13
     
14
     for message in messages[1:]:
14
     for message in messages[1:]:
15
-        if msg_ev.compute(message) > accepted_max: continue
15
+        if msg_val.get(message) > accepted_max: continue
16
 
16
 
17
-        if msg_ev.compute(message) == current.end() + 1:
17
+        if msg_val.get(message) == current.end() + 1:
18
             current.end_message = message
18
             current.end_message = message
19
         else:
19
         else:
20
             sequences.append(current)
20
             sequences.append(current)

+ 10
- 2
million/analyze/message_evaluation.py View File

1
 from math import floor
1
 from math import floor
2
+from typing import Dict
2
 from million.model.message import Message
3
 from million.model.message import Message
3
 
4
 
5
+memoization: Dict[Message, int] = {}
6
+
4
 # TODO WIP
7
 # TODO WIP
5
 # - DNS to resolve audio, gif, pictures with counts
8
 # - DNS to resolve audio, gif, pictures with counts
6
-def compute(msg: Message) -> int:
9
+def __compute__(msg: Message) -> int:
7
     """ Returns the estimated value counted in this message
10
     """ Returns the estimated value counted in this message
8
     """
11
     """
9
     value = None
12
     value = None
15
     except Exception as e:
18
     except Exception as e:
16
         raise ValueError(
19
         raise ValueError(
17
             f"Message {cleaned_content} does not contain a number ({e})")
20
             f"Message {cleaned_content} does not contain a number ({e})")
18
-    return value
21
+    
22
+    memoization[msg] = value
23
+    return value
24
+
25
+def get(msg: Message) -> int:
26
+    return memoization.get(msg, __compute__(msg))

+ 3
- 0
million/model/message.py View File

49
         dt = datetime.fromtimestamp(self.timestamp_ms / 1000)
49
         dt = datetime.fromtimestamp(self.timestamp_ms / 1000)
50
         dt_str = dt.strftime("%d/%m/%Y, %H:%M:%S")
50
         dt_str = dt.strftime("%d/%m/%Y, %H:%M:%S")
51
         return f"{self.sender_name}({dt_str}) : {self.content}"
51
         return f"{self.sender_name}({dt_str}) : {self.content}"
52
+
53
+    def __hash__(self) -> int:
54
+        return hash(self.sender_name + str(self.timestamp_ms))

+ 3
- 3
million/model/sequence.py View File

4
 import pydantic
4
 import pydantic
5
 
5
 
6
 from million.model.message import Message
6
 from million.model.message import Message
7
-import million.analyze.message_evaluation as msg_ev
7
+import million.analyze.message_evaluation as msg_val
8
 
8
 
9
 
9
 
10
 class Sequence(BaseModel):
10
 class Sequence(BaseModel):
16
         return v or values['start_message'] 
16
         return v or values['start_message'] 
17
 
17
 
18
     def start(self) -> int:
18
     def start(self) -> int:
19
-        return msg_ev.compute(self.start_message)
19
+        return msg_val.get(self.start_message)
20
     
20
     
21
     def end(self) -> int:
21
     def end(self) -> int:
22
-        return msg_ev.compute(self.end_message)
22
+        return msg_val.get(self.end_message)
23
     
23
     
24
     def length(self) -> int:
24
     def length(self) -> int:
25
         return self.end() - self.start() + 1
25
         return self.end() - self.start() + 1

Loading…
Cancel
Save