Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fill_gaps function #86

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions pyannote/core/annotation.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,36 @@ def __contains__(self, included: Union[Segment, Timeline]):
"""
return included in self.get_timeline(copy=False)

def fill_gaps(self) -> "Annotation":
"""Fill the gaps of the annotation assigning the closest label

A simple illustration:

annotation
|------| |------| |------|
| spk0 | | spk1 | | spk0 |

filled `Annotation`
|----------------------------------------------------|
| spk0 | spk0 | spk1 | spk1 | spk1 | spk0 | spk0 |

Returns
-------
filled : Annotation
Filled annotation

"""
mapping = dict()
filled = self.__class__(uri=self.uri, modality=self.modality)
for segment, _, label in self.itertracks(yield_label=True):
mapping[segment.start] = label
mapping[segment.end] = label
gaps = [[Segment(segment.start, segment.middle), Segment(segment.middle, segment.end)] for segment in self.get_timeline().gaps()]
for first_half, second_half in gaps:
filled[first_half] = mapping[first_half.start]
filled[second_half] = mapping[second_half.end]
return filled.support()

def _iter_rttm(self) -> Iterator[Text]:
"""Generate lines for an RTTM file for this annotation

Expand Down