@knaw-huc/text-annotation-segmenter 0.4.0 → 0.5.1
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/LICENSE +8 -0
- package/README.md +75 -28
- package/dist/collectGroupSegments.d.ts +6 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +6 -0
- package/dist/segment.d.ts +18 -0
- package/package.json +1 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
Copyright © 2026 KNAW-HuC
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
5
|
+
|
|
6
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,28 +1,41 @@
|
|
|
1
1
|
# @knaw-huc/text-annotation-segmenter
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@knaw-huc/text-annotation-segmenter)
|
|
4
|
+
|
|
5
|
+
Utility functions to render annotations with character offsets in a text.
|
|
4
6
|
|
|
5
7
|
Annotations on a text have a non-hierarchical nature, i.e., they can overlap:
|
|
6
8
|
```text
|
|
7
|
-
|
|
9
|
+
text: abc
|
|
10
|
+
annotation ab: __
|
|
11
|
+
annotation bc: __
|
|
8
12
|
```
|
|
9
13
|
|
|
10
|
-
However, HTML is hierarchical. How to display
|
|
14
|
+
However, HTML is hierarchical. How to display annotations that do not live inside or next to each other, but that cut across each other?
|
|
11
15
|
|
|
12
|
-
The `segment` function creates an array of segments: a flat, non-overlapping list where each segment links to both the text and all the annotations that apply. Each segment translates into a single
|
|
13
|
-
|
|
14
|
-
|
|
16
|
+
The `segment` function creates an array of segments: a flat, non-overlapping list where each segment links to both the text and all the annotations that apply. Each segment translates into a single dom/jsx element. Elements linked to multiple overlapping annotations can now be decorated with their own styling, classes and callbacks:
|
|
17
|
+
```html
|
|
18
|
+
<span class="ab">a</span>
|
|
19
|
+
<span class="ab bc">b</span>
|
|
20
|
+
<span class="bc">c</span>
|
|
21
|
+
```
|
|
15
22
|
|
|
16
23
|
## API
|
|
17
24
|
|
|
18
|
-
|
|
19
|
-
|
|
25
|
+
### Functions
|
|
26
|
+
|
|
27
|
+
- [`segment<T>(text, annotations, getOffsets, getMarkerPosition?): TextSegment<T>[]`](./src/segment.ts) <br /> Split a text into TextSegments with character offsets to the text and a list of applying annotations.
|
|
20
28
|
- [`groupSegments<T>(segments, isGroup, getId): SegmentGroup<T>[]`](src/groupSegments.ts) <br /> Recursively group segments into higher-level units (e.g., words, paragraphs, divs) by collecting all segments that share a matching annotation.
|
|
21
|
-
- [`
|
|
29
|
+
- [`collectGroupSegments<T>(group): TextSegment<T>[]`](src/groupSegments.ts) <br /> Recursively collect all TextSegments from a Group.
|
|
22
30
|
- [`findSegmentOffsets<T>(segments): Map<T, SegmentOffsets>`](src/findSegmentOffsets.ts) <br /> For each annotation, collect the first and last segment index it appears in, keyed by object reference.
|
|
31
|
+
|
|
32
|
+
### Types
|
|
33
|
+
|
|
34
|
+
- [`SegmentGroup<T>`](src/groupSegments.ts) <br /> A `Group<T>` (with annotation and recursive children) or `Ungrouped<T>` (with plain segments).
|
|
23
35
|
- [`SegmentOffsets`](src/findSegmentOffsets.ts) <br /> Start and end segment index of an annotation (excluding end segment).
|
|
36
|
+
- [`TextSegment<T>`](./src/Model.ts) <br /> Start and end offsets of text segment (excluding last character), plus the annotations that apply.
|
|
24
37
|
|
|
25
|
-
##
|
|
38
|
+
## Examples
|
|
26
39
|
|
|
27
40
|
### Create text segments
|
|
28
41
|
|
|
@@ -42,14 +55,17 @@ const ab = {id: 'ab', begin: 0, end: 2};
|
|
|
42
55
|
const bc = {id: 'bc', begin: 1, end: 3};
|
|
43
56
|
|
|
44
57
|
const getOffsets = annotation => annotation;
|
|
58
|
+
|
|
45
59
|
const segments = segment(text, [ab, bc], getOffsets);
|
|
46
60
|
|
|
47
61
|
expect(segments).toEqual([
|
|
48
62
|
{index: 0, begin: 0, end: 1, body: 'a', annotations: [ab]},
|
|
49
|
-
{index: 1, begin: 1, end: 2, body: '
|
|
63
|
+
{index: 1, begin: 1, end: 2, body: 'b', annotations: [ab, bc]},
|
|
50
64
|
{index: 2, begin: 2, end: 3, body: 'c', annotations: [bc]},
|
|
51
65
|
]);
|
|
52
66
|
```
|
|
67
|
+
- More examples: [segment.spec.ts](./src/segment.spec.ts).
|
|
68
|
+
- Benchmarks: [segment.bench.ts](./src/segment.bench.ts).
|
|
53
69
|
|
|
54
70
|
### Group segments
|
|
55
71
|
|
|
@@ -58,9 +74,9 @@ We can use segments to build up a hierachy of elements:
|
|
|
58
74
|
Given the text 'ab' with a section spanning the whole text and a paragraph spanning the first half:
|
|
59
75
|
|
|
60
76
|
```txt
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
77
|
+
text: aabb
|
|
78
|
+
section: ____
|
|
79
|
+
paragraph: __
|
|
64
80
|
```
|
|
65
81
|
|
|
66
82
|
```ts
|
|
@@ -81,24 +97,55 @@ const groups = groupSegments(
|
|
|
81
97
|
isGroup,
|
|
82
98
|
getId,
|
|
83
99
|
);
|
|
100
|
+
const aaSegment = segments[0];
|
|
101
|
+
const bbSegment = segments[1];
|
|
84
102
|
|
|
85
103
|
expect(groups).toEqual([
|
|
86
|
-
{
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
},
|
|
93
|
-
{isGroup: false, segments: [segments[1]]},
|
|
94
|
-
]
|
|
95
|
-
}
|
|
104
|
+
{annotation: section, isGroup: true, children: [
|
|
105
|
+
{annotation: paragraph, isGroup: true, children: [
|
|
106
|
+
{segments: [aaSegment], isGroup: false}
|
|
107
|
+
]},
|
|
108
|
+
{segments: [bbSegment], isGroup: false},
|
|
109
|
+
]}
|
|
96
110
|
]);
|
|
97
111
|
```
|
|
112
|
+
- More examples: [groupSegments.spec.ts](./src/groupSegments.spec.ts).
|
|
113
|
+
- Benchmarks: [groupSegments.bench.ts](./src/groupSegments.bench.ts).
|
|
98
114
|
|
|
99
|
-
---
|
|
100
115
|
|
|
101
|
-
|
|
116
|
+
### Markers
|
|
102
117
|
|
|
103
|
-
-
|
|
104
|
-
|
|
118
|
+
A special case is the marker: an annotation of zero width marking a position in the text. Markers result in zero-width segments, also linking annotations that cover, start or end at that position:
|
|
119
|
+
|
|
120
|
+
```text
|
|
121
|
+
text with marker: a‸b
|
|
122
|
+
annotation a: _
|
|
123
|
+
annotation b: _
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
const text = 'ab';
|
|
128
|
+
const marker: Annotation = {begin: 1, end: 1, id: 'm'};
|
|
129
|
+
const a: Annotation = {begin: 0, end: 1, id: 'a'};
|
|
130
|
+
const b: Annotation = {begin: 1, end: 2, id: 'b'};
|
|
131
|
+
|
|
132
|
+
const segments = segment(text, [marker, a, b], getOffsets);
|
|
133
|
+
const markerSegment = segments[1];
|
|
134
|
+
expect(markerSegment).toEqual(
|
|
135
|
+
{index: 1, begin: 1, end: 1, body: '', annotations: [marker, a]},
|
|
136
|
+
);
|
|
137
|
+
```
|
|
138
|
+
Notice how the marker is 'postfixed' to the previous annotation. When the marker shares its character index with the start and end offsets of annotations, by default the marker segment will only include the annotations that end at that offset (e.g: a note at the end of a paragraph).
|
|
139
|
+
|
|
140
|
+
However, markers can also be prefixed to annotations that share their start offset with the marker, using the `getMarkerPosition` parameter of the `segment` function:
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
const getMarkerPosition = a => a.id === 'm' ? 'prefix' : 'postfix'
|
|
144
|
+
const segments = segment(text, [marker, a, b], getOffsets, getMarkerPosition);
|
|
145
|
+
|
|
146
|
+
const markerSegment = segments[1];
|
|
147
|
+
expect(markerSegment).toEqual(
|
|
148
|
+
{index: 1, begin: 1, end: 1, body: '', annotations: [marker, b]},
|
|
149
|
+
);
|
|
150
|
+
```
|
|
151
|
+
More examples: [segment.spec.ts](./src/segment.spec.ts#L118).
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export type { AnnotationSegmentsByChar } from './segment.ts';
|
|
|
3
3
|
export { findSegmentOffsets } from './findSegmentOffsets.ts';
|
|
4
4
|
export type { SegmentOffsets } from './findSegmentOffsets.ts';
|
|
5
5
|
export { groupSegments } from './groupSegments.ts';
|
|
6
|
+
export { collectGroupSegments } from './collectGroupSegments.ts';
|
|
6
7
|
export type { Group, SegmentGroup } from './groupSegments.ts';
|
|
7
8
|
export type { Offsets, TextSegment, SegmentIndex, MarkerPosition, } from './Model.ts';
|
|
8
9
|
export type { GetOffsets } from './GetOffsets.ts';
|
package/dist/index.js
CHANGED
|
@@ -95,7 +95,13 @@ function createUngrouped(segments) {
|
|
|
95
95
|
segments
|
|
96
96
|
};
|
|
97
97
|
}
|
|
98
|
+
function collectGroupSegments(group2) {
|
|
99
|
+
return group2.children.flatMap(
|
|
100
|
+
(child) => child.isGroup ? collectGroupSegments(child) : child.segments
|
|
101
|
+
);
|
|
102
|
+
}
|
|
98
103
|
export {
|
|
104
|
+
collectGroupSegments,
|
|
99
105
|
findSegmentOffsets,
|
|
100
106
|
groupSegments,
|
|
101
107
|
segment
|
package/dist/segment.d.ts
CHANGED
|
@@ -3,6 +3,24 @@ import { GetOffsets } from './GetOffsets.ts';
|
|
|
3
3
|
import { GetMarkerPosition } from './GetMarkerPosition.ts';
|
|
4
4
|
/**
|
|
5
5
|
* Split a text into {@link TextSegment}s with character offsets and a list of applying annotations.
|
|
6
|
+
*
|
|
7
|
+
* @param text Text to split into segments
|
|
8
|
+
*
|
|
9
|
+
* @param annotations Annotations linked to text using character offsets
|
|
10
|
+
*
|
|
11
|
+
* @param getOffsets Find the character begin and end index of an annotation
|
|
12
|
+
*
|
|
13
|
+
* @param getMarkerPosition Should a marker segment be prefixed or postfixed?
|
|
14
|
+
* - 'prefix': include annotations that start at marker offset
|
|
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.
|
|
6
24
|
*/
|
|
7
25
|
export declare function segment<T>(text: string, annotations: T[], getOffsets: GetOffsets<T>, getMarkerPosition?: GetMarkerPosition<T>): TextSegment<T>[];
|
|
8
26
|
export type AnnotationSegmentsByChar<T> = {
|