@remit/mailbox-service 0.0.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/README.md +147 -0
- package/package.json +65 -0
- package/src/account-credentials.ts +128 -0
- package/src/adversarial-mime.e2e.test.ts +311 -0
- package/src/attribute-mapper.test.ts +78 -0
- package/src/attribute-mapper.ts +147 -0
- package/src/body-part-mapper.property.test.ts +354 -0
- package/src/body-part-mapper.ts +360 -0
- package/src/body-sync-queue.test.ts +124 -0
- package/src/body-sync-queue.ts +131 -0
- package/src/body-sync-terminal.test.ts +265 -0
- package/src/body-sync-terminal.ts +172 -0
- package/src/body-sync.ts +1202 -0
- package/src/connection-factory.ts +102 -0
- package/src/connection-test.ts +208 -0
- package/src/credentials.test.ts +181 -0
- package/src/filters/match.test.ts +181 -0
- package/src/filters/match.ts +143 -0
- package/src/filters/pipeline.ts +243 -0
- package/src/flag-push-terminal.test.ts +156 -0
- package/src/flag-push-terminal.ts +106 -0
- package/src/flag-push.test.ts +408 -0
- package/src/flag-push.ts +198 -0
- package/src/flag-queue.ts +500 -0
- package/src/heuristics/classifyByHeaders.test.ts +578 -0
- package/src/heuristics/classifyByHeaders.ts +369 -0
- package/src/heuristics/classifyPlacement.ts +96 -0
- package/src/heuristics/socialDomains.ts +23 -0
- package/src/heuristics/transactionalDomains.ts +26 -0
- package/src/imapflow-connect-retry.test.ts +108 -0
- package/src/imapflow-connection.e2e.test.ts +355 -0
- package/src/imapflow-connection.integ.test.ts +327 -0
- package/src/imapflow-connection.test.ts +161 -0
- package/src/imapflow-connection.ts +1374 -0
- package/src/index.ts +214 -0
- package/src/mailbox-cursor-rebuild.test.ts +171 -0
- package/src/mailbox-cursor-rebuild.ts +146 -0
- package/src/mailbox-cursor.test.ts +249 -0
- package/src/mailbox-cursor.ts +177 -0
- package/src/mailbox-management.test.ts +103 -0
- package/src/mailbox-management.ts +235 -0
- package/src/mailbox-queue.ts +258 -0
- package/src/mailbox-sync.test.ts +153 -0
- package/src/mailbox-sync.ts +590 -0
- package/src/message-move.ts +825 -0
- package/src/message-parser.ts +26 -0
- package/src/message-sync.ts +1021 -0
- package/src/mime-walker.test.ts +311 -0
- package/src/mime-walker.ts +305 -0
- package/src/outbox-queue.ts +294 -0
- package/src/pass-through-unit-of-work.ts +17 -0
- package/src/placement-move-terminal.test.ts +179 -0
- package/src/placement-move-terminal.ts +104 -0
- package/src/placement-move.ts +224 -0
- package/src/snippet.test.ts +213 -0
- package/src/snippet.ts +152 -0
- package/src/stale-message-reconcile.test.ts +82 -0
- package/src/stale-message-reconcile.ts +55 -0
- package/src/test-helpers/isolated-mailbox.ts +44 -0
- package/src/test-helpers/mailfuzz-connection.ts +27 -0
- package/src/test-helpers/mime-tree-generator.ts +461 -0
- package/src/text/normalizer.test.ts +223 -0
- package/src/text/normalizer.ts +140 -0
- package/src/types/mailparser-augment.d.ts +15 -0
- package/src/types/natural-porter-stemmer.d.ts +50 -0
- package/src/types/stopword.d.ts +69 -0
- package/src/types.ts +349 -0
- package/test/fixtures/mime/01-flat-text-plain.bodyparts.json +8 -0
- package/test/fixtures/mime/01-flat-text-plain.eml +10 -0
- package/test/fixtures/mime/01-flat-text-plain.expected.json +10 -0
- package/test/fixtures/mime/02-flat-text-html.bodyparts.json +8 -0
- package/test/fixtures/mime/02-flat-text-html.eml +10 -0
- package/test/fixtures/mime/02-flat-text-html.expected.json +10 -0
- package/test/fixtures/mime/03-alternative-text-html.bodyparts.json +20 -0
- package/test/fixtures/mime/03-alternative-text-html.eml +19 -0
- package/test/fixtures/mime/03-alternative-text-html.expected.json +16 -0
- package/test/fixtures/mime/04-mixed-html-pdf-attachment.bodyparts.json +22 -0
- package/test/fixtures/mime/04-mixed-html-pdf-attachment.eml +20 -0
- package/test/fixtures/mime/04-mixed-html-pdf-attachment.expected.json +16 -0
- package/test/fixtures/mime/05-mixed-html-octet-stream-pdf.bodyparts.json +22 -0
- package/test/fixtures/mime/05-mixed-html-octet-stream-pdf.eml +21 -0
- package/test/fixtures/mime/05-mixed-html-octet-stream-pdf.expected.json +16 -0
- package/test/fixtures/mime/06-related-html-cid-image.bodyparts.json +23 -0
- package/test/fixtures/mime/06-related-html-cid-image.eml +21 -0
- package/test/fixtures/mime/06-related-html-cid-image.expected.json +16 -0
- package/test/fixtures/mime/07-mixed-alternative-related-attachment.bodyparts.json +55 -0
- package/test/fixtures/mime/07-mixed-alternative-related-attachment.eml +47 -0
- package/test/fixtures/mime/07-mixed-alternative-related-attachment.expected.json +34 -0
- package/test/fixtures/mime/08-two-pdfs-same-filename.bodyparts.json +30 -0
- package/test/fixtures/mime/08-two-pdfs-same-filename.eml +26 -0
- package/test/fixtures/mime/08-two-pdfs-same-filename.expected.json +22 -0
- package/test/fixtures/mime/09-attachment-no-filename.bodyparts.json +21 -0
- package/test/fixtures/mime/09-attachment-no-filename.eml +20 -0
- package/test/fixtures/mime/09-attachment-no-filename.expected.json +16 -0
- package/test/fixtures/mime/10-calendar-invite.bodyparts.json +22 -0
- package/test/fixtures/mime/10-calendar-invite.eml +33 -0
- package/test/fixtures/mime/10-calendar-invite.expected.json +16 -0
- package/test/fixtures/mime/11-quoted-printable-text.bodyparts.json +8 -0
- package/test/fixtures/mime/11-quoted-printable-text.eml +10 -0
- package/test/fixtures/mime/11-quoted-printable-text.expected.json +10 -0
- package/test/fixtures/mime/12-base64-binary.bodyparts.json +22 -0
- package/test/fixtures/mime/12-base64-binary.eml +20 -0
- package/test/fixtures/mime/12-base64-binary.expected.json +16 -0
- package/test/fixtures/mime/13-message-rfc822-forward.bodyparts.json +22 -0
- package/test/fixtures/mime/13-message-rfc822-forward.eml +28 -0
- package/test/fixtures/mime/13-message-rfc822-forward.expected.json +16 -0
- package/test/fixtures/mime/14-deeply-nested-5-levels.bodyparts.json +38 -0
- package/test/fixtures/mime/14-deeply-nested-5-levels.eml +34 -0
- package/test/fixtures/mime/14-deeply-nested-5-levels.expected.json +10 -0
- package/test/fixtures/mime/15-empty-text-part.bodyparts.json +21 -0
- package/test/fixtures/mime/15-empty-text-part.eml +18 -0
- package/test/fixtures/mime/15-empty-text-part.expected.json +16 -0
- package/test/fixtures/mime/16-empty-non-text-part.bodyparts.json +30 -0
- package/test/fixtures/mime/16-empty-non-text-part.eml +24 -0
- package/test/fixtures/mime/16-empty-non-text-part.expected.json +22 -0
- package/test/fixtures/mime-adversarial-e2e/01-octet-stream-pdf.eml +21 -0
- package/test/fixtures/mime-adversarial-e2e/02-nested-related-cid-image.eml +31 -0
- package/test/fixtures/mime-adversarial-e2e/03-calendar-invite.eml +33 -0
- package/test/fixtures/mime-adversarial-e2e/04-forwarded-rfc822-with-attachments.eml +42 -0
- package/test/fixtures/mime-adversarial-e2e/05-tnef-winmail.eml +25 -0
- package/test/fixtures/mime-adversarial-e2e/06-8bit-non-ascii.eml +15 -0
- package/test/fixtures/mime-adversarial-e2e/07-quoted-printable-soft-breaks.eml +17 -0
- package/test/fixtures/mime-adversarial-e2e/08-alternative-empty-text.eml +18 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import { createTextNormalizer } from "./normalizer.js";
|
|
4
|
+
|
|
5
|
+
describe("TextNormalizer", () => {
|
|
6
|
+
const normalizer = createTextNormalizer();
|
|
7
|
+
|
|
8
|
+
describe("detectLanguage", () => {
|
|
9
|
+
it("detects English text", () => {
|
|
10
|
+
const text =
|
|
11
|
+
"The quick brown fox jumps over the lazy dog. This is a sample English text.";
|
|
12
|
+
assert.strictEqual(normalizer.detectLanguage(text), "en");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("detects German text", () => {
|
|
16
|
+
const text =
|
|
17
|
+
"Der schnelle braune Fuchs springt über den faulen Hund. Dies ist ein deutscher Text.";
|
|
18
|
+
assert.strictEqual(normalizer.detectLanguage(text), "de");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("detects French text", () => {
|
|
22
|
+
const text =
|
|
23
|
+
"Le renard brun rapide saute par-dessus le chien paresseux. Ceci est un texte français.";
|
|
24
|
+
assert.strictEqual(normalizer.detectLanguage(text), "fr");
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("detects Spanish text", () => {
|
|
28
|
+
const text =
|
|
29
|
+
"El rápido zorro marrón salta sobre el perro perezoso. Este es un texto en español.";
|
|
30
|
+
assert.strictEqual(normalizer.detectLanguage(text), "es");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("detects Italian text", () => {
|
|
34
|
+
const text =
|
|
35
|
+
"La volpe marrone veloce salta sopra il cane pigro. Questo è un testo italiano.";
|
|
36
|
+
assert.strictEqual(normalizer.detectLanguage(text), "it");
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("detects Portuguese text", () => {
|
|
40
|
+
const text =
|
|
41
|
+
"A rápida raposa marrom salta sobre o cão preguiçoso. Este é um texto em português.";
|
|
42
|
+
assert.strictEqual(normalizer.detectLanguage(text), "pt");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("detects Dutch text", () => {
|
|
46
|
+
const text =
|
|
47
|
+
"De snelle bruine vos springt over de luie hond. Dit is een Nederlandse tekst.";
|
|
48
|
+
assert.strictEqual(normalizer.detectLanguage(text), "nl");
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("defaults to English for short text", () => {
|
|
52
|
+
const text = "Hello";
|
|
53
|
+
assert.strictEqual(normalizer.detectLanguage(text), "en");
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("defaults to English for empty text", () => {
|
|
57
|
+
assert.strictEqual(normalizer.detectLanguage(""), "en");
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe("tokenize", () => {
|
|
62
|
+
it("tokenizes text into words", () => {
|
|
63
|
+
const text = "Hello, World! How are you?";
|
|
64
|
+
assert.deepStrictEqual(normalizer.tokenize(text), [
|
|
65
|
+
"hello",
|
|
66
|
+
"world",
|
|
67
|
+
"how",
|
|
68
|
+
"are",
|
|
69
|
+
"you",
|
|
70
|
+
]);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("handles Unicode characters", () => {
|
|
74
|
+
const text = "Réunion demain café";
|
|
75
|
+
assert.deepStrictEqual(normalizer.tokenize(text), [
|
|
76
|
+
"réunion",
|
|
77
|
+
"demain",
|
|
78
|
+
"café",
|
|
79
|
+
]);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("handles German umlauts", () => {
|
|
83
|
+
const text = "Besprechung über München";
|
|
84
|
+
assert.deepStrictEqual(normalizer.tokenize(text), [
|
|
85
|
+
"besprechung",
|
|
86
|
+
"über",
|
|
87
|
+
"münchen",
|
|
88
|
+
]);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("normalizes whitespace", () => {
|
|
92
|
+
const text = " multiple spaces\n\ttabs ";
|
|
93
|
+
assert.deepStrictEqual(normalizer.tokenize(text), [
|
|
94
|
+
"multiple",
|
|
95
|
+
"spaces",
|
|
96
|
+
"tabs",
|
|
97
|
+
]);
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe("stem", () => {
|
|
102
|
+
it("stems English words", () => {
|
|
103
|
+
assert.strictEqual(normalizer.stem("running", "en"), "run");
|
|
104
|
+
assert.strictEqual(normalizer.stem("jumps", "en"), "jump");
|
|
105
|
+
assert.strictEqual(normalizer.stem("connected", "en"), "connect");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("stems German words", () => {
|
|
109
|
+
// German Porter stemmer has different rules than English
|
|
110
|
+
// Just verify it returns a string and doesn't throw
|
|
111
|
+
const result1 = normalizer.stem("verbindung", "de");
|
|
112
|
+
const result2 = normalizer.stem("laufend", "de");
|
|
113
|
+
assert.ok(typeof result1 === "string");
|
|
114
|
+
assert.ok(typeof result2 === "string");
|
|
115
|
+
// The stemmer should at least return something
|
|
116
|
+
assert.ok(result1.length > 0);
|
|
117
|
+
assert.ok(result2.length > 0);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("stems French words", () => {
|
|
121
|
+
assert.strictEqual(normalizer.stem("réunion", "fr"), "réunion");
|
|
122
|
+
assert.strictEqual(normalizer.stem("connexion", "fr"), "connexion");
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("defaults to English stemmer for unknown language", () => {
|
|
126
|
+
assert.strictEqual(normalizer.stem("running"), "run");
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe("removeStopwords", () => {
|
|
131
|
+
it("removes English stopwords", () => {
|
|
132
|
+
const words = ["the", "quick", "brown", "fox", "is", "running"];
|
|
133
|
+
const result = normalizer.removeStopwords(words, "en");
|
|
134
|
+
assert.ok(!result.includes("the"));
|
|
135
|
+
assert.ok(!result.includes("is"));
|
|
136
|
+
assert.ok(result.includes("quick"));
|
|
137
|
+
assert.ok(result.includes("brown"));
|
|
138
|
+
assert.ok(result.includes("fox"));
|
|
139
|
+
assert.ok(result.includes("running"));
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("removes German stopwords", () => {
|
|
143
|
+
const words = ["der", "schnelle", "fuchs", "ist", "braun"];
|
|
144
|
+
const result = normalizer.removeStopwords(words, "de");
|
|
145
|
+
assert.ok(!result.includes("der"));
|
|
146
|
+
assert.ok(!result.includes("ist"));
|
|
147
|
+
assert.ok(result.includes("schnelle"));
|
|
148
|
+
assert.ok(result.includes("fuchs"));
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("removes French stopwords", () => {
|
|
152
|
+
const words = ["le", "renard", "est", "rapide"];
|
|
153
|
+
const result = normalizer.removeStopwords(words, "fr");
|
|
154
|
+
assert.ok(!result.includes("le"));
|
|
155
|
+
assert.ok(!result.includes("est"));
|
|
156
|
+
assert.ok(result.includes("renard"));
|
|
157
|
+
assert.ok(result.includes("rapide"));
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("removes Dutch stopwords", () => {
|
|
161
|
+
const words = ["de", "snelle", "vos", "is", "bruin"];
|
|
162
|
+
const result = normalizer.removeStopwords(words, "nl");
|
|
163
|
+
assert.ok(!result.includes("de"));
|
|
164
|
+
assert.ok(!result.includes("is"));
|
|
165
|
+
assert.ok(result.includes("snelle"));
|
|
166
|
+
assert.ok(result.includes("vos"));
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
describe("normalize", () => {
|
|
171
|
+
it("normalizes English text with stemming and stopword removal", () => {
|
|
172
|
+
const text = "The quick brown fox is running quickly";
|
|
173
|
+
const result = normalizer.normalize(text, { language: "en" });
|
|
174
|
+
// Should remove "the" and "is", stem "running" and "quickly"
|
|
175
|
+
assert.ok(!result.includes("the "));
|
|
176
|
+
assert.ok(!result.includes(" is "));
|
|
177
|
+
assert.ok(result.includes("quick"));
|
|
178
|
+
assert.ok(result.includes("run"));
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("normalizes German text", () => {
|
|
182
|
+
const text = "Der schnelle braune Fuchs ist laufend";
|
|
183
|
+
const result = normalizer.normalize(text, { language: "de" });
|
|
184
|
+
assert.ok(!result.includes("der "));
|
|
185
|
+
assert.ok(!result.includes(" ist "));
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("normalizes without stemming when disabled", () => {
|
|
189
|
+
const text = "running quickly";
|
|
190
|
+
const result = normalizer.normalize(text, {
|
|
191
|
+
language: "en",
|
|
192
|
+
stem: false,
|
|
193
|
+
removeStopwords: false,
|
|
194
|
+
});
|
|
195
|
+
assert.strictEqual(result, "running quickly");
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it("handles Unicode text", () => {
|
|
199
|
+
const text = "Réunion à Paris demain";
|
|
200
|
+
const result = normalizer.normalize(text, { language: "fr" });
|
|
201
|
+
assert.ok(result.includes("réunion"));
|
|
202
|
+
assert.ok(result.includes("pari"));
|
|
203
|
+
assert.ok(result.includes("demain"));
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("deduplicates consecutive words", () => {
|
|
207
|
+
const text = "hello hello world world world";
|
|
208
|
+
const result = normalizer.normalize(text, {
|
|
209
|
+
language: "en",
|
|
210
|
+
stem: false,
|
|
211
|
+
removeStopwords: false,
|
|
212
|
+
});
|
|
213
|
+
assert.strictEqual(result, "hello world");
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("defaults to English when language not specified", () => {
|
|
217
|
+
const text = "The quick brown fox";
|
|
218
|
+
const result = normalizer.normalize(text);
|
|
219
|
+
// "the" should be removed as English stopword
|
|
220
|
+
assert.ok(!result.includes("the "));
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
});
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Multilingual text normalizer for email processing.
|
|
3
|
+
*
|
|
4
|
+
* Uses franc for language detection, natural for stemming,
|
|
5
|
+
* and stopword for stopword removal.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/// <reference path="../types/natural-porter-stemmer.d.ts" />
|
|
9
|
+
import { franc } from "franc";
|
|
10
|
+
// Import each Porter stemmer file directly instead of the `natural` package
|
|
11
|
+
// root or its `stemmers` submodule barrel: the root barrel
|
|
12
|
+
// (`natural/lib/natural/index.js`) loads WordNet and the Brill POS tagger,
|
|
13
|
+
// which pull in `mongoose` and — via its Postgres storage plugin —
|
|
14
|
+
// `pg`/`dotenv` (`dotenv.config()` at module scope on every cold start, see
|
|
15
|
+
// #1244/#1247); the `stemmers` barrel is CJS and unconditionally requires
|
|
16
|
+
// every stemmer it lists, including Japanese (which drags in its own
|
|
17
|
+
// tokenizer) and Indonesian, neither used here. This module only needs the
|
|
18
|
+
// seven Porter stemmers below — see
|
|
19
|
+
// ../types/natural-porter-stemmer.d.ts for their (hand-written, since
|
|
20
|
+
// `natural` ships none for individual files) type declarations.
|
|
21
|
+
import PorterStemmer from "natural/lib/natural/stemmers/porter_stemmer.js";
|
|
22
|
+
import PorterStemmerDe from "natural/lib/natural/stemmers/porter_stemmer_de.js";
|
|
23
|
+
import PorterStemmerEs from "natural/lib/natural/stemmers/porter_stemmer_es.js";
|
|
24
|
+
import PorterStemmerFr from "natural/lib/natural/stemmers/porter_stemmer_fr.js";
|
|
25
|
+
import PorterStemmerIt from "natural/lib/natural/stemmers/porter_stemmer_it.js";
|
|
26
|
+
import PorterStemmerNl from "natural/lib/natural/stemmers/porter_stemmer_nl.js";
|
|
27
|
+
import PorterStemmerPt from "natural/lib/natural/stemmers/porter_stemmer_pt.js";
|
|
28
|
+
import { deu, eng, fra, ita, nld, por, removeStopwords, spa } from "stopword";
|
|
29
|
+
|
|
30
|
+
export type SupportedLanguage = "en" | "de" | "fr" | "es" | "it" | "nl" | "pt";
|
|
31
|
+
|
|
32
|
+
export interface NormalizeOptions {
|
|
33
|
+
language?: SupportedLanguage;
|
|
34
|
+
stem?: boolean;
|
|
35
|
+
removeStopwords?: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface TextNormalizer {
|
|
39
|
+
detectLanguage(text: string): SupportedLanguage;
|
|
40
|
+
normalize(text: string, options?: NormalizeOptions): string;
|
|
41
|
+
tokenize(text: string): string[];
|
|
42
|
+
stem(word: string, language?: SupportedLanguage): string;
|
|
43
|
+
removeStopwords(words: string[], language?: SupportedLanguage): string[];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// ISO 639-3 (franc) → ISO 639-1 (our SupportedLanguage)
|
|
47
|
+
const iso3ToIso1: Record<string, SupportedLanguage> = {
|
|
48
|
+
eng: "en",
|
|
49
|
+
deu: "de",
|
|
50
|
+
fra: "fr",
|
|
51
|
+
nld: "nl",
|
|
52
|
+
spa: "es",
|
|
53
|
+
ita: "it",
|
|
54
|
+
por: "pt",
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// Restrict franc to only detect languages we can stem
|
|
58
|
+
const supportedIso3 = Object.keys(iso3ToIso1);
|
|
59
|
+
|
|
60
|
+
const stemmers: Record<SupportedLanguage, { stem: (word: string) => string }> =
|
|
61
|
+
{
|
|
62
|
+
en: PorterStemmer,
|
|
63
|
+
de: PorterStemmerDe,
|
|
64
|
+
fr: PorterStemmerFr,
|
|
65
|
+
es: PorterStemmerEs,
|
|
66
|
+
it: PorterStemmerIt,
|
|
67
|
+
nl: PorterStemmerNl,
|
|
68
|
+
pt: PorterStemmerPt,
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const stopwordLists: Record<SupportedLanguage, string[]> = {
|
|
72
|
+
en: eng,
|
|
73
|
+
de: deu,
|
|
74
|
+
fr: fra,
|
|
75
|
+
es: spa,
|
|
76
|
+
it: ita,
|
|
77
|
+
nl: nld,
|
|
78
|
+
pt: por,
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export const createTextNormalizer = (): TextNormalizer => ({
|
|
82
|
+
detectLanguage: (text) => {
|
|
83
|
+
// Use franc with trigram analysis, restricted to supported languages
|
|
84
|
+
const detected = franc(text, { only: supportedIso3, minLength: 10 });
|
|
85
|
+
|
|
86
|
+
// Map to our language codes, default to English
|
|
87
|
+
return iso3ToIso1[detected] ?? "en";
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
normalize: (text, options = {}) => {
|
|
91
|
+
const {
|
|
92
|
+
language = "en",
|
|
93
|
+
stem = true,
|
|
94
|
+
removeStopwords: removeStop = true,
|
|
95
|
+
} = options;
|
|
96
|
+
|
|
97
|
+
// Normalize unicode and lowercase
|
|
98
|
+
let normalized = text.normalize("NFKC").toLowerCase();
|
|
99
|
+
|
|
100
|
+
// Keep Unicode letters and numbers, remove punctuation
|
|
101
|
+
normalized = normalized.replace(/[^\p{L}\p{N}\s]/gu, " ");
|
|
102
|
+
|
|
103
|
+
// Tokenize
|
|
104
|
+
let words = normalized.split(/\s+/).filter((w) => w.length > 0);
|
|
105
|
+
|
|
106
|
+
// Remove stopwords
|
|
107
|
+
if (removeStop) {
|
|
108
|
+
const list = stopwordLists[language] ?? eng;
|
|
109
|
+
words = removeStopwords(words, list);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Stem
|
|
113
|
+
if (stem) {
|
|
114
|
+
const stemmer = stemmers[language] ?? stemmers.en;
|
|
115
|
+
words = words.map((w) => stemmer.stem(w));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Dedupe consecutive words
|
|
119
|
+
return words.filter((w, i, arr) => i === 0 || w !== arr[i - 1]).join(" ");
|
|
120
|
+
},
|
|
121
|
+
|
|
122
|
+
tokenize: (text) => {
|
|
123
|
+
return text
|
|
124
|
+
.normalize("NFKC")
|
|
125
|
+
.toLowerCase()
|
|
126
|
+
.replace(/[^\p{L}\p{N}\s]/gu, " ")
|
|
127
|
+
.split(/\s+/)
|
|
128
|
+
.filter((w) => w.length > 0);
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
stem: (word, language = "en") => {
|
|
132
|
+
const stemmer = stemmers[language] ?? stemmers.en;
|
|
133
|
+
return stemmer.stem(word.toLowerCase());
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
removeStopwords: (words, language = "en") => {
|
|
137
|
+
const list = stopwordLists[language] ?? eng;
|
|
138
|
+
return removeStopwords(words, list);
|
|
139
|
+
},
|
|
140
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `mailparser`'s `simpleParser` populates `partId` on every entry in
|
|
3
|
+
* `parsed.attachments[]` at runtime (set inside `mail-parser.js:860`), but
|
|
4
|
+
* the field is missing from `@types/mailparser`. Format is the IMAP
|
|
5
|
+
* dot-numbered path (`"1"`, `"2.2.2"`, `"3"`) — the same shape mime-walker
|
|
6
|
+
* writes to `BodyPart.partPath`. The body-part mapper uses it as the
|
|
7
|
+
* primary structural pairing signal.
|
|
8
|
+
*/
|
|
9
|
+
declare module "mailparser" {
|
|
10
|
+
interface Attachment {
|
|
11
|
+
partId?: string;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// `natural` ships no per-file type declarations for its stemmer submodules —
|
|
2
|
+
// only the package-root barrel (`natural/lib/natural/index.d.ts`) and the
|
|
3
|
+
// `stemmers` submodule barrel (`natural/lib/natural/stemmers/index.d.ts`) are
|
|
4
|
+
// typed. Both barrels are CommonJS, so importing either unconditionally
|
|
5
|
+
// requires every stemmer they list — including Japanese (`stemmer_ja.js`,
|
|
6
|
+
// which pulls in its own `tokenizer_ja.js`) and Indonesian
|
|
7
|
+
// (`indonesian/stemmer_id.js`) — even when unused. Importing each Porter
|
|
8
|
+
// stemmer file this module actually uses, directly, avoids that; these
|
|
9
|
+
// ambient declarations cover the resulting import paths. Referenced via a
|
|
10
|
+
// triple-slash directive from normalizer.ts so tsc includes it in any
|
|
11
|
+
// program that type-checks normalizer.ts, even a consuming package's (whose
|
|
12
|
+
// own tsconfig `include` doesn't reach into this package's src/types).
|
|
13
|
+
interface NaturalStemmer {
|
|
14
|
+
stem: (token: string) => string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
declare module "natural/lib/natural/stemmers/porter_stemmer.js" {
|
|
18
|
+
const stemmer: NaturalStemmer;
|
|
19
|
+
export default stemmer;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
declare module "natural/lib/natural/stemmers/porter_stemmer_de.js" {
|
|
23
|
+
const stemmer: NaturalStemmer;
|
|
24
|
+
export default stemmer;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
declare module "natural/lib/natural/stemmers/porter_stemmer_es.js" {
|
|
28
|
+
const stemmer: NaturalStemmer;
|
|
29
|
+
export default stemmer;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
declare module "natural/lib/natural/stemmers/porter_stemmer_fr.js" {
|
|
33
|
+
const stemmer: NaturalStemmer;
|
|
34
|
+
export default stemmer;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
declare module "natural/lib/natural/stemmers/porter_stemmer_it.js" {
|
|
38
|
+
const stemmer: NaturalStemmer;
|
|
39
|
+
export default stemmer;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
declare module "natural/lib/natural/stemmers/porter_stemmer_nl.js" {
|
|
43
|
+
const stemmer: NaturalStemmer;
|
|
44
|
+
export default stemmer;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
declare module "natural/lib/natural/stemmers/porter_stemmer_pt.js" {
|
|
48
|
+
const stemmer: NaturalStemmer;
|
|
49
|
+
export default stemmer;
|
|
50
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
declare module "stopword" {
|
|
2
|
+
export function removeStopwords(
|
|
3
|
+
words: string[],
|
|
4
|
+
stopwords?: string[],
|
|
5
|
+
): string[];
|
|
6
|
+
|
|
7
|
+
export const afr: string[];
|
|
8
|
+
export const ara: string[];
|
|
9
|
+
export const ben: string[];
|
|
10
|
+
export const bre: string[];
|
|
11
|
+
export const bul: string[];
|
|
12
|
+
export const cat: string[];
|
|
13
|
+
export const ces: string[];
|
|
14
|
+
export const dan: string[];
|
|
15
|
+
export const deu: string[];
|
|
16
|
+
export const ell: string[];
|
|
17
|
+
export const eng: string[];
|
|
18
|
+
export const epo: string[];
|
|
19
|
+
export const est: string[];
|
|
20
|
+
export const eus: string[];
|
|
21
|
+
export const fas: string[];
|
|
22
|
+
export const fin: string[];
|
|
23
|
+
export const fra: string[];
|
|
24
|
+
export const glg: string[];
|
|
25
|
+
export const guj: string[];
|
|
26
|
+
export const hau: string[];
|
|
27
|
+
export const heb: string[];
|
|
28
|
+
export const hin: string[];
|
|
29
|
+
export const hrv: string[];
|
|
30
|
+
export const hun: string[];
|
|
31
|
+
export const hye: string[];
|
|
32
|
+
export const ind: string[];
|
|
33
|
+
export const ita: string[];
|
|
34
|
+
export const jpn: string[];
|
|
35
|
+
export const kor: string[];
|
|
36
|
+
export const kur: string[];
|
|
37
|
+
export const lat: string[];
|
|
38
|
+
export const lav: string[];
|
|
39
|
+
export const lit: string[];
|
|
40
|
+
export const lgg: string[];
|
|
41
|
+
export const lggNd: string[];
|
|
42
|
+
export const mar: string[];
|
|
43
|
+
export const msa: string[];
|
|
44
|
+
export const mya: string[];
|
|
45
|
+
export const nld: string[];
|
|
46
|
+
export const nob: string[];
|
|
47
|
+
export const pan: string[];
|
|
48
|
+
export const pol: string[];
|
|
49
|
+
export const por: string[];
|
|
50
|
+
export const porBr: string[];
|
|
51
|
+
export const ron: string[];
|
|
52
|
+
export const rus: string[];
|
|
53
|
+
export const slk: string[];
|
|
54
|
+
export const slv: string[];
|
|
55
|
+
export const som: string[];
|
|
56
|
+
export const sot: string[];
|
|
57
|
+
export const spa: string[];
|
|
58
|
+
export const swa: string[];
|
|
59
|
+
export const swe: string[];
|
|
60
|
+
export const tgl: string[];
|
|
61
|
+
export const tha: string[];
|
|
62
|
+
export const tur: string[];
|
|
63
|
+
export const ukr: string[];
|
|
64
|
+
export const urd: string[];
|
|
65
|
+
export const vie: string[];
|
|
66
|
+
export const yor: string[];
|
|
67
|
+
export const zho: string[];
|
|
68
|
+
export const zul: string[];
|
|
69
|
+
}
|