@knaw-huc/text-annotation-segmenter 0.4.0 → 0.5.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/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
- Utility functions to help render overlapping annotations in a text.
3
+ [![npm version](https://img.shields.io/npm/v/@knaw-huc/text-annotation-segmenter.svg?color=green)](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
- Aa<bc>bb<cd>cc</bc>dd<cd>ee.
9
+ text: abc
10
+ annotation ab: __
11
+ annotation bc: __
8
12
  ```
9
13
 
10
- However, HTML is hierarchical. How to display these kinds of annotations that do not live inside or next to each other, but cut across each other?
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 DOM element. Elements linked to multiple overlapping annotations can now be decorated with their own styling, classes and callbacks.
13
-
14
- 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 all annotations that start at or span across that position.
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
- - [`segment<T>(text, annotations, getOffsets): TextSegment<T>[]`](./src/segment.ts) <br /> Split a text into TextSegments with char offsets to the text and a list of applying annotations.
19
- - [`TextSegment<T>`](./src/Model.ts) <br /> Start and end offsets of text segment (excluding last character), plus the annotations that apply.
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
- - [`SegmentGroup<T>`](src/groupSegments.ts) <br /> A `Group<T>` (with annotation and recursive children) or `Ungrouped<T>` (with plain segments).
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
- ## Example
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: 'ab', annotations: [ab, bc]},
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
- text: aabb
62
- section: ____
63
- paragraph: __
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
- isGroup: true, annotation: section, children: [
88
- {
89
- isGroup: true, annotation: paragraph, children: [
90
- {isGroup: false, segments: [segments[0]]}
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
- More examples:
116
+ ### Markers
102
117
 
103
- - For edge cases, see: [segment.spec.ts](./src/segment.spec.ts).
104
- - For benchmarks, see [segment.bench.ts](./src/segment.bench.ts).
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).
@@ -0,0 +1,6 @@
1
+ import { TextSegment } from './Model.ts';
2
+ import { Group } from './groupSegments.ts';
3
+ /**
4
+ * Recursively collect all {@link TextSegment}s from a {@link Group}
5
+ */
6
+ export declare function collectGroupSegments<T>(group: Group<T>): TextSegment<T>[];
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> = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knaw-huc/text-annotation-segmenter",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/knaw-huc/text-annotation-segmenter.git"