|
@@ -7,44 +7,53 @@ import million.analyze.message_evaluation as msg_val
|
7
|
7
|
|
8
|
8
|
|
9
|
9
|
def compute_sequences(messages: List[Message], accepted_max: int = 1_000_000) -> List[Sequence]:
|
10
|
|
- sequences: List[Sequence] = []
|
11
|
|
- current = Sequence(start_message=messages[0])
|
|
10
|
+ sequences: List[Sequence] = [Sequence(start_message=messages[0])]
|
12
|
11
|
|
13
|
12
|
for message in messages[1:]:
|
14
|
13
|
if msg_val.get(message) > accepted_max: continue
|
15
|
14
|
|
16
|
|
- if msg_val.get(message) == current.end() + 1:
|
17
|
|
- current.end_message = message
|
|
15
|
+ if msg_val.get(message) == sequences[-1].end() + 1:
|
|
16
|
+ sequences[-1].end_message = message
|
18
|
17
|
else:
|
19
|
|
- sequences.append(current)
|
20
|
|
- current = Sequence(start_message=message)
|
|
18
|
+ sequences.append(Sequence(start_message=message))
|
21
|
19
|
|
22
|
|
- sequences.sort(key=lambda s: s.start())
|
|
20
|
+ return sequences
|
23
|
21
|
|
24
|
|
- merged_sequences: List[Sequence] = []
|
25
|
|
- previous = sequences[0]
|
26
|
|
-
|
27
|
|
- for sequence in sequences[1:]:
|
28
|
|
- if previous.overlaps(sequence):
|
29
|
|
- previous.merge(sequence)
|
|
22
|
+
|
|
23
|
+def merge_duplicates(sequences: List[Sequence]) -> List[Sequence]:
|
|
24
|
+ o_sequences = sorted(sequences, key= lambda s : s.start())
|
|
25
|
+ current = o_sequences[0]
|
|
26
|
+
|
|
27
|
+ result = []
|
|
28
|
+
|
|
29
|
+ for sequence in o_sequences[1:]:
|
|
30
|
+ if current.overlaps(sequence):
|
|
31
|
+ current.merge(sequence)
|
30
|
32
|
else:
|
31
|
|
- merged_sequences.append(previous)
|
32
|
|
- previous = sequence
|
|
33
|
+ result.append(current)
|
|
34
|
+ current = sequence
|
|
35
|
+
|
|
36
|
+ return result
|
33
|
37
|
|
34
|
|
- return [s for s in merged_sequences if s.length() > 1]
|
35
|
38
|
|
36
|
39
|
|
|
40
|
+def invert_sequences(sequences: List[Sequence]) -> List[Sequence]:
|
|
41
|
+ result = []
|
|
42
|
+
|
|
43
|
+ for previous, current in zip(sequences[:-1],sequences[1:]):
|
|
44
|
+ result.append(Sequence(
|
|
45
|
+ start_message=previous.end_message,
|
|
46
|
+ end_message=current.start_message
|
|
47
|
+ ))
|
|
48
|
+
|
|
49
|
+ return result
|
|
50
|
+
|
37
|
51
|
def find_holes(messages: List[Message], accepted_max: int = 1_000_000) -> List[Sequence]:
|
38
|
52
|
"""
|
39
|
53
|
Find the holes in the conversation
|
40
|
54
|
"""
|
41
|
55
|
sequences = compute_sequences(messages, accepted_max)
|
42
|
|
- holes = []
|
43
|
|
-
|
44
|
|
- for previous, current in zip(sequences[:-1],sequences[1:]):
|
45
|
|
- holes.append(Sequence(
|
46
|
|
- start_message=previous.end_message,
|
47
|
|
- end_message=current.start_message
|
48
|
|
- ))
|
|
56
|
+ merged = merge_duplicates(sequences)
|
|
57
|
+ merged = [s for s in merged if s.length() > 1]
|
49
|
58
|
|
50
|
|
- return holes
|
|
59
|
+ return invert_sequences(merged)
|