@knaw-huc/text-annotation-segmenter 0.3.0 → 0.4.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 CHANGED
@@ -35,13 +35,14 @@ annotation bc: __
35
35
  ```
36
36
 
37
37
  ```ts
38
- import { segment } from "@knaw-huc/text-annotation-segmenter";
38
+ import {segment} from "@knaw-huc/text-annotation-segmenter";
39
39
 
40
40
  const text = 'abc';
41
41
  const ab = {id: 'ab', begin: 0, end: 2};
42
42
  const bc = {id: 'bc', begin: 1, end: 3};
43
43
 
44
- const segments = segment(text, [ab, bc], (a) => a);
44
+ const getOffsets = annotation => annotation;
45
+ const segments = segment(text, [ab, bc], getOffsets);
45
46
 
46
47
  expect(segments).toEqual([
47
48
  {index: 0, begin: 0, end: 1, body: 'a', annotations: [ab]},
@@ -69,7 +70,8 @@ const text = 'aabb';
69
70
  const section = {id: 'section', type: 'section', begin: 0, end: 4};
70
71
  const paragraph = {id: 'paragraph', type: 'paragraph', begin: 0, end: 2};
71
72
 
72
- const segments = segment(text, [section, paragraph], (a) => a);
73
+ const getOffsets = annotation => annotation;
74
+ const segments = segment(text, [section, paragraph], getOffsets);
73
75
 
74
76
  const isGroup = a => a.type === 'section' || a.type === 'paragraph';
75
77
  const getId = a => a.id;
@@ -0,0 +1,2 @@
1
+ import { MarkerPosition } from './Model.ts';
2
+ export type GetMarkerPosition<T> = (a: T) => MarkerPosition;
package/dist/Model.d.ts CHANGED
@@ -14,3 +14,4 @@ export type TextSegment<T> = Offsets & {
14
14
  annotations: T[];
15
15
  };
16
16
  export type SegmentIndex = number;
17
+ export type MarkerPosition = 'prefix' | 'postfix';
package/dist/index.d.ts CHANGED
@@ -4,5 +4,6 @@ export { findSegmentOffsets } from './findSegmentOffsets.ts';
4
4
  export type { SegmentOffsets } from './findSegmentOffsets.ts';
5
5
  export { groupSegments } from './groupSegments.ts';
6
6
  export type { Group, SegmentGroup } from './groupSegments.ts';
7
- export type { Offsets, TextSegment, SegmentIndex, } from './Model.ts';
7
+ export type { Offsets, TextSegment, SegmentIndex, MarkerPosition, } from './Model.ts';
8
8
  export type { GetOffsets } from './GetOffsets.ts';
9
+ export type { GetMarkerPosition } from './GetMarkerPosition.ts';
package/dist/index.js CHANGED
@@ -1,10 +1,16 @@
1
- function segment(text, annotations, getOffsets) {
1
+ function segment(text, annotations, getOffsets, getMarkerPosition = () => "postfix") {
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
- };
7
+ }, createMarkerSegment = (markers, charIndex) => ({
8
+ index: ++segmentCounter,
9
+ begin: charIndex,
10
+ end: charIndex,
11
+ body: "",
12
+ annotations: [...markers, ...activeAnnotations]
13
+ });
8
14
  getOrCreateOffset(0), getOrCreateOffset(text.length);
9
15
  for (const annotation of annotations) {
10
16
  const offsets = getOffsets(annotation), isMarker = offsets.begin === offsets.end;
@@ -29,23 +35,15 @@ function segment(text, annotations, getOffsets) {
29
35
  });
30
36
  }
31
37
  lastOffset = offset.charIndex;
32
- let hasMarkers = !1;
33
- const markerSegmentAnnotations = [];
34
- for (const a of offset.starting)
35
- offset.ending.includes(a) ? (hasMarkers = !0, markerSegmentAnnotations.push(a)) : activeAnnotations.add(a);
38
+ const postfixMarkers = [], prefixMarkers = [];
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));
42
+ for (const annotation of offset.starting)
43
+ offset.ending.includes(annotation) || activeAnnotations.add(annotation);
36
44
  for (const annotation of offset.ending)
37
45
  activeAnnotations.delete(annotation);
38
- if (hasMarkers) {
39
- markerSegmentAnnotations.push(...activeAnnotations);
40
- const index = ++segmentCounter;
41
- segments.push({
42
- index,
43
- begin: offset.charIndex,
44
- end: offset.charIndex,
45
- body: "",
46
- annotations: markerSegmentAnnotations
47
- });
48
- }
46
+ prefixMarkers.length && segments.push(createMarkerSegment(prefixMarkers, offset.charIndex));
49
47
  }
50
48
  return segments;
51
49
  }
package/dist/segment.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import { TextSegment } from './Model';
2
2
  import { GetOffsets } from './GetOffsets.ts';
3
+ import { GetMarkerPosition } from './GetMarkerPosition.ts';
3
4
  /**
4
5
  * Split a text into {@link TextSegment}s with character offsets and a list of applying annotations.
5
6
  */
6
- export declare function segment<T>(text: string, annotations: T[], getOffsets: GetOffsets<T>): TextSegment<T>[];
7
+ export declare function segment<T>(text: string, annotations: T[], getOffsets: GetOffsets<T>, getMarkerPosition?: GetMarkerPosition<T>): TextSegment<T>[];
7
8
  export type AnnotationSegmentsByChar<T> = {
8
9
  charIndex: number;
9
10
  starting: T[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knaw-huc/text-annotation-segmenter",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/knaw-huc/text-annotation-segmenter.git"