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 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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(start_message=messages[0])
  9. for message in messages[1:]:
  10. if msg_ev.compute(message) > accepted_max: continue
  11. if msg_ev.compute(message) == current.end() + 1:
  12. current.end_message = message
  13. else:
  14. sequences.append(current)
  15. current = Sequence(start_message=message)
  16. # order the sequences by start
  17. sequences.sort(key=lambda s: s.start())
  18. merged_sequences: List[Sequence] = []
  19. previous = sequences[0]
  20. for sequence in sequences[1:]:
  21. if previous.overlaps(sequence):
  22. previous.merge(sequence)
  23. else:
  24. merged_sequences.append(previous)
  25. previous = sequence
  26. # Having merged the sequences once, any sequence having start = end can be removed
  27. return [s for s in merged_sequences if s.length() > 1]
  28. def find_holes(messages: List[Message], accepted_max: int = 1_000_000) -> List[Hole]:
  29. """
  30. Find the holes in the conversation
  31. """
  32. merged_sequences = compute_sequences(messages, accepted_max)
  33. holes = []
  34. for i in range(1, len(merged_sequences)):
  35. previous_sequence = merged_sequences[i - 1]
  36. sequence = merged_sequences[i]
  37. if sequence.start() - previous_sequence.end() > 1:
  38. holes.append(Hole(
  39. start=previous_sequence.end(),
  40. end=sequence.start(),
  41. start_message=previous_sequence.end_message,
  42. end_message=sequence.start_message
  43. ))
  44. return holes