選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

find_holes.py 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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