@anuma/sdk 10.6.1 → 10.6.2
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.
|
@@ -42,20 +42,24 @@ var DEFAULT_TAG_MAP = {
|
|
|
42
42
|
MISC: void 0
|
|
43
43
|
};
|
|
44
44
|
function placeTokens(tokens, text) {
|
|
45
|
-
|
|
45
|
+
return tokens.map((token) => {
|
|
46
|
+
const { start = -1, end = -1 } = token;
|
|
47
|
+
return Number.isInteger(start) && Number.isInteger(end) && start >= 0 && end > start && end <= text.length ? { ...token, start, end } : { ...token, start: -1, end: -1 };
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
function tokenOffsets(text, tokenizer) {
|
|
51
|
+
const { input_ids } = tokenizer(text, { return_tensor: false, truncation: true });
|
|
46
52
|
let cursor = 0;
|
|
47
|
-
|
|
48
|
-
const
|
|
53
|
+
return input_ids.map((id) => {
|
|
54
|
+
const word = tokenizer.decode([id], { skip_special_tokens: true });
|
|
55
|
+
const surface = word.startsWith("##") ? word.slice(2) : word;
|
|
56
|
+
if (!surface) return { start: -1, end: -1 };
|
|
49
57
|
const start = text.indexOf(surface, cursor);
|
|
50
|
-
if (start < 0) {
|
|
51
|
-
placed.push({ ...t, start: -1, end: -1 });
|
|
52
|
-
continue;
|
|
53
|
-
}
|
|
58
|
+
if (start < 0) return { start: -1, end: -1 };
|
|
54
59
|
const end = start + surface.length;
|
|
55
|
-
placed.push({ ...t, start, end });
|
|
56
60
|
cursor = end;
|
|
57
|
-
|
|
58
|
-
|
|
61
|
+
return { start, end };
|
|
62
|
+
});
|
|
59
63
|
}
|
|
60
64
|
function aggregateTokens(tokens, text, tagMap, minScore) {
|
|
61
65
|
const placed = placeTokens(tokens, text);
|
|
@@ -115,7 +119,14 @@ function createTransformersNerDetector(options = {}) {
|
|
|
115
119
|
if (!text) return [];
|
|
116
120
|
const classify = await load();
|
|
117
121
|
const tokens = await classify(text);
|
|
118
|
-
|
|
122
|
+
if (!tokens.length) return [];
|
|
123
|
+
const offsets = tokenOffsets(text, classify.tokenizer);
|
|
124
|
+
return aggregateTokens(
|
|
125
|
+
tokens.map((token) => ({ ...token, ...offsets[token.index] })),
|
|
126
|
+
text,
|
|
127
|
+
tagMap,
|
|
128
|
+
minScore
|
|
129
|
+
);
|
|
119
130
|
}
|
|
120
131
|
};
|
|
121
132
|
}
|
|
@@ -54,13 +54,10 @@ interface RawToken {
|
|
|
54
54
|
score: number;
|
|
55
55
|
index: number;
|
|
56
56
|
word: string;
|
|
57
|
+
start?: number;
|
|
58
|
+
end?: number;
|
|
57
59
|
}
|
|
58
|
-
/**
|
|
59
|
-
* Aggregate B-/I- subword tokens into whole-entity spans. A new entity starts
|
|
60
|
-
* on a `B-` tag or a tag change; contiguous `I-` tokens (and WordPiece "##"
|
|
61
|
-
* continuations) of the same base tag extend it. (The SDK additionally snaps
|
|
62
|
-
* spans to word boundaries, so dropped trailing subwords are recovered there.)
|
|
63
|
-
*/
|
|
60
|
+
/** Aggregate classified tokens with character offsets into whole-entity spans. */
|
|
64
61
|
declare function aggregateTokens(tokens: RawToken[], text: string, tagMap: Record<string, string | undefined>, minScore: number): PiiSpan[];
|
|
65
62
|
/**
|
|
66
63
|
* Create a Transformers.js-backed {@link NerDetector}. The returned detector
|
|
@@ -54,13 +54,10 @@ interface RawToken {
|
|
|
54
54
|
score: number;
|
|
55
55
|
index: number;
|
|
56
56
|
word: string;
|
|
57
|
+
start?: number;
|
|
58
|
+
end?: number;
|
|
57
59
|
}
|
|
58
|
-
/**
|
|
59
|
-
* Aggregate B-/I- subword tokens into whole-entity spans. A new entity starts
|
|
60
|
-
* on a `B-` tag or a tag change; contiguous `I-` tokens (and WordPiece "##"
|
|
61
|
-
* continuations) of the same base tag extend it. (The SDK additionally snaps
|
|
62
|
-
* spans to word boundaries, so dropped trailing subwords are recovered there.)
|
|
63
|
-
*/
|
|
60
|
+
/** Aggregate classified tokens with character offsets into whole-entity spans. */
|
|
64
61
|
declare function aggregateTokens(tokens: RawToken[], text: string, tagMap: Record<string, string | undefined>, minScore: number): PiiSpan[];
|
|
65
62
|
/**
|
|
66
63
|
* Create a Transformers.js-backed {@link NerDetector}. The returned detector
|
|
@@ -7,20 +7,24 @@ var DEFAULT_TAG_MAP = {
|
|
|
7
7
|
MISC: void 0
|
|
8
8
|
};
|
|
9
9
|
function placeTokens(tokens, text) {
|
|
10
|
-
|
|
10
|
+
return tokens.map((token) => {
|
|
11
|
+
const { start = -1, end = -1 } = token;
|
|
12
|
+
return Number.isInteger(start) && Number.isInteger(end) && start >= 0 && end > start && end <= text.length ? { ...token, start, end } : { ...token, start: -1, end: -1 };
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
function tokenOffsets(text, tokenizer) {
|
|
16
|
+
const { input_ids } = tokenizer(text, { return_tensor: false, truncation: true });
|
|
11
17
|
let cursor = 0;
|
|
12
|
-
|
|
13
|
-
const
|
|
18
|
+
return input_ids.map((id) => {
|
|
19
|
+
const word = tokenizer.decode([id], { skip_special_tokens: true });
|
|
20
|
+
const surface = word.startsWith("##") ? word.slice(2) : word;
|
|
21
|
+
if (!surface) return { start: -1, end: -1 };
|
|
14
22
|
const start = text.indexOf(surface, cursor);
|
|
15
|
-
if (start < 0) {
|
|
16
|
-
placed.push({ ...t, start: -1, end: -1 });
|
|
17
|
-
continue;
|
|
18
|
-
}
|
|
23
|
+
if (start < 0) return { start: -1, end: -1 };
|
|
19
24
|
const end = start + surface.length;
|
|
20
|
-
placed.push({ ...t, start, end });
|
|
21
25
|
cursor = end;
|
|
22
|
-
|
|
23
|
-
|
|
26
|
+
return { start, end };
|
|
27
|
+
});
|
|
24
28
|
}
|
|
25
29
|
function aggregateTokens(tokens, text, tagMap, minScore) {
|
|
26
30
|
const placed = placeTokens(tokens, text);
|
|
@@ -80,7 +84,14 @@ function createTransformersNerDetector(options = {}) {
|
|
|
80
84
|
if (!text) return [];
|
|
81
85
|
const classify = await load();
|
|
82
86
|
const tokens = await classify(text);
|
|
83
|
-
|
|
87
|
+
if (!tokens.length) return [];
|
|
88
|
+
const offsets = tokenOffsets(text, classify.tokenizer);
|
|
89
|
+
return aggregateTokens(
|
|
90
|
+
tokens.map((token) => ({ ...token, ...offsets[token.index] })),
|
|
91
|
+
text,
|
|
92
|
+
tagMap,
|
|
93
|
+
minScore
|
|
94
|
+
);
|
|
84
95
|
}
|
|
85
96
|
};
|
|
86
97
|
}
|