Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

find_holes.py 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. import million.analyze.message_evaluation as msg_ev
  6. def compute_sequences(messages: List[Message], accepted_max: int = 1_000_000) -> List[Sequence]:
  7. sequences: List[Sequence] = []
  8. current_sequence = Sequence(
  9. start = msg_ev.compute(messages[0]),
  10. start_message=messages[0],
  11. end = msg_ev.compute(messages[0]),
  12. end_message=messages[0]
  13. )
  14. for i in range(1, len(messages)):
  15. message = messages[i]
  16. message_value = msg_ev.compute(message)
  17. if message_value > accepted_max:
  18. continue
  19. if message_value - current_sequence.end == 1:
  20. current_sequence.end = message_value
  21. current_sequence.end_message = message
  22. else:
  23. sequences.append(current_sequence)
  24. current_sequence = Sequence(
  25. start=message_value,
  26. start_message=message,
  27. end=message_value,
  28. end_message=message
  29. )
  30. # order the sequences by start
  31. sequences.sort(key=lambda s: s.start)
  32. merged_sequences: List[Sequence] = []
  33. current_sequence = sequences[0]
  34. for i in range(1, len(sequences)):
  35. sequence = sequences[i]
  36. sequence_start_is_in_current_sequence = current_sequence.start <= sequence.start and current_sequence.end >= sequence.start
  37. sequence_end_is_further = sequence.end > current_sequence.end
  38. sequence_start_is_current_end_or_next = sequence.start == current_sequence.end + 1
  39. if sequence_start_is_in_current_sequence or sequence_start_is_current_end_or_next:
  40. if sequence_end_is_further:
  41. current_sequence.end = sequence.end
  42. current_sequence.end_message = sequence.end_message
  43. else:
  44. merged_sequences.append(current_sequence)
  45. current_sequence = sequence
  46. # Having merged the sequences once, any sequence having start = end can be removed
  47. return [s for s in merged_sequences if s.start != s.end]
  48. def find_holes(messages: List[Message], accepted_max: int = 1_000_000) -> List[Hole]:
  49. """
  50. Find the holes in the conversation
  51. """
  52. merged_sequences = compute_sequences(messages, accepted_max)
  53. holes = []
  54. for i in range(1, len(merged_sequences)):
  55. previous_sequence = merged_sequences[i - 1]
  56. sequence = merged_sequences[i]
  57. if sequence.start - previous_sequence.end > 1:
  58. holes.append(Hole(
  59. start=previous_sequence.end,
  60. end=sequence.start,
  61. start_message=previous_sequence.end_message,
  62. end_message=sequence.start_message
  63. ))
  64. return holes