@@ -79,11 +79,6 @@ and handle edge case where it doesn't find a match
79
79
```
80
80
*/
81
81
function groupWordsInParagraphsBySpeakers ( words , segments ) {
82
- // add speakers to each word
83
- // const wordsWithSpeakers = addSpeakerToEachWord(words, segments);
84
- // group words by speakers sequentially
85
- // const result = groupWordsBySpeaker(wordsWithSpeakers);
86
-
87
82
const result = addWordsToSpeakersParagraphs ( words , segments ) ;
88
83
89
84
return result ;
@@ -96,39 +91,32 @@ function addWordsToSpeakersParagraphs (words, segments) {
96
91
let previousSegmentIndex = 0 ;
97
92
let paragraph = { words : [ ] , text : '' , speaker : '' } ;
98
93
words . forEach ( ( word ) => {
99
- // console.log(word);
100
94
currentSegment = findSegmentForWord ( word , segments ) ;
101
95
// if a segment exists for the word
102
- if ( currentSegment ) {
103
- currentSegmentIndex = segments . indexOf ( currentSegment ) ;
104
- if ( currentSegmentIndex === previousSegmentIndex ) {
105
- paragraph . words . push ( word ) ;
106
- paragraph . text += word . text + ' ' ;
107
- paragraph . speaker = currentSegment . speaker ;
108
- }
109
- else {
110
- previousSegmentIndex = currentSegmentIndex ;
111
- results . push ( paragraph ) ;
112
- paragraph = { words : [ ] , text : '' , speaker : '' } ;
113
- }
96
+ // if (currentSegment) {
97
+
98
+ currentSegmentIndex = segments . indexOf ( currentSegment ) ;
99
+ if ( currentSegmentIndex === previousSegmentIndex ) {
100
+ paragraph . words . push ( word ) ;
101
+ paragraph . text += word . text + ' ' ;
102
+ paragraph . speaker = currentSegment . speaker ;
114
103
}
115
- // TODO: handling edge case orphan words
116
- // TODO: this needs to be tested/check with input sequence that has
117
- // orphan words
118
104
else {
119
- currentSegment = 'UKN' ;
120
- if ( currentSegmentIndex === previousSegmentIndex ) {
121
- paragraph . words . push ( word ) ;
122
- paragraph . text += word . text + ' ' ;
123
- paragraph . speaker = currentSegment . speaker ;
124
- }
125
- else {
126
- previousSegmentIndex = currentSegmentIndex ;
127
- results . push ( paragraph ) ;
128
- paragraph = { words : [ ] , text : '' , speaker : '' } ;
129
- }
105
+ previousSegmentIndex = currentSegmentIndex ;
106
+ paragraph . text . trim ( ) ;
107
+ results . push ( paragraph ) ;
108
+ paragraph = { words : [ ] , text : '' , speaker : '' } ;
109
+ paragraph . words . push ( word ) ;
110
+ paragraph . text += word . text + ' ' ;
111
+ paragraph . speaker = currentSegment . speaker ;
130
112
}
131
113
} ) ;
114
+ results . push ( paragraph ) ;
115
+ const resultWordCount = results . reduce ( reduceFunction , 0 ) ;
116
+
117
+ function reduceFunction ( total , currentParagraph ) {
118
+ return total + currentParagraph . words . length ;
119
+ } ;
132
120
133
121
results = results . filter ( ( p ) => {
134
122
return p . words . length !== 0 ;
0 commit comments