w1913 0.0.0-stage → 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/LICENSE +29 -0
- package/README.md +61 -2
- package/dist/fold.d.ts +4 -0
- package/dist/fold.js +22 -0
- package/dist/format.d.ts +43 -0
- package/dist/format.js +3 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +5 -0
- package/dist/inflect.d.ts +23 -0
- package/dist/inflect.js +102 -0
- package/dist/lookup.d.ts +56 -0
- package/dist/lookup.js +120 -0
- package/dist/shards.d.ts +2 -0
- package/dist/shards.js +34 -0
- package/package.json +39 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Christopher Schweda
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
This licence covers the code, documentation, and design in this repository.
|
|
26
|
+
The published dictionary edition (the w1913-<version>.jsonl releases) is
|
|
27
|
+
dedicated to the public domain under CC0 1.0; see LICENSE-DATA. The text of
|
|
28
|
+
Webster's Revised Unabridged Dictionary (1913) is itself in the public domain
|
|
29
|
+
in the United States.
|
package/README.md
CHANGED
|
@@ -1,3 +1,62 @@
|
|
|
1
|
-
#
|
|
1
|
+
# w1913
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Look words up in *Webster's Revised Unabridged Dictionary* (1913) inside your
|
|
4
|
+
own program, offline, with the same answers as [w1913.com](https://w1913.com).
|
|
5
|
+
No API, no keys, no network: you carry the data, and nothing a writer types
|
|
6
|
+
leaves their device.
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
npm install w1913
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { createLookup } from "w1913";
|
|
14
|
+
|
|
15
|
+
const build = await (await fetch("/w1913-lookup-0.7.json")).json(); // or readFile in Electron/Node
|
|
16
|
+
const dictionary = createLookup(build);
|
|
17
|
+
|
|
18
|
+
dictionary.lookup("goeth");
|
|
19
|
+
// { slug: "go", headword: "Go", via: "goeth", url: "https://w1913.com/go",
|
|
20
|
+
// entries: [{ pos: "v. i.", senses: [{ n: 1, text: "To pass from one place to another; …", sub: [] }, …] }, …] }
|
|
21
|
+
|
|
22
|
+
dictionary.lookup("bill of fare");
|
|
23
|
+
// { slug: "bill", headword: "Bill", via: null, url: "https://w1913.com/bill",
|
|
24
|
+
// phrase: { phrases: ["Bill of fare"], definition: "a written or printed enumeration of the dishes …",
|
|
25
|
+
// anchor: "n4-p-bill-of-fare", url: "https://w1913.com/bill#n4-p-bill-of-fare" }, entries: [ … ] }
|
|
26
|
+
|
|
27
|
+
dictionary.lookup("smartphone"); // null: not in this dictionary
|
|
28
|
+
dictionary.suggest("saun"); // [{ headword: "Saunter", slug: "saunter" }, …]
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## The data
|
|
32
|
+
|
|
33
|
+
- **The lookup build**, `w1913-lookup-<edition>.json` (24.8 MB; 8.1 MB gzipped, edition 0.7):
|
|
34
|
+
every headword, variant and listed inflection; every entry's part of speech and senses, with
|
|
35
|
+
block quotations left out; and, from edition 0.7, the phrases defined inside entries (24,628,
|
|
36
|
+
such as *Bill of fare*), each with its definition and its anchor on w1913.com. A word of several
|
|
37
|
+
parts that is not a page of its own but is such a phrase comes back with `phrase` set; every
|
|
38
|
+
entry lists its `phrases`. Older builds simply have none. Published with each W1913 release that changes the data, on the
|
|
39
|
+
[releases page](https://github.com/cschweda/w1913/releases). CC0 1.0.
|
|
40
|
+
- **Static shards** at `https://w1913.com/data/<edition>/<prefix>.json` (380 files, one per
|
|
41
|
+
two-letter prefix; `index.json` lists them), for a browser program that may fetch from
|
|
42
|
+
w1913.com:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { createShardedLookup } from "w1913";
|
|
46
|
+
const dictionary = createShardedLookup((prefix) => fetch(`https://w1913.com/data/0.7/${prefix}.json`).then((r) => (r.ok ? r.json() : null)));
|
|
47
|
+
await dictionary.lookup("walked");
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Same answers as the site
|
|
51
|
+
|
|
52
|
+
The folding (`foldForCompare`) and the suffix stripper (`stems`, `lookupWith`) in this package
|
|
53
|
+
are the ones w1913.com itself runs; the site imports them from here. A test checks that
|
|
54
|
+
`lookup()` agrees with the site on 200 fixed words, from the whole build and from shards.
|
|
55
|
+
|
|
56
|
+
A word is found when it is a headword, a variant, or an inflection the dictionary lists
|
|
57
|
+
(*went*, *oxen*); otherwise its stems are tried (*walked* → walk, *goeth* → go, *criteria* →
|
|
58
|
+
criterion). `via` names the form that led there. Prefixes and suffixes ("Com-") are never stems.
|
|
59
|
+
|
|
60
|
+
## Licence
|
|
61
|
+
|
|
62
|
+
Code: MIT. Data: CC0 1.0 (the 1913 text is in the US public domain).
|
package/dist/fold.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Rule: Æ/Œ spelled out, diacritics removed, lower case, letters and digits only. */
|
|
2
|
+
export declare function foldForCompare(s: string): string;
|
|
3
|
+
/** Rule: like folding, but runs of other characters become one hyphen ("Sea salmon" → sea-salmon). */
|
|
4
|
+
export declare function slugify(s: string): string;
|
package/dist/fold.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Folding and slugs: the same rules as the edition (packages/data) and the
|
|
2
|
+
// site (apps/site imports these), so a word finds the same entry everywhere.
|
|
3
|
+
/** Rule: Æ/Œ spelled out, diacritics removed, lower case, letters and digits only. */
|
|
4
|
+
export function foldForCompare(s) {
|
|
5
|
+
return s
|
|
6
|
+
.replace(/Æ/g, "AE").replace(/æ/g, "ae").replace(/Œ/g, "OE").replace(/œ/g, "oe")
|
|
7
|
+
.normalize("NFKD")
|
|
8
|
+
.replace(/\p{M}/gu, "")
|
|
9
|
+
.toLowerCase()
|
|
10
|
+
.replace(/[^a-z0-9]/g, "");
|
|
11
|
+
}
|
|
12
|
+
/** Rule: like folding, but runs of other characters become one hyphen ("Sea salmon" → sea-salmon). */
|
|
13
|
+
export function slugify(s) {
|
|
14
|
+
return s
|
|
15
|
+
.replace(/Æ/g, "AE").replace(/æ/g, "ae").replace(/Œ/g, "OE").replace(/œ/g, "oe")
|
|
16
|
+
.normalize("NFKD")
|
|
17
|
+
.replace(/\p{M}/gu, "")
|
|
18
|
+
.replace(/[*"`]/g, "")
|
|
19
|
+
.toLowerCase()
|
|
20
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
21
|
+
.replace(/^-+|-+$/g, "");
|
|
22
|
+
}
|
package/dist/format.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** A sense: its printed number (or null), its text without block quotations or phrases, and its lettered sub-senses. */
|
|
2
|
+
export type BuildSense = [n: number | null, text: string, sub?: [letter: string, text: string][]];
|
|
3
|
+
/** A phrase defined in an entry (edition 0.7): its phrases as printed (several share one definition), the definition, and its anchor on the page. */
|
|
4
|
+
export type BuildPhrase = [phrases: string[], definition: string, anchor: string];
|
|
5
|
+
/** An entry on a page: its part of speech (or null), senses, and phrases (from edition 0.7; absent when none). */
|
|
6
|
+
export type BuildEntry = [pos: string | null, senses: BuildSense[], phrases?: BuildPhrase[]];
|
|
7
|
+
/** A page: every homograph of one headword, as on w1913.com/<slug>. */
|
|
8
|
+
export type BuildPage = [slug: string, headword: string, entries: BuildEntry[]];
|
|
9
|
+
export interface LookupBuild {
|
|
10
|
+
format: "w1913-lookup";
|
|
11
|
+
/** Version of this file format. */
|
|
12
|
+
formatVersion: 1;
|
|
13
|
+
/** The W1913 edition it was made from, and the site version that made it. */
|
|
14
|
+
edition: string;
|
|
15
|
+
siteVersion: string;
|
|
16
|
+
built: string;
|
|
17
|
+
licence: "CC0-1.0";
|
|
18
|
+
source: string;
|
|
19
|
+
/** Base address of entries; a reserved slug lives under /w/. */
|
|
20
|
+
site: string;
|
|
21
|
+
reserved: string[];
|
|
22
|
+
pages: BuildPage[];
|
|
23
|
+
/** Folded headword, variant or listed inflection → page index. Headwords and variants win over inflections; then dictionary order. */
|
|
24
|
+
forms: Record<string, number>;
|
|
25
|
+
/** Where the answer for a stem differs (prefixes and suffixes are never stems): folded form → page index, or -1 for none. */
|
|
26
|
+
stemForms: Record<string, number>;
|
|
27
|
+
/** From edition 0.7: folded phrase ("billoffare") → [page index, anchor]. Absent in older builds. */
|
|
28
|
+
phrases?: Record<string, [page: number, anchor: string]>;
|
|
29
|
+
}
|
|
30
|
+
/** A shard (DESIGN.md §4b static shards): the forms starting with two letters, and the pages they reach. */
|
|
31
|
+
export interface LookupShard {
|
|
32
|
+
format: "w1913-lookup-shard";
|
|
33
|
+
formatVersion: 1;
|
|
34
|
+
edition: string;
|
|
35
|
+
prefix: string;
|
|
36
|
+
site: string;
|
|
37
|
+
reserved: string[];
|
|
38
|
+
pages: Record<string, BuildPage>;
|
|
39
|
+
forms: Record<string, string>;
|
|
40
|
+
stemForms: Record<string, string | null>;
|
|
41
|
+
/** From edition 0.7: folded phrases starting with the prefix → [slug, anchor]. */
|
|
42
|
+
phrases?: Record<string, [slug: string, anchor: string]>;
|
|
43
|
+
}
|
package/dist/format.js
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { createLookup, createShardedLookup, shardPrefix, type Result, type Entry, type Sense, type Phrase } from "./lookup.ts";
|
|
2
|
+
export { foldForCompare, slugify } from "./fold.ts";
|
|
3
|
+
export { stems, lookupWith } from "./inflect.ts";
|
|
4
|
+
export type { LookupBuild, LookupShard, BuildPage, BuildEntry, BuildSense, BuildPhrase } from "./format.ts";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// w1913 (npm; packages/lookup): look words up in the 1913 Webster's offline, with the same
|
|
2
|
+
// answers as w1913.com. See README.md.
|
|
3
|
+
export { createLookup, createShardedLookup, shardPrefix } from "./lookup.js";
|
|
4
|
+
export { foldForCompare, slugify } from "./fold.js";
|
|
5
|
+
export { stems, lookupWith } from "./inflect.js";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rule: the base forms a word may be inflected from, most likely first, never
|
|
3
|
+
* the word itself. Covers plurals and the third person (-s, -es, -ies),
|
|
4
|
+
* possessives ('s, s'), past and participles (-ed, -ied, -ing, with a
|
|
5
|
+
* restored -e or an undoubled consonant: loved → love, stopped → stop),
|
|
6
|
+
* comparatives (-er, -est, -ier, -iest), adverbs (-ly, -ily), and the
|
|
7
|
+
* archaic forms the 1913's quotations use (-eth, -est, -edst, -'d, -'st).
|
|
8
|
+
* and the learned plurals (-ae → -a, -i → -us, -a → -um or -on, -ses → -sis,
|
|
9
|
+
* -ices → -ix or -ex). Candidates shorter than two letters are dropped.
|
|
10
|
+
*/
|
|
11
|
+
export declare function stems(word: string): string[];
|
|
12
|
+
/**
|
|
13
|
+
* Rule (DESIGN.md §4, inflected lookup): the word's own entry when it is a
|
|
14
|
+
* headword, variant or listed inflection (`exact`); otherwise the first of
|
|
15
|
+
* its stems() that is one ("walked" → walk, "goeth" → go). `via` names the
|
|
16
|
+
* form that led there, or is null for a direct match.
|
|
17
|
+
*/
|
|
18
|
+
export declare function lookupWith(word: string, exact: (w: string) => string | null, fold: (s: string) => string,
|
|
19
|
+
/** Lookup for stems; by default exact. The server's leaves out prefixes and suffixes ("Com-"), which are never stems. */
|
|
20
|
+
stemExact?: (w: string) => string | null): {
|
|
21
|
+
slug: string;
|
|
22
|
+
via: string | null;
|
|
23
|
+
} | null;
|
package/dist/inflect.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// The suffix stripper (DESIGN.md §2 problem 9, §4): from an inflected or
|
|
2
|
+
// archaic form to the forms it may come from. One module, used by the server
|
|
3
|
+
// (lookup, unusual words) and the browser (/check), so both give the same
|
|
4
|
+
// answer. It proposes candidates; only those that are headwords count.
|
|
5
|
+
const VOWEL = /[aeiouy]/;
|
|
6
|
+
const doubled = (s) => s.length >= 3 && s.at(-1) === s.at(-2) && !VOWEL.test(s.at(-1)) && !/(?:ll|ss|ff|zz)$/.test(s);
|
|
7
|
+
/**
|
|
8
|
+
* Rule: the base forms a word may be inflected from, most likely first, never
|
|
9
|
+
* the word itself. Covers plurals and the third person (-s, -es, -ies),
|
|
10
|
+
* possessives ('s, s'), past and participles (-ed, -ied, -ing, with a
|
|
11
|
+
* restored -e or an undoubled consonant: loved → love, stopped → stop),
|
|
12
|
+
* comparatives (-er, -est, -ier, -iest), adverbs (-ly, -ily), and the
|
|
13
|
+
* archaic forms the 1913's quotations use (-eth, -est, -edst, -'d, -'st).
|
|
14
|
+
* and the learned plurals (-ae → -a, -i → -us, -a → -um or -on, -ses → -sis,
|
|
15
|
+
* -ices → -ix or -ex). Candidates shorter than two letters are dropped.
|
|
16
|
+
*/
|
|
17
|
+
export function stems(word) {
|
|
18
|
+
const w = word.toLowerCase().replace(/[’]/g, "'");
|
|
19
|
+
const out = [];
|
|
20
|
+
const add = (s) => { if (s.length >= 2 && s !== w && !out.includes(s))
|
|
21
|
+
out.push(s); };
|
|
22
|
+
const root = (s) => { add(s); add(s + "e"); if (doubled(s))
|
|
23
|
+
add(s.slice(0, -1)); };
|
|
24
|
+
if (/'s$/.test(w))
|
|
25
|
+
add(w.slice(0, -2));
|
|
26
|
+
if (/s'$/.test(w))
|
|
27
|
+
add(w.slice(0, -1));
|
|
28
|
+
if (/'d$/.test(w))
|
|
29
|
+
root(w.slice(0, -2));
|
|
30
|
+
if (/'st$/.test(w))
|
|
31
|
+
root(w.slice(0, -3));
|
|
32
|
+
if (/ies$/.test(w))
|
|
33
|
+
add(w.slice(0, -3) + "y");
|
|
34
|
+
if (/(?:s|x|z|ch|sh|o)es$/.test(w))
|
|
35
|
+
add(w.slice(0, -2));
|
|
36
|
+
if (/[^s]s$/.test(w))
|
|
37
|
+
add(w.slice(0, -1));
|
|
38
|
+
if (/ied$/.test(w))
|
|
39
|
+
add(w.slice(0, -3) + "y");
|
|
40
|
+
if (/edst$/.test(w))
|
|
41
|
+
root(w.slice(0, -4));
|
|
42
|
+
if (/ed$/.test(w))
|
|
43
|
+
root(w.slice(0, -2));
|
|
44
|
+
if (/ing$/.test(w))
|
|
45
|
+
root(w.slice(0, -3));
|
|
46
|
+
if (/eth$/.test(w))
|
|
47
|
+
root(w.slice(0, -3));
|
|
48
|
+
if (/iest$/.test(w))
|
|
49
|
+
add(w.slice(0, -4) + "y");
|
|
50
|
+
if (/ier$/.test(w))
|
|
51
|
+
add(w.slice(0, -3) + "y");
|
|
52
|
+
if (/est$/.test(w))
|
|
53
|
+
root(w.slice(0, -3));
|
|
54
|
+
if (/st$/.test(w) && !/est$/.test(w))
|
|
55
|
+
add(w.slice(0, -2));
|
|
56
|
+
if (/er$/.test(w))
|
|
57
|
+
root(w.slice(0, -2));
|
|
58
|
+
if (/ily$/.test(w))
|
|
59
|
+
add(w.slice(0, -3) + "y");
|
|
60
|
+
if (/ly$/.test(w)) {
|
|
61
|
+
add(w.slice(0, -2));
|
|
62
|
+
add(w.slice(0, -2) + "le");
|
|
63
|
+
}
|
|
64
|
+
if (/ies$/.test(w) || /ied$/.test(w))
|
|
65
|
+
add(w.slice(0, -1));
|
|
66
|
+
if (/ae$/.test(w))
|
|
67
|
+
add(w.slice(0, -1));
|
|
68
|
+
if (/ices$/.test(w)) {
|
|
69
|
+
add(w.slice(0, -4) + "ix");
|
|
70
|
+
add(w.slice(0, -4) + "ex");
|
|
71
|
+
}
|
|
72
|
+
if (/ses$/.test(w))
|
|
73
|
+
add(w.slice(0, -2) + "is");
|
|
74
|
+
if (/i$/.test(w))
|
|
75
|
+
add(w.slice(0, -1) + "us");
|
|
76
|
+
if (/a$/.test(w)) {
|
|
77
|
+
add(w.slice(0, -1) + "um");
|
|
78
|
+
add(w.slice(0, -1) + "on");
|
|
79
|
+
}
|
|
80
|
+
return out;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Rule (DESIGN.md §4, inflected lookup): the word's own entry when it is a
|
|
84
|
+
* headword, variant or listed inflection (`exact`); otherwise the first of
|
|
85
|
+
* its stems() that is one ("walked" → walk, "goeth" → go). `via` names the
|
|
86
|
+
* form that led there, or is null for a direct match.
|
|
87
|
+
*/
|
|
88
|
+
export function lookupWith(word, exact, fold,
|
|
89
|
+
/** Lookup for stems; by default exact. The server's leaves out prefixes and suffixes ("Com-"), which are never stems. */
|
|
90
|
+
stemExact = exact) {
|
|
91
|
+
const direct = exact(word);
|
|
92
|
+
if (direct)
|
|
93
|
+
return { slug: direct, via: null };
|
|
94
|
+
if (!fold(word))
|
|
95
|
+
return null;
|
|
96
|
+
for (const s of stems(word.toLowerCase())) {
|
|
97
|
+
const slug = stemExact(s);
|
|
98
|
+
if (slug)
|
|
99
|
+
return { slug, via: word };
|
|
100
|
+
}
|
|
101
|
+
return null;
|
|
102
|
+
}
|
package/dist/lookup.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { LookupBuild, LookupShard } from "./format.ts";
|
|
2
|
+
export interface Sense {
|
|
3
|
+
n: number | null;
|
|
4
|
+
text: string;
|
|
5
|
+
sub: {
|
|
6
|
+
letter: string;
|
|
7
|
+
text: string;
|
|
8
|
+
}[];
|
|
9
|
+
}
|
|
10
|
+
/** A phrase defined in an entry: its phrases as printed, its definition, and the anchor (of its first phrase, or of the one asked for) on w1913.com. */
|
|
11
|
+
export interface Phrase {
|
|
12
|
+
phrases: string[];
|
|
13
|
+
definition: string;
|
|
14
|
+
anchor: string;
|
|
15
|
+
url: string;
|
|
16
|
+
}
|
|
17
|
+
export interface Entry {
|
|
18
|
+
pos: string | null;
|
|
19
|
+
senses: Sense[];
|
|
20
|
+
phrases: Phrase[];
|
|
21
|
+
}
|
|
22
|
+
export interface Result {
|
|
23
|
+
slug: string;
|
|
24
|
+
headword: string;
|
|
25
|
+
/** The entry's address on w1913.com. */
|
|
26
|
+
url: string;
|
|
27
|
+
/** The word as given, when it was matched as an inflected form ("walked" for walk); null for a direct match. */
|
|
28
|
+
via: string | null;
|
|
29
|
+
/** The phrase asked for, when the word was one defined inside the entry ("bill of fare" under bill); null otherwise. */
|
|
30
|
+
phrase: Phrase | null;
|
|
31
|
+
entries: Entry[];
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Look words up in a whole lookup build (loaded once, then instant and
|
|
35
|
+
* offline). `lookup` returns null for a word the dictionary does not have.
|
|
36
|
+
*/
|
|
37
|
+
export declare function createLookup(build: LookupBuild): {
|
|
38
|
+
edition: string;
|
|
39
|
+
lookup(word: string): Result | null;
|
|
40
|
+
/** Headwords starting with a prefix (folded), in dictionary order, at most `limit`. */
|
|
41
|
+
suggest(prefix: string, limit?: number): {
|
|
42
|
+
headword: string;
|
|
43
|
+
slug: string;
|
|
44
|
+
}[];
|
|
45
|
+
};
|
|
46
|
+
/** The shard a folded word lives in: its first two letters (or one, for a one-letter word). */
|
|
47
|
+
export declare const shardPrefix: (fold: string) => string;
|
|
48
|
+
/**
|
|
49
|
+
* Look words up from static shards (w1913.com/data/<edition>/<prefix>.json),
|
|
50
|
+
* fetching only the shards a word needs, through `fetchShard`, which the
|
|
51
|
+
* program supplies (and may cache). A word and its stems share their first
|
|
52
|
+
* two letters, so one shard almost always answers.
|
|
53
|
+
*/
|
|
54
|
+
export declare function createShardedLookup(fetchShard: (prefix: string) => Promise<LookupShard | null>): {
|
|
55
|
+
lookup(word: string): Promise<Result | null>;
|
|
56
|
+
};
|
package/dist/lookup.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// lookup(word) over a lookup build or static shards, with the same folding
|
|
2
|
+
// and suffix stripping as w1913.com (DESIGN.md §4b).
|
|
3
|
+
import { foldForCompare, slugify } from "./fold.js";
|
|
4
|
+
import { lookupWith, stems } from "./inflect.js";
|
|
5
|
+
function toResult(page, via, site, reserved, match = null) {
|
|
6
|
+
const [slug, headword, entries] = page;
|
|
7
|
+
const url = `${site}/${reserved.has(slug) ? `w/${slug}` : slug}`;
|
|
8
|
+
const out = entries.map(([pos, senses, phrases]) => ({
|
|
9
|
+
pos,
|
|
10
|
+
senses: senses.map(([n, text, sub]) => ({ n, text, sub: (sub ?? []).map(([letter, t]) => ({ letter, text: t })) })),
|
|
11
|
+
phrases: (phrases ?? []).map(([ps, definition, a]) => ({ phrases: ps, definition, anchor: a, url: `${url}#${a}` })),
|
|
12
|
+
}));
|
|
13
|
+
// The run holding the phrase asked for, pointed at that phrase's own anchor ("Love apple" in "Apple of love, or Love apple").
|
|
14
|
+
const run = match ? out.flatMap((e) => e.phrases).find((p) => p.phrases.some((x) => foldForCompare(x) === match.fold)) : undefined;
|
|
15
|
+
const phrase = run && match ? { ...run, anchor: match.anchor, url: `${url}#${match.anchor}` } : null;
|
|
16
|
+
return { slug, headword, url, via, phrase, entries: out };
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Rule (as on w1913.com/bill-of-fare): a word of several parts that is not a
|
|
20
|
+
* page of its own, but is a phrase defined inside an entry, is that phrase;
|
|
21
|
+
* this comes before inflections and stems.
|
|
22
|
+
*/
|
|
23
|
+
const asPhrase = (word, isPage) => {
|
|
24
|
+
const slug = slugify(word);
|
|
25
|
+
return slug.includes("-") && !isPage(slug) ? foldForCompare(word) : null;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Look words up in a whole lookup build (loaded once, then instant and
|
|
29
|
+
* offline). `lookup` returns null for a word the dictionary does not have.
|
|
30
|
+
*/
|
|
31
|
+
export function createLookup(build) {
|
|
32
|
+
if (build.format !== "w1913-lookup" || build.formatVersion !== 1)
|
|
33
|
+
throw new Error("Not a W1913 lookup build (format 1).");
|
|
34
|
+
const reserved = new Set(build.reserved);
|
|
35
|
+
const exact = (w) => {
|
|
36
|
+
const i = build.forms[foldForCompare(w)];
|
|
37
|
+
return i === undefined ? null : build.pages[i][0];
|
|
38
|
+
};
|
|
39
|
+
const stemExact = (w) => {
|
|
40
|
+
const f = foldForCompare(w);
|
|
41
|
+
const i = f in build.stemForms ? build.stemForms[f] : build.forms[f];
|
|
42
|
+
return i === undefined || i < 0 ? null : build.pages[i][0];
|
|
43
|
+
};
|
|
44
|
+
const bySlug = new Map(build.pages.map((p) => [p[0], p]));
|
|
45
|
+
return {
|
|
46
|
+
edition: build.edition,
|
|
47
|
+
lookup(word) {
|
|
48
|
+
const pf = asPhrase(word, (s) => bySlug.has(s));
|
|
49
|
+
const hit = pf ? build.phrases?.[pf] : undefined;
|
|
50
|
+
if (hit)
|
|
51
|
+
return toResult(build.pages[hit[0]], null, build.site, reserved, { fold: pf, anchor: hit[1] });
|
|
52
|
+
const r = lookupWith(word, exact, foldForCompare, stemExact);
|
|
53
|
+
return r ? toResult(bySlug.get(r.slug), r.via, build.site, reserved) : null;
|
|
54
|
+
},
|
|
55
|
+
/** Headwords starting with a prefix (folded), in dictionary order, at most `limit`. */
|
|
56
|
+
suggest(prefix, limit = 8) {
|
|
57
|
+
const f = foldForCompare(prefix);
|
|
58
|
+
if (!f)
|
|
59
|
+
return [];
|
|
60
|
+
const out = [];
|
|
61
|
+
for (const p of build.pages) {
|
|
62
|
+
if (foldForCompare(p[1]).startsWith(f))
|
|
63
|
+
out.push({ headword: p[1], slug: p[0] });
|
|
64
|
+
if (out.length >= limit)
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/** The shard a folded word lives in: its first two letters (or one, for a one-letter word). */
|
|
72
|
+
export const shardPrefix = (fold) => fold.slice(0, 2);
|
|
73
|
+
/**
|
|
74
|
+
* Look words up from static shards (w1913.com/data/<edition>/<prefix>.json),
|
|
75
|
+
* fetching only the shards a word needs, through `fetchShard`, which the
|
|
76
|
+
* program supplies (and may cache). A word and its stems share their first
|
|
77
|
+
* two letters, so one shard almost always answers.
|
|
78
|
+
*/
|
|
79
|
+
export function createShardedLookup(fetchShard) {
|
|
80
|
+
const shards = new Map();
|
|
81
|
+
const shard = (prefix) => {
|
|
82
|
+
let s = shards.get(prefix);
|
|
83
|
+
if (!s)
|
|
84
|
+
shards.set(prefix, (s = fetchShard(prefix).catch(() => null)));
|
|
85
|
+
return s;
|
|
86
|
+
};
|
|
87
|
+
return {
|
|
88
|
+
async lookup(word) {
|
|
89
|
+
const f = foldForCompare(word);
|
|
90
|
+
if (!f)
|
|
91
|
+
return null;
|
|
92
|
+
const prefixes = [...new Set([f, ...stems(word.toLowerCase()).map(foldForCompare)].filter(Boolean).map(shardPrefix))];
|
|
93
|
+
const loaded = (await Promise.all(prefixes.map(shard))).filter((s) => !!s);
|
|
94
|
+
if (!loaded.length)
|
|
95
|
+
return null;
|
|
96
|
+
const pages = new Map();
|
|
97
|
+
for (const s of loaded)
|
|
98
|
+
for (const [k, p] of Object.entries(s.pages))
|
|
99
|
+
pages.set(k, p);
|
|
100
|
+
const own = loaded.find((x) => x.prefix === shardPrefix(f));
|
|
101
|
+
// Every page's slug is a form, so a page "exists" when its folded slug is in the shard's forms as itself.
|
|
102
|
+
const pf = asPhrase(word, (s) => own?.forms[foldForCompare(s)] === s);
|
|
103
|
+
const hit = pf ? own?.phrases?.[pf] : undefined;
|
|
104
|
+
if (hit && pages.has(hit[0]))
|
|
105
|
+
return toResult(pages.get(hit[0]), null, loaded[0].site, new Set(loaded[0].reserved), { fold: pf, anchor: hit[1] });
|
|
106
|
+
const find = (w, stem) => {
|
|
107
|
+
const g = foldForCompare(w);
|
|
108
|
+
const s = loaded.find((x) => x.prefix === shardPrefix(g));
|
|
109
|
+
if (!s)
|
|
110
|
+
return null;
|
|
111
|
+
if (stem && g in s.stemForms)
|
|
112
|
+
return s.stemForms[g] ?? null;
|
|
113
|
+
return s.forms[g] ?? null;
|
|
114
|
+
};
|
|
115
|
+
const r = lookupWith(word, (w) => find(w, false), foldForCompare, (w) => find(w, true));
|
|
116
|
+
const page = r && pages.get(r.slug);
|
|
117
|
+
return page ? toResult(page, r.via, loaded[0].site, new Set(loaded[0].reserved)) : null;
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
}
|
package/dist/shards.d.ts
ADDED
package/dist/shards.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { shardPrefix } from "./lookup.js";
|
|
2
|
+
export function makeShards(build) {
|
|
3
|
+
const out = new Map();
|
|
4
|
+
const get = (prefix) => {
|
|
5
|
+
let s = out.get(prefix);
|
|
6
|
+
if (!s)
|
|
7
|
+
out.set(prefix, (s = { format: "w1913-lookup-shard", formatVersion: 1, edition: build.edition, prefix, site: build.site, reserved: build.reserved, pages: {}, forms: {}, stemForms: {} }));
|
|
8
|
+
return s;
|
|
9
|
+
};
|
|
10
|
+
const add = (s, p) => { s.pages[p[0]] = p; };
|
|
11
|
+
for (const [fold, i] of Object.entries(build.forms)) {
|
|
12
|
+
const s = get(shardPrefix(fold));
|
|
13
|
+
const p = build.pages[i];
|
|
14
|
+
s.forms[fold] = p[0];
|
|
15
|
+
add(s, p);
|
|
16
|
+
}
|
|
17
|
+
for (const [fold, [i, anchor]] of Object.entries(build.phrases ?? {})) {
|
|
18
|
+
const s = get(shardPrefix(fold));
|
|
19
|
+
const p = build.pages[i];
|
|
20
|
+
(s.phrases ??= {})[fold] = [p[0], anchor];
|
|
21
|
+
add(s, p);
|
|
22
|
+
}
|
|
23
|
+
for (const [fold, i] of Object.entries(build.stemForms)) {
|
|
24
|
+
const s = get(shardPrefix(fold));
|
|
25
|
+
if (i < 0)
|
|
26
|
+
s.stemForms[fold] = null;
|
|
27
|
+
else {
|
|
28
|
+
const p = build.pages[i];
|
|
29
|
+
s.stemForms[fold] = p[0];
|
|
30
|
+
add(s, p);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return out;
|
|
34
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "w1913",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Look words up in the 1913 Webster's offline, with the same answers as w1913.com.",
|
|
5
|
+
"homepage": "https://w1913.com",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"README.md",
|
|
17
|
+
"LICENSE"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsc -p tsconfig.build.json",
|
|
21
|
+
"test": "vitest run",
|
|
22
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
23
|
+
"prepublishOnly": "tsc -p tsconfig.build.json"
|
|
24
|
+
},
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/cschweda/w1913.git",
|
|
28
|
+
"directory": "packages/lookup"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"dictionary",
|
|
32
|
+
"webster",
|
|
33
|
+
"1913",
|
|
34
|
+
"offline",
|
|
35
|
+
"lookup"
|
|
36
|
+
],
|
|
37
|
+
"sideEffects": false,
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=20"
|
|
40
|
+
}
|
|
41
|
+
}
|