You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

find_holes.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from typing import List
  2. from million.model.hole import Hole
  3. from million.model.message import Message
  4. from million.model.sequence import Sequence
  5. def compute_sequences(messages: List[Message], accepted_max: int = 1_000_000) -> List[Sequence]:
  6. sequences: List[Sequence] = []
  7. current_sequence = Sequence(
  8. start=messages[0].get_counted_value(),
  9. start_message=messages[0],
  10. end=messages[0].get_counted_value(),
  11. end_message=messages[0]
  12. )
  13. for i in range(1, len(messages)):
  14. message = messages[i]
  15. message_value = message.get_counted_value()
  16. if message_value > accepted_max:
  17. continue
  18. if message_value - current_sequence.end == 1:
  19. current_sequence.end = message_value
  20. current_sequence.end_message = message
  21. else:
  22. sequences.append(current_sequence)
  23. current_sequence = Sequence(
  24. start=message_value,
  25. start_message=message,
  26. end=message_value,
  27. end_message=message
  28. )
  29. # order the sequences by start
  30. sequences.sort(key=lambda s: s.start)
  31. merged_sequences: List[Sequence] = []
  32. current_sequence = sequences[0]
  33. for i in range(1, len(sequences)):
  34. sequence = sequences[i]
  35. sequence_start_is_in_current_sequence = current_sequence.start <= sequence.start and current_sequence.end >= sequence.start
  36. sequence_end_is_further = sequence.end > current_sequence.end
  37. sequence_start_is_current_end_or_next = sequence.start == current_sequence.end + 1
  38. if sequence_start_is_in_current_sequence or sequence_start_is_current_end_or_next:
  39. if sequence_end_is_further:
  40. current_sequence.end = sequence.end
  41. current_sequence.end_message = sequence.end_message
  42. else:
  43. merged_sequences.append(current_sequence)
  44. current_sequence = sequence
  45. # Having merged the sequences once, any sequence having start = end can be removed
  46. return [s for s in merged_sequences if s.start != s.end]
  47. def find_holes(messages: List[Message], accepted_max: int = 1_000_000) -> List[Hole]:
  48. """
  49. Find the holes in the conversation
  50. """
  51. merged_sequences = compute_sequences(messages, accepted_max)
  52. holes = []
  53. for i in range(1, len(merged_sequences)):
  54. previous_sequence = merged_sequences[i - 1]
  55. sequence = merged_sequences[i]
  56. if sequence.start - previous_sequence.end > 1:
  57. holes.append(Hole(
  58. start=previous_sequence.end,
  59. end=sequence.start,
  60. start_message=previous_sequence.end_message,
  61. end_message=sequence.start_message
  62. ))
  63. return holes
  64. def find_holesV2(messages: List[Message]) -> List[Hole]:
  65. current = 1
  66. msg_idx = 0
  67. threshold = 1000
  68. result = []
  69. while True:
  70. msgCurrent = next((m for m in messages if m.get_counted_value() == current), None)
  71. if msgCurrent:
  72. if len(result) > 0 and result[-1].end < 1:
  73. result[-1].end = current-1
  74. result[-1].end_message = msgCurrent
  75. messages.remove(msgCurrent)
  76. current += 1
  77. else:
  78. if len(result) == 0 or result[-1].end > 0:
  79. hole = Hole(
  80. start = current,
  81. end = 0,
  82. start_message = Message(sender_name='',timestamp_ms=0),
  83. end_message = Message(sender_name='',timestamp_ms=0)
  84. )
  85. result.append(hole)
  86. elif current - result[-1].start > threshold:
  87. break
  88. return result