@knaw-huc/text-annotation-segmenter 0.1.0 → 0.2.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/README.md +3 -1
- package/dist/Model.d.ts +6 -11
- package/dist/index.d.ts +3 -1
- package/dist/index.js +24 -16
- package/dist/mapAnnotationSegmentRanges.d.ts +12 -0
- package/dist/segment.d.ts +5 -5
- package/package.json +2 -2
- package/README.html +0 -41
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@ However, HTML is hierarchical. How to display these kinds of annotations that do
|
|
|
11
11
|
|
|
12
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
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
|
|
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.
|
|
15
15
|
|
|
16
16
|
## API
|
|
17
17
|
|
|
@@ -20,6 +20,8 @@ A special case is the marker: an annotation of zero width marking a position in
|
|
|
20
20
|
- [`TextSegment<T>`](./src/Model.ts) <br /> Output list of segments with character offsets and the annotations that apply.
|
|
21
21
|
- [`groupSegments<T>(segments, predicate): Group<T>[]`](./src/groupSegments.ts) <br /> Group segments into higher-level units (e.g., words, sentences, entities) by collecting all segments that share a matching annotation.
|
|
22
22
|
- [`Group<T>`](./src/groupSegments.ts) <br /> Output group of segments matching the same predicate result.
|
|
23
|
+
- [`mapAnnotationSegmentRanges<T>(segments): Map<T, SegmentRange>`](./src/mapAnnotationSegmentRanges.ts) <br /> For each annotation, collect the first and last segment index it appears in, keyed by object reference.
|
|
24
|
+
- [`SegmentRange`](./src/mapAnnotationSegmentRanges.ts) <br /> Start and end segment index of an annotation (end excluding).
|
|
23
25
|
|
|
24
26
|
## Example
|
|
25
27
|
|
package/dist/Model.d.ts
CHANGED
|
@@ -1,18 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
* Input list of objects linking annotations to the text using character offsets.
|
|
3
|
-
*/
|
|
4
|
-
export type AnnotationSegment<T = unknown> = {
|
|
1
|
+
export type Offsets = {
|
|
5
2
|
begin: number;
|
|
6
3
|
end: number;
|
|
7
|
-
body: T;
|
|
8
4
|
};
|
|
9
5
|
/**
|
|
10
|
-
* Output
|
|
6
|
+
* Output: segment of text and the annotations that apply.
|
|
11
7
|
*/
|
|
12
|
-
export type TextSegment<T =
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
end: number;
|
|
8
|
+
export type TextSegment<T> = Offsets & {
|
|
9
|
+
index: SegmentIndex;
|
|
10
|
+
body: string;
|
|
16
11
|
annotations: T[];
|
|
17
12
|
};
|
|
18
|
-
export type
|
|
13
|
+
export type SegmentIndex = number;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export { segment } from './segment.ts';
|
|
2
2
|
export type { AnnotationSegmentsByChar } from './segment.ts';
|
|
3
|
+
export { mapAnnotationSegmentRanges } from './mapAnnotationSegmentRanges.ts';
|
|
4
|
+
export type { SegmentRange } from './mapAnnotationSegmentRanges.ts';
|
|
3
5
|
export { groupSegments } from './groupSegments.ts';
|
|
4
6
|
export type { Group } from './groupSegments.ts';
|
|
5
|
-
export type {
|
|
7
|
+
export type { Offsets, TextSegment, SegmentIndex, } from './Model.ts';
|
package/dist/index.js
CHANGED
|
@@ -1,21 +1,17 @@
|
|
|
1
1
|
function segment(text, annotations) {
|
|
2
2
|
const segments = [];
|
|
3
|
-
let segmentCounter =
|
|
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
7
|
};
|
|
8
8
|
getOrCreateOffset(0), getOrCreateOffset(text.length);
|
|
9
9
|
for (const annotation of annotations) {
|
|
10
|
-
|
|
10
|
+
const isMarker = annotation.begin === annotation.end;
|
|
11
|
+
if (isMarker && (annotation.end < 0 || annotation.begin > text.length) || !isMarker && (annotation.end <= 0 || annotation.begin >= text.length))
|
|
11
12
|
continue;
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
needsClamping && (clamped = {
|
|
15
|
-
...annotation,
|
|
16
|
-
begin: Math.max(0, annotation.begin),
|
|
17
|
-
end: Math.min(text.length, annotation.end)
|
|
18
|
-
}), getOrCreateOffset(clamped.begin).starting.push(clamped), getOrCreateOffset(clamped.end).ending.push(clamped);
|
|
13
|
+
const begin = Math.max(0, annotation.begin), end = Math.min(text.length, annotation.end);
|
|
14
|
+
getOrCreateOffset(begin).starting.push(annotation), getOrCreateOffset(end).ending.push(annotation);
|
|
19
15
|
}
|
|
20
16
|
const sortedOffsets = [...offsetMap.values()].sort(
|
|
21
17
|
(a, b) => a.charIndex - b.charIndex
|
|
@@ -23,11 +19,12 @@ function segment(text, annotations) {
|
|
|
23
19
|
let lastOffset = 0;
|
|
24
20
|
for (const offset of sortedOffsets) {
|
|
25
21
|
if (offset.charIndex > lastOffset) {
|
|
26
|
-
const
|
|
22
|
+
const index = ++segmentCounter;
|
|
27
23
|
segments.push({
|
|
28
|
-
|
|
24
|
+
index,
|
|
29
25
|
begin: lastOffset,
|
|
30
26
|
end: offset.charIndex,
|
|
27
|
+
body: text.slice(lastOffset, offset.charIndex),
|
|
31
28
|
annotations: [...activeAnnotations]
|
|
32
29
|
});
|
|
33
30
|
}
|
|
@@ -35,22 +32,32 @@ function segment(text, annotations) {
|
|
|
35
32
|
let hasMarkers = !1;
|
|
36
33
|
const markerSegmentAnnotations = [];
|
|
37
34
|
for (const a of offset.starting)
|
|
38
|
-
offset.ending.includes(a) ? (hasMarkers = !0, markerSegmentAnnotations.push(a
|
|
39
|
-
for (const
|
|
40
|
-
activeAnnotations.delete(
|
|
35
|
+
offset.ending.includes(a) ? (hasMarkers = !0, markerSegmentAnnotations.push(a)) : activeAnnotations.add(a);
|
|
36
|
+
for (const annotation of offset.ending)
|
|
37
|
+
activeAnnotations.delete(annotation);
|
|
41
38
|
if (hasMarkers) {
|
|
42
39
|
markerSegmentAnnotations.push(...activeAnnotations);
|
|
43
|
-
const
|
|
40
|
+
const index = ++segmentCounter;
|
|
44
41
|
segments.push({
|
|
45
|
-
|
|
42
|
+
index,
|
|
46
43
|
begin: offset.charIndex,
|
|
47
44
|
end: offset.charIndex,
|
|
45
|
+
body: "",
|
|
48
46
|
annotations: markerSegmentAnnotations
|
|
49
47
|
});
|
|
50
48
|
}
|
|
51
49
|
}
|
|
52
50
|
return segments;
|
|
53
51
|
}
|
|
52
|
+
function mapAnnotationSegmentRanges(segments) {
|
|
53
|
+
const ranges = /* @__PURE__ */ new Map();
|
|
54
|
+
for (let i = 0; i < segments.length; i++)
|
|
55
|
+
for (const annotation of segments[i].annotations) {
|
|
56
|
+
const existing = ranges.get(annotation);
|
|
57
|
+
existing ? existing.endSegment = i + 1 : ranges.set(annotation, { startSegment: i, endSegment: i + 1 });
|
|
58
|
+
}
|
|
59
|
+
return ranges;
|
|
60
|
+
}
|
|
54
61
|
function groupSegments(segments, predicate) {
|
|
55
62
|
const groups = /* @__PURE__ */ new Map();
|
|
56
63
|
for (const segment2 of segments) {
|
|
@@ -67,5 +74,6 @@ function groupSegments(segments, predicate) {
|
|
|
67
74
|
}
|
|
68
75
|
export {
|
|
69
76
|
groupSegments,
|
|
77
|
+
mapAnnotationSegmentRanges,
|
|
70
78
|
segment
|
|
71
79
|
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { TextSegment } from './Model';
|
|
2
|
+
export type SegmentRange = {
|
|
3
|
+
startSegment: number;
|
|
4
|
+
/**
|
|
5
|
+
* Excluding last segment
|
|
6
|
+
*/
|
|
7
|
+
endSegment: number;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* For each annotation, collect the first and last segment index it appears in, keyed by object reference.
|
|
11
|
+
*/
|
|
12
|
+
export declare function mapAnnotationSegmentRanges<T>(segments: TextSegment<T>[]): Map<T, SegmentRange>;
|
package/dist/segment.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Offsets, TextSegment } from './Model';
|
|
2
2
|
/**
|
|
3
3
|
* Split a text into {@link TextSegment}s with character offsets and a list of applying annotations.
|
|
4
4
|
*/
|
|
5
|
-
export declare function segment<T>(text: string, annotations:
|
|
6
|
-
export type AnnotationSegmentsByChar<T =
|
|
5
|
+
export declare function segment<T extends Offsets>(text: string, annotations: T[]): TextSegment<T>[];
|
|
6
|
+
export type AnnotationSegmentsByChar<T extends Offsets = Offsets> = {
|
|
7
7
|
charIndex: number;
|
|
8
|
-
starting:
|
|
9
|
-
ending:
|
|
8
|
+
starting: T[];
|
|
9
|
+
ending: T[];
|
|
10
10
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@knaw-huc/text-annotation-segmenter",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/knaw-huc/text-annotation-segmenter.git"
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
],
|
|
20
20
|
"scripts": {
|
|
21
21
|
"dev:build": "vite build --watch",
|
|
22
|
-
"build": "vite build",
|
|
22
|
+
"build": "npm run test && vite build",
|
|
23
23
|
"test": "vitest run",
|
|
24
24
|
"bench": "vitest bench",
|
|
25
25
|
"prepublishOnly": "npm run build"
|
package/README.html
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
<h1 id="knaw-huctext-annotation-segmenter"><span class="citation" data-cites="knaw-huc/text-annotation-segmenter">@knaw-huc/text-annotation-segmenter</span></h1>
|
|
2
|
-
<p>Utility functions to help render overlapping annotations in a text.</p>
|
|
3
|
-
<p>Annotations on a text have a non-hierarchical nature, i.e., they can overlap:</p>
|
|
4
|
-
<pre class="text"><code>Aa<bc>bb<cd>cc</bc>dd<cd>ee.</code></pre>
|
|
5
|
-
<p>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?</p>
|
|
6
|
-
<p>The <code>segment</code> 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.</p>
|
|
7
|
-
<p>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, end at, or span across that position.</p>
|
|
8
|
-
<h2 id="api">API</h2>
|
|
9
|
-
<ul>
|
|
10
|
-
<li><a href="./src/segment.ts"><code>segment<T>(text, annotations): TextSegment<T>[]</code></a> <br /> Split a text into TextSegments with char offsets to the text and a list of applying annotations.</li>
|
|
11
|
-
<li><a href="./src/Model.ts"><code>AnnotationSegment<T></code></a> <br /> Input list of objects linking annotations to the text using character offsets.</li>
|
|
12
|
-
<li><a href="./src/Model.ts"><code>TextSegment<T></code></a> <br /> Output list of segments with character offsets and the annotations that apply.</li>
|
|
13
|
-
<li><a href="./src/groupSegments.ts"><code>groupSegments<T>(segments, predicate): Group<T>[]</code></a> <br /> Group segments into higher-level units (e.g., words, sentences, entities) by collecting all segments that share a matching annotation.</li>
|
|
14
|
-
<li><a href="./src/groupSegments.ts"><code>Group<T></code></a> <br /> Output group of segments matching the same predicate result.</li>
|
|
15
|
-
</ul>
|
|
16
|
-
<h2 id="example">Example</h2>
|
|
17
|
-
<p>Given the text <code>"abc"</code> with two overlapping annotations:</p>
|
|
18
|
-
<div class="sourceCode" id="cb2"><pre class="sourceCode txt"><code class="sourceCode default"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true"></a> text: abc</span>
|
|
19
|
-
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true"></a>annotation ab: __</span>
|
|
20
|
-
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true"></a>annotation bc: __</span></code></pre></div>
|
|
21
|
-
<div class="sourceCode" id="cb3"><pre class="sourceCode ts"><code class="sourceCode typescript"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true"></a><span class="im">import</span> { segment } from <span class="st">"text-annotation-segmenter"</span><span class="op">;</span></span>
|
|
22
|
-
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true"></a></span>
|
|
23
|
-
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true"></a>const text <span class="op">=</span> <span class="st">'abc'</span><span class="op">;</span></span>
|
|
24
|
-
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true"></a>const ab <span class="op">=</span> {id<span class="op">:</span> <span class="st">'ab'</span>}<span class="op">;</span></span>
|
|
25
|
-
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true"></a>const bc <span class="op">=</span> {id<span class="op">:</span> <span class="st">'bc'</span>}<span class="op">;</span></span>
|
|
26
|
-
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true"></a></span>
|
|
27
|
-
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true"></a>const segments <span class="op">=</span> <span class="fu">segment</span>(text<span class="op">,</span> [</span>
|
|
28
|
-
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true"></a> {begin<span class="op">:</span> <span class="dv">0</span><span class="op">,</span> end<span class="op">:</span> <span class="dv">2</span><span class="op">,</span> body<span class="op">:</span> ab}<span class="op">,</span></span>
|
|
29
|
-
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true"></a> {begin<span class="op">:</span> <span class="dv">1</span><span class="op">,</span> end<span class="op">:</span> <span class="dv">3</span><span class="op">,</span> body<span class="op">:</span> bc}<span class="op">,</span></span>
|
|
30
|
-
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true"></a>])<span class="op">;</span></span>
|
|
31
|
-
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true"></a></span>
|
|
32
|
-
<span id="cb3-12"><a href="#cb3-12" aria-hidden="true"></a><span class="fu">expect</span>(segments)<span class="op">.</span><span class="fu">toEqual</span>([</span>
|
|
33
|
-
<span id="cb3-13"><a href="#cb3-13" aria-hidden="true"></a> {id<span class="op">:</span> <span class="st">'0'</span><span class="op">,</span> begin<span class="op">:</span> <span class="dv">0</span><span class="op">,</span> end<span class="op">:</span> <span class="dv">1</span><span class="op">,</span> annotations<span class="op">:</span> [ab]}<span class="op">,</span></span>
|
|
34
|
-
<span id="cb3-14"><a href="#cb3-14" aria-hidden="true"></a> {id<span class="op">:</span> <span class="st">'1'</span><span class="op">,</span> begin<span class="op">:</span> <span class="dv">1</span><span class="op">,</span> end<span class="op">:</span> <span class="dv">2</span><span class="op">,</span> annotations<span class="op">:</span> [ab<span class="op">,</span> bc]}<span class="op">,</span></span>
|
|
35
|
-
<span id="cb3-15"><a href="#cb3-15" aria-hidden="true"></a> {id<span class="op">:</span> <span class="st">'2'</span><span class="op">,</span> begin<span class="op">:</span> <span class="dv">2</span><span class="op">,</span> end<span class="op">:</span> <span class="dv">3</span><span class="op">,</span> annotations<span class="op">:</span> [bc]}<span class="op">,</span></span>
|
|
36
|
-
<span id="cb3-16"><a href="#cb3-16" aria-hidden="true"></a>])<span class="op">;</span></span></code></pre></div>
|
|
37
|
-
<p>More examples:</p>
|
|
38
|
-
<ul>
|
|
39
|
-
<li>For edge cases, see: <a href="./src/segment.spec.ts">segment.spec.ts</a>.</li>
|
|
40
|
-
<li>For benchmarks, see <a href="./src/segment.bench.ts">segment.bench.ts</a>.</li>
|
|
41
|
-
</ul>
|