-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathtext_tools.tsx
More file actions
182 lines (173 loc) · 6.43 KB
/
Copy pathtext_tools.tsx
File metadata and controls
182 lines (173 loc) · 6.43 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { TbHammer } from 'react-icons/tb';
import { IconButton } from '../components/button';
import { EditorWithWebsocket } from './automerge_websocket_editor';
import { Document, Paragraph } from '../editor/types';
import { Popup } from '../components/popup';
import { primitiveWithClassname } from '../styled';
export const MenuItemButton = primitiveWithClassname('button', [
'hover:bg-gray-200 dark:hover:bg-neutral-700',
'rounded-md',
'w-full',
'text-left',
'px-2',
'py-1',
'block',
]);
function mergeSameSpeakerParagraphs(doc: Document) {
const mergePoints: number[] = [];
for (let i = 0; i < doc.children.length - 1; i++) {
const paragraph = doc.children[i];
const nextParagraph = doc.children[i + 1];
if (paragraph.speaker == nextParagraph.speaker) {
mergePoints.push(i);
}
}
let removed = 0;
mergePoints.forEach((index) => {
const i = index - removed;
doc.children[i].children.push(...JSON.parse(JSON.stringify(doc.children[i + 1].children)));
doc.children.splice(i + 1, 1);
removed++;
});
}
const punctuations = ['.', '?', '!'];
const non_punctuations = ['...'];
function containsSentenceEnd(text: string) {
return (
punctuations.some((punct) => text.includes(punct)) &&
!non_punctuations.some((np) => text.includes(np))
);
}
export function TextTools({ editor }: { editor: EditorWithWebsocket }) {
return (
<Popup
button={<IconButton icon={TbHammer} label={'text tools'} />}
onClick={(e) => {
e.preventDefault();
}}
>
<MenuItemButton
onClick={() => {
editor.update(mergeSameSpeakerParagraphs);
}}
>
Reflow to One Paragraph per Speaker
</MenuItemButton>
<MenuItemButton
onClick={() => {
editor.update((doc: Document) => {
// stategy: we first merge everything that could possibly be merged...
mergeSameSpeakerParagraphs(doc);
// ...and only then break up on sentence boundaries
const newChildren: Paragraph[] = [];
doc.children.forEach((paragraph) => {
let currentParagraph = {
...paragraph,
children: [] as { text: string }[],
};
paragraph.children.forEach((token) => {
currentParagraph.children.push(JSON.parse(JSON.stringify(token)));
if (containsSentenceEnd(token.text)) {
newChildren.push(currentParagraph);
currentParagraph = {
...paragraph,
children: [],
};
}
});
if (currentParagraph.children.length > 0) {
newChildren.push(currentParagraph);
}
});
doc.children = newChildren;
});
}}
>
Reflow to One Paragraph per Sentence
</MenuItemButton>
<MenuItemButton
onClick={() => {
// this strategy tries to split paragraphs at sentence boundaries, but only if there is a pause between the sentences
// or the paragraphs would become too long.
const initial = 2;
const decay = 0.95;
const getPause = (i: number, paragraph: Paragraph) => {
const token = paragraph.children[i];
const nextToken = paragraph.children[i + 1];
if (nextToken?.start !== undefined && token?.end !== undefined) {
return nextToken.start - token.end;
}
return 0;
};
editor.update((doc: Document) => {
mergeSameSpeakerParagraphs(doc);
const newChildren: Paragraph[] = [];
const addNewChild = (paragraph: Paragraph) => {
// if the paragraph is very long and does not contain any sentence ends, we still want to break it up
if (paragraph.children.length <= 100) {
newChildren.push(paragraph);
} else {
const silences = paragraph.children
.map((x, i) => ({ ...x, pause: getPause(i, paragraph) }))
.filter((token) => token.text.includes(','))
.map((token) => token.pause);
silences.sort();
const thresholdIndex = Math.floor(paragraph.children.length / 100); // aim for paragraphs of max ~50 tokens
const threshold = silences[silences.length - 1 - thresholdIndex];
let currentParagraph = {
...paragraph,
children: [] as { text: string }[],
};
paragraph.children.forEach((token, i) => {
currentParagraph.children.push(JSON.parse(JSON.stringify(token)));
if (
getPause(i, paragraph) >= threshold &&
token.text.includes(',') &&
currentParagraph.children.length > 3
) {
newChildren.push(currentParagraph);
currentParagraph = {
...paragraph,
children: [],
};
}
});
if (currentParagraph.children.length > 0) {
newChildren.push(currentParagraph);
}
}
};
doc.children.forEach((paragraph) => {
let minPauseBetweenSentences = initial; // this gets reduced with every additional token
let currentParagraph = {
...paragraph,
children: [] as { text: string }[],
};
paragraph.children.forEach((token, i) => {
currentParagraph.children.push(JSON.parse(JSON.stringify(token)));
minPauseBetweenSentences *= decay;
if (
getPause(i, paragraph) >= minPauseBetweenSentences &&
containsSentenceEnd(token.text)
) {
addNewChild(currentParagraph);
minPauseBetweenSentences = initial;
currentParagraph = {
...paragraph,
children: [],
};
}
});
if (currentParagraph.children.length > 0) {
addNewChild(currentParagraph);
}
});
doc.children = newChildren;
});
}}
>
Smart Reflow ✨
</MenuItemButton>
</Popup>
);
}