nepalipi 0.0.0-stage → 2.4.12

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.
Files changed (62) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/LICENSE +21 -0
  3. package/LICENSES.md +15 -0
  4. package/README.md +85 -2
  5. package/dist/browser/caret-popup.d.ts +14 -0
  6. package/dist/browser/caret-popup.js +158 -0
  7. package/dist/browser/controller.d.ts +37 -0
  8. package/dist/browser/controller.js +371 -0
  9. package/dist/browser/hints.d.ts +6 -0
  10. package/dist/browser/hints.js +48 -0
  11. package/dist/browser/types.d.ts +23 -0
  12. package/dist/browser/types.js +1 -0
  13. package/dist/browser/word-at-caret.d.ts +5 -0
  14. package/dist/browser/word-at-caret.js +12 -0
  15. package/dist/engine/Aksharantar-README.md +252 -0
  16. package/dist/engine/MODEL-NOTICE.md +141 -0
  17. package/dist/engine/context.d.ts +13 -0
  18. package/dist/engine/context.js +181 -0
  19. package/dist/engine/core-lexicon.js +3 -0
  20. package/dist/engine/corrections.d.ts +2 -0
  21. package/dist/engine/corrections.js +26 -0
  22. package/dist/engine/decoder-config.d.ts +9 -0
  23. package/dist/engine/decoder-config.js +2 -0
  24. package/dist/engine/dictionary-source/LGPL-2.1.txt +501 -0
  25. package/dist/engine/dictionary-source/README_ne_NP.txt +26 -0
  26. package/dist/engine/dictionary-source/ne_NP.dic +39925 -0
  27. package/dist/engine/dictionary-source/provenance.json +14 -0
  28. package/dist/engine/engine.d.ts +18 -0
  29. package/dist/engine/engine.js +283 -0
  30. package/dist/engine/lexicon.d.ts +2 -0
  31. package/dist/engine/lexicon.js +422 -0
  32. package/dist/engine/morphology.d.ts +15 -0
  33. package/dist/engine/morphology.js +212 -0
  34. package/dist/engine/native-lexicon-data.js +2 -0
  35. package/dist/engine/native-lexicon.d.ts +11 -0
  36. package/dist/engine/native-lexicon.js +67 -0
  37. package/dist/engine/orthography-data.js +2 -0
  38. package/dist/engine/orthography.d.ts +5 -0
  39. package/dist/engine/orthography.js +27 -0
  40. package/dist/engine/position-data.js +2 -0
  41. package/dist/engine/preferences.d.ts +18 -0
  42. package/dist/engine/preferences.js +48 -0
  43. package/dist/engine/rules.d.ts +8 -0
  44. package/dist/engine/rules.js +80 -0
  45. package/dist/engine/syllables.d.ts +1 -0
  46. package/dist/engine/syllables.js +57 -0
  47. package/dist/engine/transducer-data.js +2 -0
  48. package/dist/engine/transducer.d.ts +14 -0
  49. package/dist/engine/transducer.js +82 -0
  50. package/dist/engine/types.d.ts +26 -0
  51. package/dist/engine/types.js +1 -0
  52. package/dist/engine/unit-language-data.js +4 -0
  53. package/dist/engine/unit-language.d.ts +1 -0
  54. package/dist/engine/unit-language.js +20 -0
  55. package/dist/engine/unpack-unit-language.d.ts +2 -0
  56. package/dist/engine/unpack-unit-language.js +41 -0
  57. package/dist/index.cjs +4 -0
  58. package/dist/index.d.cts +2 -0
  59. package/dist/index.d.ts +58 -0
  60. package/dist/index.js +265 -0
  61. package/package.json +58 -4
  62. package/scripts/build-native-lexicon.py +73 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ # Changelog
2
+
3
+ ## 2.4.12
4
+
5
+ - Link the npm package to the live Nepalipi playground.
6
+
7
+ ## 2.4.11
8
+
9
+ - Add npm documentation and issue links, browser bundler entry fields, and the changelog to the release tarball.
10
+
11
+ - Label the period-key choices पूर्णविराम and दशमलव.
12
+
13
+ - Make upstream data credits, the paper citation, and separate licensing terms visible in Git and npm.
14
+
15
+ - Move the browser SDK and handwritten engine source to strict TypeScript.
16
+ - Ship ESM, a CommonJS entry, and format-specific TypeScript declarations.
17
+ - Preserve the generated model tables byte for byte in the built package.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nepalipi contributors
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.
package/LICENSES.md ADDED
@@ -0,0 +1,15 @@
1
+ # License scope
2
+
3
+ - **Original Nepalipi SDK code:** [MIT](LICENSE).
4
+ - **Aksharantar-derived model tables:** See the
5
+ [pinned AI4Bharat dataset card](https://huggingface.co/datasets/ai4bharat/Aksharantar/blob/e418c1fc928d9f5393af33268472cf20c1891be8/README.md)
6
+ for terms and limitations that vary by part of the data.
7
+ - **Native spelling dictionary and derived lexicon:** Madan Puraskar
8
+ Pustakalaya Nepali SpellChecking Dictionary 1.1, distributed under LGPL 2.1.
9
+ The original dictionary, contributor notice, license text, and provenance
10
+ are included in `dictionary-source/` beside the generated lexicon.
11
+
12
+ See the [model and dictionary notice](https://github.com/razaanstha/nepalipi/blob/main/src/engine/MODEL-NOTICE.md)
13
+ for credits, source links and a description of the changes. The notice is
14
+ available at `src/engine/MODEL-NOTICE.md` in the repository and
15
+ `dist/engine/MODEL-NOTICE.md` in the npm package.
package/README.md CHANGED
@@ -1,3 +1,86 @@
1
- # Temporary Holding Version
1
+ # Nepalipi
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Browser SDK for typing Nepali with Roman letters. The transliteration engine,
4
+ model data, and per-field spelling hints run on the device.
5
+
6
+ ## Credits
7
+
8
+ **AI4Bharat Aksharantar** provided the Nepali training data used to build
9
+ Nepalipi’s transliteration model. Thanks to the AI4Bharat dataset team,
10
+ the annotators, and paper authors Yash Madhani, Sushane Parthan, Priyanka
11
+ Bedekar, Gokul NC, Ruchi Khapra, Anoop Kunchukuttan, Pratyush Kumar, and
12
+ Mitesh M. Khapra.
13
+ [Pinned dataset card](https://huggingface.co/datasets/ai4bharat/Aksharantar/blob/e418c1fc928d9f5393af33268472cf20c1891be8/README.md) ·
14
+ [Aksharantar paper](https://aclanthology.org/2023.findings-emnlp.4/)
15
+
16
+ **Madan Puraskar Pustakalaya** compiled the Nepali SpellChecking Dictionary
17
+ that supplies Nepalipi’s native spelling lexicon. Thanks to Amar Gurung,
18
+ Bal Krishna Bal, Basanta Karki, Basanta Shrestha, Ishwor Sharma, Laxmi
19
+ Prasad Khatiwada, Paras Pradhan, Prajol Shrestha, and Subir Pradhanang.
20
+ The original notice also thanks NUCES in Pakistan and IDRC Canada’s Pan
21
+ Asia Networking program.
22
+ [Pinned LibreOffice source](https://github.com/LibreOffice/dictionaries/tree/c0865a85eb67dc377f2cf8e8fe2c4fa9e53f0dcb/ne_NP) ·
23
+ [Original contributor notice](https://github.com/LibreOffice/dictionaries/blob/c0865a85eb67dc377f2cf8e8fe2c4fa9e53f0dcb/ne_NP/README_ne_NP.txt)
24
+
25
+ See [license scope and bundled notices](LICENSES.md).
26
+
27
+ ## Install
28
+
29
+ ```sh
30
+ npm install nepalipi
31
+ ```
32
+
33
+ ## Browser integration
34
+
35
+ Attach the SDK to a native text or search input or textarea. The example below
36
+ assumes a `<textarea id="message"></textarea>` in the page and an npm-aware
37
+ bundler. To self-host without a bundler, serve the entire `dist/` directory and
38
+ import `./dist/index.js` from your browser module.
39
+
40
+ ```js
41
+ import { NepaliPi } from 'nepalipi';
42
+
43
+ const field = NepaliPi('#message', {
44
+ hints: { pani: ['पानी', 'पनि'] },
45
+ });
46
+ const unsubscribe = field.subscribe(state => {
47
+ // Use state.suggestions to render your own list.
48
+ });
49
+
50
+ // Call field.choose(suggestion.id) when the user selects an option.
51
+ // Call unsubscribe() and field.destroy() when the field is removed.
52
+ ```
53
+
54
+ The controller handles shortcuts and text replacement. The app owns popup
55
+ markup and styles. Use `field.getInputAttributes()`, `field.getListAttributes()`
56
+ and `field.getOptionAttributes(id)` for accessible rendering. The ES module
57
+ also exposes `window.NepaliPi` in the browser.
58
+
59
+ ## Module formats
60
+
61
+ JavaScript and TypeScript can use `import { NepaliPi } from 'nepalipi'`. The
62
+ default export is also available. For CommonJS, use:
63
+
64
+ ```js
65
+ const { NepaliPi } = require('nepalipi');
66
+ ```
67
+
68
+ For a TypeScript CommonJS file (`.cts`), use `import sdk = require('nepalipi')`
69
+ to get the same typed interface. Both formats share one runtime. CommonJS
70
+ uses Node’s synchronous ESM support and requires Node 22.12 or newer on the 22
71
+ line, or Node 23 and newer. A browser DOM is required to attach a field. Importing
72
+ the module alone is safe in Node or during server-side rendering.
73
+
74
+ ## Repository layout
75
+
76
+ - `src/index.ts` exposes the browser SDK.
77
+ - `src/browser/` contains field handling, spelling hints, and caret positioning.
78
+ - `src/engine/` contains ranking rules, model data, and data attribution.
79
+ - `scripts/build-native-lexicon.py` regenerates the bundled dictionary module.
80
+
81
+ ## Release check
82
+
83
+ Run `npm ci`, then `npm run typecheck` and `npm pack --dry-run`. Packing
84
+ builds the JavaScript and TypeScript declarations into `dist/`. Build a test tarball with
85
+ `npm pack`, install it in a separate project, and verify the package export and
86
+ browser input before publishing.
@@ -0,0 +1,14 @@
1
+ import type { InputField } from './types.js';
2
+ export declare function getCaretRect(element: InputField): {
3
+ left: number;
4
+ top: number;
5
+ bottom: number;
6
+ height: number;
7
+ };
8
+ export declare function attachCaretPopup(element: InputField, popup: HTMLElement, { gap, padding }?: {
9
+ gap?: number | undefined;
10
+ padding?: number | undefined;
11
+ }): {
12
+ update: () => void;
13
+ destroy(): void;
14
+ };
@@ -0,0 +1,158 @@
1
+ // Optional consumer-side geometry. The headless controller still owns no UI.
2
+ const textProperties = [
3
+ 'direction', 'fontFamily', 'fontSize', 'fontStyle', 'fontVariant', 'fontWeight',
4
+ 'fontStretch', 'fontKerning', 'fontFeatureSettings', 'fontVariationSettings',
5
+ 'lineHeight', 'letterSpacing', 'wordSpacing', 'textAlign', 'textIndent',
6
+ 'textTransform', 'tabSize', 'paddingTop', 'paddingRight', 'paddingBottom',
7
+ 'paddingLeft', 'borderTopWidth', 'borderRightWidth', 'borderBottomWidth',
8
+ 'borderLeftWidth', 'wordBreak', 'overflowWrap',
9
+ ];
10
+ function createMirror(element) {
11
+ const mirror = element.ownerDocument.createElement('div');
12
+ mirror.setAttribute('aria-hidden', 'true');
13
+ mirror.setAttribute('data-nepalipi-caret-mirror', '');
14
+ mirror.inert = true;
15
+ element.ownerDocument.body.append(mirror);
16
+ return mirror;
17
+ }
18
+ function measure(element, mirror) {
19
+ const view = element.ownerDocument.defaultView;
20
+ if (!view)
21
+ throw new Error('Caret positioning requires a browser window.');
22
+ const style = view.getComputedStyle(element);
23
+ const number = (name) => parseFloat(String(style[name])) || 0;
24
+ for (const property of textProperties)
25
+ mirror.style[property] = style[property];
26
+ Object.assign(mirror.style, {
27
+ position: 'fixed', visibility: 'hidden', pointerEvents: 'none',
28
+ left: '-10000px', top: '0', margin: '0', boxSizing: 'border-box',
29
+ borderStyle: 'solid', height: 'auto', minHeight: '0', maxHeight: 'none',
30
+ width: `${element.clientWidth + number('borderLeftWidth') + number('borderRightWidth')}px`,
31
+ whiteSpace: element instanceof view.HTMLTextAreaElement && element.wrap !== 'off' ? 'pre-wrap' : 'pre',
32
+ });
33
+ const position = element.selectionStart ?? 0;
34
+ mirror.textContent = element.value.slice(0, position);
35
+ const marker = element.ownerDocument.createElement('span');
36
+ marker.textContent = element.value.slice(position) || '\u200b';
37
+ mirror.append(marker);
38
+ const box = element.getBoundingClientRect();
39
+ const mirrorBox = mirror.getBoundingClientRect();
40
+ const markerBox = marker.getClientRects()[0];
41
+ const height = number('lineHeight') || number('fontSize') * 1.2;
42
+ const left = box.left + markerBox.left - mirrorBox.left - element.scrollLeft;
43
+ const top = box.top + markerBox.top - mirrorBox.top - element.scrollTop;
44
+ return { left, top, bottom: top + height, height };
45
+ }
46
+ export function getCaretRect(element) {
47
+ const mirror = createMirror(element);
48
+ try {
49
+ return measure(element, mirror);
50
+ }
51
+ finally {
52
+ mirror.remove();
53
+ }
54
+ }
55
+ export function attachCaretPopup(element, popup, { gap = 6, padding = 8 } = {}) {
56
+ const doc = element.ownerDocument;
57
+ if (!doc.defaultView)
58
+ throw new Error('Caret positioning requires a browser window.');
59
+ const view = doc.defaultView;
60
+ const events = new view.AbortController();
61
+ const mirror = createMirror(element);
62
+ const parent = popup.parentNode, sibling = popup.nextSibling;
63
+ const properties = ['position', 'left', 'top', 'right', 'bottom', 'margin', 'maxWidth', 'maxHeight', 'visibility'];
64
+ const original = Object.fromEntries(properties.map(name => [name, popup.style[name]]));
65
+ let frame = 0, destroyed = false, revealActive = false;
66
+ // A body-level popup escapes the textarea and ordinary overflow containers.
67
+ popup.classList.add('caret-popup');
68
+ Object.assign(popup.style, { position: 'fixed', right: 'auto', bottom: 'auto', margin: '0', visibility: 'hidden' });
69
+ doc.body.append(popup);
70
+ function position() {
71
+ frame = 0;
72
+ if (destroyed || popup.hidden)
73
+ return;
74
+ const caret = measure(element, mirror);
75
+ const box = element.getBoundingClientRect();
76
+ const viewport = view.visualViewport;
77
+ const left = (viewport?.offsetLeft ?? 0) + padding;
78
+ const top = (viewport?.offsetTop ?? 0) + padding;
79
+ const right = left + (viewport?.width ?? doc.documentElement.clientWidth) - padding * 2;
80
+ const bottom = top + (viewport?.height ?? view.innerHeight) - padding * 2;
81
+ const clipped = doc.activeElement !== element ||
82
+ caret.left < box.left + element.clientLeft - 1 ||
83
+ caret.left > box.left + element.clientLeft + element.clientWidth + 1 ||
84
+ caret.bottom <= box.top + element.clientTop ||
85
+ caret.top >= box.top + element.clientTop + element.clientHeight ||
86
+ caret.bottom <= top || caret.top >= bottom || caret.left < left || caret.left > right;
87
+ popup.style.visibility = clipped ? 'hidden' : 'visible';
88
+ if (clipped)
89
+ return;
90
+ popup.style.maxWidth = `${Math.max(0, right - left)}px`;
91
+ // Measure natural height before choosing a side; previous clamps must not
92
+ // make an above/below choice oscillate on subsequent observer callbacks.
93
+ const scrollTop = popup.scrollTop;
94
+ popup.style.maxHeight = 'none';
95
+ const naturalHeight = popup.getBoundingClientRect().height;
96
+ const below = Math.max(0, bottom - caret.bottom - gap);
97
+ const above = Math.max(0, caret.top - top - gap);
98
+ const placement = naturalHeight <= below || below >= above ? 'bottom' : 'top';
99
+ popup.style.maxHeight = `${placement === 'bottom' ? below : above}px`;
100
+ // Expanding to measure natural height can clamp scrollTop back to zero.
101
+ popup.scrollTop = scrollTop;
102
+ const size = popup.getBoundingClientRect();
103
+ popup.style.left = `${Math.max(left, Math.min(caret.left, right - size.width))}px`;
104
+ popup.style.top = `${placement === 'bottom' ? caret.bottom + gap : caret.top - gap - size.height}px`;
105
+ popup.dataset.placement = placement;
106
+ const active = popup.querySelector('[aria-selected="true"]');
107
+ if (revealActive && active) {
108
+ const option = active.getBoundingClientRect(), bounds = popup.getBoundingClientRect();
109
+ if (option.top < bounds.top)
110
+ popup.scrollTop -= bounds.top - option.top;
111
+ else if (option.bottom > bounds.bottom)
112
+ popup.scrollTop += option.bottom - bounds.bottom;
113
+ }
114
+ revealActive = false;
115
+ }
116
+ function schedule() {
117
+ if (!destroyed && !frame)
118
+ frame = view.requestAnimationFrame(position);
119
+ }
120
+ function update() {
121
+ revealActive = true;
122
+ schedule();
123
+ }
124
+ const on = (target, name, capture = false) => target?.addEventListener(name, schedule, { capture, signal: events.signal });
125
+ // Scrolling the popup must not force it back to the highlighted first row.
126
+ // Explicit renders can reveal the active row; geometry events leave the
127
+ // user's manual scroll position intact.
128
+ on(element, 'scroll');
129
+ on(doc, 'selectionchange');
130
+ on(view, 'scroll', true);
131
+ on(view, 'resize');
132
+ on(view.visualViewport, 'scroll');
133
+ on(view.visualViewport, 'resize');
134
+ on(doc.fonts, 'loadingdone');
135
+ const resize = new view.ResizeObserver(schedule);
136
+ resize.observe(element);
137
+ resize.observe(popup);
138
+ doc.fonts.ready.then(schedule);
139
+ return {
140
+ update,
141
+ destroy() {
142
+ destroyed = true;
143
+ if (frame)
144
+ view.cancelAnimationFrame(frame);
145
+ events.abort();
146
+ resize.disconnect();
147
+ mirror.remove();
148
+ popup.classList.remove('caret-popup');
149
+ delete popup.dataset.placement;
150
+ for (const property of properties)
151
+ popup.style[property] = original[property];
152
+ if (parent)
153
+ parent.insertBefore(popup, sibling?.parentNode === parent ? sibling : null);
154
+ else
155
+ popup.remove();
156
+ },
157
+ };
158
+ }
@@ -0,0 +1,37 @@
1
+ import type { SpellingHints, SuggestionState } from './types.js';
2
+ interface FieldOptions {
3
+ enabled?: boolean;
4
+ numberShortcuts?: boolean;
5
+ hints?: SpellingHints;
6
+ }
7
+ type StateListener = (state: SuggestionState) => void;
8
+ export declare function attachNepalipi(target: Element, { numberShortcuts, hints }?: FieldOptions): {
9
+ subscribe(listener: StateListener): () => void;
10
+ getState(): SuggestionState;
11
+ open(): boolean;
12
+ choose: (candidateId?: string | null, separator?: string, explicit?: boolean) => boolean;
13
+ highlight(candidateId: string): void;
14
+ dismiss: () => void;
15
+ update(options: FieldOptions): void;
16
+ getInputAttributes(): {
17
+ 'aria-autocomplete': string;
18
+ 'aria-controls': string;
19
+ 'aria-activedescendant': string | null;
20
+ role?: string | undefined;
21
+ 'aria-expanded'?: string | undefined;
22
+ };
23
+ getListAttributes(): {
24
+ id: string;
25
+ role: string;
26
+ 'aria-label': string;
27
+ };
28
+ getOptionAttributes(candidateId: string): {
29
+ id: string;
30
+ role: string;
31
+ 'aria-selected': string;
32
+ tabindex: string;
33
+ 'aria-keyshortcuts': string | null;
34
+ };
35
+ destroy(): void;
36
+ };
37
+ export {};