{"size": 1794, "ext": "js", "max_stars_repo_path": "src/adapters/KaldiAdapter.js", "max_stars_repo_name": "bbc/transcript-model", "max_stars_repo_stars_event_min_datetime": "2017-03-10T07:18:36.000Z", "max_stars_repo_stars_event_max_datetime": "2019-09-10T18:50:11.000Z", "max_issues_repo_path": "src/adapters/KaldiAdapter.js", "max_issues_repo_name": "alexnorton/transcript-model", "max_issues_repo_issues_event_min_datetime": "2017-02-23T17:24:13.000Z", "max_issues_repo_issues_event_max_datetime": "2018-02-20T17:52:40.000Z", "max_forks_repo_path": "src/adapters/KaldiAdapter.js", "max_forks_repo_name": "alexnorton/transcript-model", "max_forks_repo_forks_event_min_datetime": "2017-05-13T14:19:44.000Z", "max_forks_repo_forks_event_max_datetime": "2017-12-01T03:52:24.000Z"}
{"max_stars_count": 12, "max_issues_count": 3, "max_forks_count": 3, "avg_line_length": 29.4098360656, "max_line_length": 96, "alphanum_fraction": 0.5774804905}
import Immutable from 'immutable';

import Speaker from '../Speaker';
import TranscriptSegment from '../TranscriptSegment';
import TranscriptWord from '../TranscriptWord';
import Transcript from '../Transcript';

class KaldiAdapter {
  static parse(transcriptJson, segmentsJson) {
    const speakerIdMap = {};
    const speakers = [];

    const words = transcriptJson.words.reduce((currentWords, word) => {
      let segmentIndex = segmentsJson.segments.findIndex(segment =>
        word.start >= segment.start && word.end < segment.start + segment.duration);

      if (segmentIndex === -1) {
        if (currentWords[currentWords.length - 1]) {
          ({ segmentIndex } = currentWords[currentWords.length - 1]);
        } else {
          segmentIndex = 0;
        }
      }

      return [...currentWords, { ...word, segmentIndex }];
    }, []);

    const segments = new Immutable.List(segmentsJson.segments.reduce(
      (list, segment, segmentIndex) => {
        const segmentWords = words.filter(word => word.segmentIndex === segmentIndex);

        if (segmentWords.length === 0) {
          return list;
        }

        if (!speakerIdMap[segment.speaker['@id']]) {
          speakerIdMap[segment.speaker['@id']] = speakers.push(new Speaker({ name: null })) - 1;
        }

        return [
          ...list,
          new TranscriptSegment({
            speaker: speakerIdMap[segment.speaker['@id']],
            words: new Immutable.List(segmentWords.map(w =>
              new TranscriptWord({
                text: w.punct,
                start: w.start,
                end: w.end,
              }))),
          }),
        ];
      },
      [],
    ));

    return new Transcript({ speakers: new Immutable.List(speakers), segments });
  }
}

export default KaldiAdapter;
