summaryrefslogtreecommitdiff
path: root/animism-align/frontend/app/utils/transcript.utils.js
blob: 2a089a0fb03e51cad5c391d9b759a476c39f9bc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { store, history, dispatch } from 'app/store'
import {
  TEXT_ANNOTATION_TYPES,
  MEDIA_ANNOTATION_TYPES,
  INLINE_UTILITY_ANNOTATION_TYPES,
  FULLSCREEN_UTILITY_ANNOTATION_TYPES,
} from 'app/constants'

export const buildParagraphs = (annotationOrder, sectionCount, footnoteCount) => {
  const state = store.getState()
  const { lookup: annotationLookup } = state.annotation.index
  const { lookup: paragraphLookup } = state.paragraph.index
  let currentParagraph = {}
  const paragraphs = []
  const footnotes = []

  sectionCount = (sectionCount || 0)
  footnoteCount = (footnoteCount || 0)

  // loop over the annotations in time order
  annotationOrder.forEach((annotation_id, i) => {
    // fetch annotation and paragraph
    const annotation = annotationLookup[annotation_id]
    const paragraph = paragraphLookup[annotation.paragraph_id]

    // if this annotation is a utility (curtain, gallery instructions), then skip it
    if (FULLSCREEN_UTILITY_ANNOTATION_TYPES.has(annotation.type)) {
      return
    }

    // if this annotation is an inline utility (intro div) don't skip it
    if (INLINE_UTILITY_ANNOTATION_TYPES.has(annotation.type)) {
      if (!annotation.settings.hideCitation) {
        const item = {
          id: ('index_' + annotation.id),
          type: annotation.type,
          start_ts: annotation.start_ts,
          end_ts: 0,
          annotations: [annotation],
        }
        if (annotation.type === 'intro') {
          paragraphs.unshift(item)
        } else {
          paragraphs.push(item)
        }
      }
      return
    }

    // if this annotation is media, then insert it after the current paragraph
    if (MEDIA_ANNOTATION_TYPES.has(annotation.type)) {
      // add option to hide the citation from the transcript
      if (!annotation.settings.hideCitation) {
        paragraphs.push({
          id: ('index_' + i),
          type: annotation.type,
          start_ts: annotation.start_ts,
          end_ts: 0,
          isMedia: true,
          annotations: [annotation],
        })
      }
      return
    }

    // if this annotation is from a different paragraph, make a new paragraph
    if (annotation.type === 'section_heading' || annotation.type === 'heading_text' || annotation.paragraph_id !== currentParagraph.id) {
      const paragraph_type = getParagraphType(annotation, paragraph)
      currentParagraph = {
        id: annotation.paragraph_id || ('index_' + i),
        type: paragraph_type,
        start_ts: annotation.start_ts,
        end_ts: 0,
        annotations: [],
        settings: paragraph ? paragraph.settings : {},
      }
      if (paragraph && paragraph.type === 'hidden') {
        currentParagraph.hidden = true
      }
      if (annotation.type === 'section_heading') {
        currentParagraph.sectionIndex = sectionCount++
        currentParagraph.id = 'section_' + currentParagraph.sectionIndex
        if (annotation.settings.hidden) {
          currentParagraph.hidden = true
        }
      }
      paragraphs.push(currentParagraph)
    }

    // accumulate footnotes
    if (annotation.type === 'footnote') {
      // bump footnote count and attach it to this annotation, so we can find it later
      footnoteCount += 1
      annotation.footnote_id = footnoteCount
      // set annotation start point to the start of the previous sentence
      annotation.start_ts = currentParagraph.annotations[currentParagraph.annotations.length - 1].start_ts
      footnotes.push(annotation)
    }

    // if this annotation is a paragraph_end, set the end timestamp
    if (annotation.type === 'paragraph_end') {
      currentParagraph.end_ts = annotation.start_ts
    } // otherwise, just append this annotation to the paragraph
    else {
      currentParagraph.annotations.push(annotation)
    }
  })

  // finally, go over the paragraphs to fix some timestamps
  for (let i = 0; i < (paragraphs.length - 1); i++) {
    // update the end_ts, if none is set
    if (!paragraphs[i].end_ts) {
      paragraphs[i].end_ts = paragraphs[i+1].start_ts - 0.1
    }
    // push the timestamp for media to the next paragraph
    if (paragraphs[i].isMedia) {
      paragraphs[i].start_ts = paragraphs[i+1].start_ts - 0.01
    }
  }
  return { paragraphs, footnotes }
}

const getParagraphType = (annotation, paragraph) => {
  if (annotation.type === 'section_heading') {
    return annotation.type
  }
  if (annotation.type === 'heading_text') {
    return annotation.type
  }
  if (!paragraph) {
    return annotation.type
  }
  return paragraph.type
}