Exemple #1
0
    def read(self, path, uri=None, **kwargs):
        """Load .srt file as transcription

        Parameters
        ----------
        path : str
            Path to .srt file

        Returns
        -------
        subtitle : Transcription
        """

        # load .srt file using pysrt
        subtitles = pysrt.open(path)

        # initial empty transcription
        transcription = Transcription(uri=uri)

        # keep track of end of previous subtitle
        prev_end = TStart

        # loop on each subtitle in chronological order
        for subtitle in subtitles:

            # convert start/end time into seconds
            start = self._timeInSeconds(subtitle.start)
            end = self._timeInSeconds(subtitle.end)

            # connect current subtitle with previous one
            # if there is a gap between them
            if start > prev_end:
                transcription.add_edge(prev_end, start)
            # warning in case current subtitle starts
            # before previous subtitle ends
            elif start < prev_end:
                warnings.warn('Non-chronological subtitles')

            # split subtitle in multiple speaker lines (only if needed)
            lines = self._split(subtitle.text)

            # loop on subtitle lines
            for line, start_t, end_t in self._duration(lines, start, end):
                transcription.add_edge(start_t, end_t, subtitle=line)

            prev_end = end

        transcription.add_edge(end, TEnd)

        self._loaded = {(uri, 'subtitle'): transcription}

        return self
Exemple #2
0
    def read(self, path, uri=None, **kwargs):
        """Load .srt file as transcription

        Parameters
        ----------
        path : str
            Path to .srt file

        Returns
        -------
        subtitle : Transcription
        """

        # load .srt file using pysrt
        subtitles = pysrt.open(path)

        # initial empty transcription
        transcription = Transcription(uri=uri)

        # keep track of end of previous subtitle
        prev_end = TStart

        # loop on each subtitle in chronological order
        for subtitle in subtitles:

            # convert start/end time into seconds
            start = self._timeInSeconds(subtitle.start)
            end = self._timeInSeconds(subtitle.end)

            # connect current subtitle with previous one
            # if there is a gap between them
            if start > prev_end:
                transcription.add_edge(prev_end, start)
            # raise an error in case current subtitle starts
            # before previous subtitle ends
            elif start < prev_end:
                raise ValueError('Non-chronological subtitles')

            # split subtitle in multiple speaker lines (only if needed)
            lines = self._split(subtitle.text)

            # loop on subtitle lines
            for line, start_t, end_t in self._duration(lines, start, end):
                transcription.add_edge(start_t, end_t, subtitle=line)

            prev_end = end

        transcription.add_edge(end, TEnd)

        self._loaded = {(uri, 'subtitle'): transcription}

        return self
Exemple #3
0
    def read(self, path, **kwargs):

        T.reset()
        transcription = Transcription()

        previousNode = TStart

        arc = []

        for line in self.iterlines(path, encoding='utf-8'):

            # skip comments
            if re.search(r'^;;', line):
                continue

            fields = line.strip().split()

            # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            word = fields[4]

            # remove punctuations
            if not self.punctuation:
                word = re.sub(r'[\.!,;?":]+', ' ', word)
                word = re.sub(r' +', ' ', word)

            word = word.strip()
            if not word:
                continue

            # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            confidence = float(fields[5])

            start_time = round(float(fields[2]), 3)
            duration = round(float(fields[3]), 3)
            end_time = float(start_time)+float(duration)
            end_time = round(end_time, 3)

            if duration == 0:
                node_start = previousNode
                node_end = T()

                if len(arc) == 2:

                    transcription.remove_edge(arc[0], arc[1])
                    transcription.add_edge(arc[0], node_end, **arc_data)
                    node_inter = T()
                    transcription.add_edge(node_end, node_inter, speech=word, confidence=confidence)
                    transcription.add_edge(node_inter, arc[1])
                    arc.append(node_end)
                    arc.append(node_inter)
                    node_end=arc[1]

                elif len(arc) > 2:
                    node_anc_start = arc[0]
                    node_anc_end = arc[1]
                    transcription.remove_edge(arc[len(arc)-1], node_anc_end)
                    transcription.add_edge(arc[len(arc)-1], node_end, speech=word, confidence=confidence)
                    transcription.add_edge(node_end, node_anc_end)
                    arc.append(node_end)
                    node_end=arc[1]

            else:
                addEdge = True
                node_start = T(start_time)
                node_end = T(end_time)
                if previousNode.drifting:
                    if not transcription.has_edge(previousNode, node_start):
                        transcription.add_edge(previousNode, node_start)
                else:
                    if node_start < previousNode:
                        node_start = previousNode
                    elif node_start > previousNode:
                        transcription.add_edge(previousNode, node_start)
                if node_start.anchored and node_end.anchored:
                    if node_start == node_end:
                        addEdge = False
                        node_start = previousNode
                        node_end = T()
                        if len(arc) == 2:
                            transcription.remove_edge(arc[0], arc[1])
                            transcription.add_edge(arc[0], node_end, **arc_data)
                            node_inter = T()
                            transcription.add_edge(node_end, node_inter,
                                                   speech=word,
                                                   confidence=confidence)
                            transcription.add_edge(node_inter, arc[1])
                            arc.append(node_end)
                            arc.append(node_inter)
                            node_end = arc[1]
                        elif len(arc) > 2:
                            node_anc_start = arc[0]
                            node_anc_end = arc[1]
                            transcription.remove_edge(arc[len(arc) - 1],
                                                      node_anc_end)
                            transcription.add_edge(arc[len(arc) - 1], node_end, speech=word, confidence=confidence)
                            transcription.add_edge(node_end, node_anc_end)
                            arc.append(node_end)
                            node_end = arc[1]
                    else:
                        arc = [node_start, node_end]
                        arc_data = {'speech': word, 'confidence': confidence}
                if addEdge:
                    transcription.add_edge(node_start, node_end,
                                           speech=word, confidence=confidence)
            previousNode = node_end

        transcription.add_edge(previousNode, TEnd)

        self._loaded = transcription

        return self
Exemple #4
0
 def empty(self, uri=None, modality=None, **kwargs):
     return Transcription(uri=uri, modality=modality)
Exemple #5
0
def transcription():
    transcription = Transcription()
    transcription.add_edge(3, 5, speech="hi there, I'm Penny",
                           speaker='Penny')
    transcription.add_edge(5, 5.5)
    transcription.add_edge(5.5, 7, speech="hi. I'm Leonard", speaker='Leonard')

    transcription.add_edge(T('A'), T('B'), summary='Leonard meets Penny')

    transcription.add_edge(7, T('C'))

    return transcription
Exemple #6
0
    def get_transcription(self, path2ctm):
        
        T.reset()
        transcription = Transcription()

        with codecs.open(path2ctm, "rt", encoding='utf8') as f:
            lines = f.readlines()

        # last word
        previous = (None, TStart)

        # last word with duration greater than 0
        previous_with_duration = (None, TStart)

        for line in lines:

            # skip comments
            if re.search(r'^;;', line):
                continue

            fields = line.strip().split()

            # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            word = fields[4]

            # remove punctuations
            if not self.punctuation:
                word = re.sub(r'[\.!,;?":]+',' ', word)
                word = re.sub(r' +',' ', word)

            word = word.strip()
            if not word:
                continue

            # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            confidence = fields[5]

            start_time = round(float(fields[2]), 3)
            duration = round(float(fields[3]), 3)
            end_time = float(start_time)+float(duration)
            end_time = round(end_time, 3)


            if duration > 0.:

                start = T(start_time)
                end = T(end_time)

                # if current word starts before the end of previous word,
                # make it start exactly at the end of previous word
                if start < previous_with_duration[1]:
                    start = previous_with_duration[1]

                # if current word starts after the end of previous word,
                # add an empty edge
                if start > previous_with_duration[1]:                
                    transcription.add_edge(previous_with_duration[1], start)

                # add word and update `previous`
                transcription.add_edge(start, end, speech=word, confidence=confidence)
                previous_with_duration = (start, end)
                previous = (start, end)

            else:

                start = previous
                end = T()



            if duration == 0:
                node_start = previousNode
                node_end = T()
            
                if len(arc) == 2:
                    
                    transcription.remove_edge(arc[0], arc[1])
                    transcription.add_edge(arc[0], node_end, **arc_data)
                    node_inter = T()
                    transcription.add_edge(node_end, node_inter, speech=word, confidence=confidence)
                    transcription.add_edge(node_inter, arc[1])
                    arc.append(node_end)
                    arc.append(node_inter)
                    node_end=arc[1]
            
                elif len(arc) > 2:
                    node_anc_start = arc[0]
                    node_anc_end = arc[1]
                    transcription.remove_edge(arc[len(arc)-1], node_anc_end)
                    transcription.add_edge(arc[len(arc)-1], node_end, speech=word, confidence=confidence)
                    transcription.add_edge(node_end, node_anc_end)
                    arc.append(node_end)
                    node_end=arc[1]
            
            else:
                addEdge = True
                node_start = T(start_time)
                node_end = T(end_time)
                if previousNode.drifting:
                    if not transcription.has_edge(previousNode, node_start):
                        transcription.add_edge(previousNode, node_start)
                else:
                    if node_start < previousNode:
                        node_start = previousNode
                    elif node_start > previousNode:
                        transcription.add_edge(previousNode, node_start)
                if node_start.anchored and node_end.anchored:
                    if node_start == node_end:
                        addEdge = False
                        node_start = previousNode
                        node_end = T()
                        if len(arc) == 2:
                            transcription.remove_edge(arc[0], arc[1])
                            transcription.add_edge(arc[0], node_end, **arc_data)
                            node_inter = T()
                            transcription.add_edge(node_end, node_inter, speech=word, confidence=confidence)
                            transcription.add_edge(node_inter, arc[1])
                            arc.append(node_end)
                            arc.append(node_inter)
                            node_end=arc[1]
                        elif len(arc) > 2:
                            node_anc_start = arc[0]
                            node_anc_end = arc[1]
                            transcription.remove_edge(arc[len(arc)-1], node_anc_end)
                            transcription.add_edge(arc[len(arc)-1], node_end, speech=word, confidence=confidence)
                            transcription.add_edge(node_end, node_anc_end)
                            arc.append(node_end)
                            node_end=arc[1]
                    else:
                        arc = [node_start, node_end]
                        arc_data = {'speech':word, 'confidence':confidence}
                if addEdge:
                    transcription.add_edge(node_start, node_end, speech=word, confidence=confidence)
            previousNode=node_end

        transcription.add_edge(previousNode, TEnd)

        return transcription