@knaw-huc/text-annotation-segmenter 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Model.d.ts +0 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.js +11 -14
- package/dist/segment.d.ts +3 -13
- package/package.json +1 -1
- package/dist/GetMarkerPosition.d.ts +0 -2
package/dist/Model.d.ts
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,5 @@ export { findSegmentRange } from './findSegmentRange.ts';
|
|
|
4
4
|
export { groupSegments } from './groupSegments.ts';
|
|
5
5
|
export { collectGroupSegments } from './collectGroupSegments.ts';
|
|
6
6
|
export type { Group, SegmentGroup } from './groupSegments.ts';
|
|
7
|
-
export type { TextPosition, TextSegment, SegmentIndex,
|
|
7
|
+
export type { TextPosition, TextSegment, SegmentIndex, SegmentRange } from './Model.ts';
|
|
8
8
|
export type { GetOffsets } from './GetOffsets.ts';
|
|
9
|
-
export type { GetMarkerPosition } from './GetMarkerPosition.ts';
|
package/dist/index.js
CHANGED
|
@@ -1,16 +1,10 @@
|
|
|
1
|
-
function segment(text, annotations, getOffsets
|
|
1
|
+
function segment(text, annotations, getOffsets) {
|
|
2
2
|
const segments = [];
|
|
3
3
|
let segmentCounter = -1;
|
|
4
4
|
const offsetMap = /* @__PURE__ */ new Map(), getOrCreateOffset = (charIndex) => {
|
|
5
5
|
let offset = offsetMap.get(charIndex);
|
|
6
6
|
return offset || (offset = { charIndex, starting: [], ending: [] }, offsetMap.set(charIndex, offset)), offset;
|
|
7
|
-
}
|
|
8
|
-
index: ++segmentCounter,
|
|
9
|
-
start: charIndex,
|
|
10
|
-
end: charIndex,
|
|
11
|
-
value: "",
|
|
12
|
-
annotations: [...markers, ...activeAnnotations]
|
|
13
|
-
});
|
|
7
|
+
};
|
|
14
8
|
getOrCreateOffset(0), getOrCreateOffset(text.length);
|
|
15
9
|
for (const annotation of annotations) {
|
|
16
10
|
const offsets = getOffsets(annotation), isMarker = offsets.start === offsets.end;
|
|
@@ -35,15 +29,18 @@ function segment(text, annotations, getOffsets, getMarkerPosition = () => "postf
|
|
|
35
29
|
});
|
|
36
30
|
}
|
|
37
31
|
lastOffset = offset.charIndex;
|
|
38
|
-
const
|
|
39
|
-
for (const annotation of offset.starting)
|
|
40
|
-
offset.ending.includes(annotation) && (getMarkerPosition(annotation) === "prefix" ? prefixMarkers.push(annotation) : postfixMarkers.push(annotation));
|
|
41
|
-
postfixMarkers.length && segments.push(createMarkerSegment(postfixMarkers, offset.charIndex));
|
|
32
|
+
const markers = [];
|
|
42
33
|
for (const annotation of offset.starting)
|
|
43
|
-
offset.ending.includes(annotation)
|
|
34
|
+
offset.ending.includes(annotation) ? markers.push(annotation) : activeAnnotations.add(annotation);
|
|
35
|
+
markers.length && segments.push({
|
|
36
|
+
index: ++segmentCounter,
|
|
37
|
+
start: offset.charIndex,
|
|
38
|
+
end: offset.charIndex,
|
|
39
|
+
value: "",
|
|
40
|
+
annotations: [...markers, ...activeAnnotations]
|
|
41
|
+
});
|
|
44
42
|
for (const annotation of offset.ending)
|
|
45
43
|
activeAnnotations.delete(annotation);
|
|
46
|
-
prefixMarkers.length && segments.push(createMarkerSegment(prefixMarkers, offset.charIndex));
|
|
47
44
|
}
|
|
48
45
|
return segments;
|
|
49
46
|
}
|
package/dist/segment.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { TextSegment } from './Model';
|
|
2
2
|
import { GetOffsets } from './GetOffsets.ts';
|
|
3
|
-
import { GetMarkerPosition } from './GetMarkerPosition.ts';
|
|
4
3
|
/**
|
|
5
4
|
* Split a text into {@link TextSegment}s with character offsets and a list of applying annotations.
|
|
6
5
|
*
|
|
@@ -10,19 +9,10 @@ import { GetMarkerPosition } from './GetMarkerPosition.ts';
|
|
|
10
9
|
*
|
|
11
10
|
* @param getOffsets Find the character begin and end index of an annotation
|
|
12
11
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* - 'postfix': include annotations that end at marker offset
|
|
16
|
-
*
|
|
17
|
-
* For example, consider two paragraphs: <p1>aa</p1><marker/><p2>bb</p2>
|
|
18
|
-
* The annotation <marker/> shares the end character index with annotation <p1>,
|
|
19
|
-
* and the begin character index with annotation <p2>:
|
|
20
|
-
* - with 'postfix': segment (2,2) will contain annotations [marker, p1]
|
|
21
|
-
* - with 'prefix': segment (2,2) will contain annotations [marker, p2]
|
|
22
|
-
*
|
|
23
|
-
* By default, markers are postfixed.
|
|
12
|
+
* Marker segments include all annotations present at that position:
|
|
13
|
+
* ending, spanning, and starting. Consumers can filter these as needed.
|
|
24
14
|
*/
|
|
25
|
-
export declare function segment<T>(text: string, annotations: T[], getOffsets: GetOffsets<T
|
|
15
|
+
export declare function segment<T>(text: string, annotations: T[], getOffsets: GetOffsets<T>): TextSegment<T>[];
|
|
26
16
|
export type AnnotationSegmentsByChar<T> = {
|
|
27
17
|
charIndex: number;
|
|
28
18
|
starting: T[];
|
package/package.json
CHANGED