This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSplitText.js
105 lines (97 loc) · 3.56 KB
/
SplitText.js
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
// @flow
import SegmentedText from "./SegmentedText";
import AnnotatedText from "./AnnotatedText";
export type Splitter = (string) => number[];
export default class SplitText {
annotatedText: AnnotatedText;
splitter: Splitter;
_texts: SegmentedText[] | null;
_textsId: string | null;
_textsFinalPositions: number[];
constructor(annotatedText: AnnotatedText, splitter: Splitter) {
this.annotatedText = annotatedText;
this.splitter = splitter;
this._texts = null;
this._textsId = null;
this._textsFinalPositions = [];
}
get texts(): SegmentedText[] {
if (!this.annotatedText) {
return [];
}
if (
!this._texts ||
this._textsId !== this.annotatedText.getUniqueId()
) {
this._textsFinalPositions = [];
const segmentedText = this.annotatedText.segmentedText;
const textString = segmentedText.getText();
let splitPositions = this.splitter(textString).filter(
(l) => l !== 0
);
if (splitPositions.length === 0) {
this._textsFinalPositions.push(textString.length);
return [segmentedText];
}
let lastPosition = splitPositions[splitPositions.length - 1];
if (lastPosition + 1 < textString.length) {
splitPositions.push(textString.length - 1);
}
const segments = segmentedText.segments;
let startIndex = 0;
let texts = [];
for (let i = 0; i < splitPositions.length; i++) {
const position = splitPositions[i];
const endIndex = segmentedText.indexOfSegmentAtPosition(
position - 1
);
let textSegments;
if (i == splitPositions.length - 1) {
// final position
textSegments = segments.slice(startIndex);
} else {
textSegments = segments.slice(startIndex, endIndex + 1);
}
const text = new SegmentedText(textSegments);
texts.push(text);
startIndex = endIndex + 1;
if (endIndex >= 0) {
const finalSegment = segments[endIndex];
this._textsFinalPositions.push(finalSegment.end);
}
}
this._texts = texts.filter((l) => l.segments.length > 0);
this._textsId = this.annotatedText.getUniqueId();
}
return this._texts || [];
}
getTextsFinalPositions(): number[] {
this.texts;
return this._textsFinalPositions;
}
/**
* Return the index of the text portion that contains the given position.
*
* @param position
* @returns number
*/
getTextIndexOfPosition(position: number): number {
const textsFinalPositions = this.getTextsFinalPositions();
let lastPosition = 0;
let textIndex = null;
for (let i = 0; i < textsFinalPositions.length; i++) {
let endPosition = textsFinalPositions[i];
if (position >= lastPosition && position <= endPosition) {
textIndex = i;
break;
}
lastPosition = endPosition;
}
if (textIndex === null) {
// Likely an insertion at the end of a text
console.warn("no index for position %d", position);
textIndex = textsFinalPositions.length - 1;
}
return textIndex;
}
}