@willwade/aac-processors 0.0.13 → 0.0.14
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 +4 -5
- package/dist/core/baseProcessor.d.ts +41 -0
- package/dist/core/baseProcessor.js +41 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/processors/gridset/symbolAlignment.d.ts +125 -0
- package/dist/processors/gridset/symbolAlignment.js +283 -0
- package/dist/processors/gridsetProcessor.d.ts +26 -0
- package/dist/processors/gridsetProcessor.js +121 -5
- package/dist/processors/obfProcessor.d.ts +26 -0
- package/dist/processors/obfProcessor.js +82 -1
- package/dist/processors/snapProcessor.js +1 -1
- package/dist/processors/touchchatProcessor.d.ts +26 -0
- package/dist/processors/touchchatProcessor.js +82 -1
- package/dist/utilities/translation/translationProcessor.d.ts +119 -0
- package/dist/utilities/translation/translationProcessor.js +204 -0
- package/package.json +1 -1
- /package/dist/{optional → utilities}/analytics/history.d.ts +0 -0
- /package/dist/{optional → utilities}/analytics/history.js +0 -0
- /package/dist/{optional → utilities}/analytics/index.d.ts +0 -0
- /package/dist/{optional → utilities}/analytics/index.js +0 -0
- /package/dist/{optional → utilities}/analytics/metrics/comparison.d.ts +0 -0
- /package/dist/{optional → utilities}/analytics/metrics/comparison.js +0 -0
- /package/dist/{optional → utilities}/analytics/metrics/core.d.ts +0 -0
- /package/dist/{optional → utilities}/analytics/metrics/core.js +0 -0
- /package/dist/{optional → utilities}/analytics/metrics/effort.d.ts +0 -0
- /package/dist/{optional → utilities}/analytics/metrics/effort.js +0 -0
- /package/dist/{optional → utilities}/analytics/metrics/index.d.ts +0 -0
- /package/dist/{optional → utilities}/analytics/metrics/index.js +0 -0
- /package/dist/{optional → utilities}/analytics/metrics/obl-types.d.ts +0 -0
- /package/dist/{optional → utilities}/analytics/metrics/obl-types.js +0 -0
- /package/dist/{optional → utilities}/analytics/metrics/obl.d.ts +0 -0
- /package/dist/{optional → utilities}/analytics/metrics/obl.js +0 -0
- /package/dist/{optional → utilities}/analytics/metrics/sentence.d.ts +0 -0
- /package/dist/{optional → utilities}/analytics/metrics/sentence.js +0 -0
- /package/dist/{optional → utilities}/analytics/metrics/types.d.ts +0 -0
- /package/dist/{optional → utilities}/analytics/metrics/types.js +0 -0
- /package/dist/{optional → utilities}/analytics/metrics/vocabulary.d.ts +0 -0
- /package/dist/{optional → utilities}/analytics/metrics/vocabulary.js +0 -0
- /package/dist/{optional → utilities}/analytics/reference/index.d.ts +0 -0
- /package/dist/{optional → utilities}/analytics/reference/index.js +0 -0
- /package/dist/{optional → utilities}/analytics/utils/idGenerator.d.ts +0 -0
- /package/dist/{optional → utilities}/analytics/utils/idGenerator.js +0 -0
- /package/dist/{optional → utilities}/symbolTools.d.ts +0 -0
- /package/dist/{optional → utilities}/symbolTools.js +0 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* LLM-Based Translation with Symbol Preservation
|
|
4
|
+
*
|
|
5
|
+
* This module provides utilities for translating AAC files while preserving
|
|
6
|
+
* symbol-to-word associations across different formats (gridset, OBF, Snap, etc.).
|
|
7
|
+
*
|
|
8
|
+
* The key insight: Different AAC formats have different internal structures,
|
|
9
|
+
* but they all share common concepts:
|
|
10
|
+
* - Buttons with labels and messages
|
|
11
|
+
* - Symbols attached to specific words
|
|
12
|
+
* - Need to preserve symbol positions during translation
|
|
13
|
+
*
|
|
14
|
+
* This module provides a format-agnostic way to:
|
|
15
|
+
* 1. Extract symbol information for LLM processing
|
|
16
|
+
* 2. Apply LLM translations with preserved symbols
|
|
17
|
+
*
|
|
18
|
+
* Usage:
|
|
19
|
+
* 1. Processor extracts buttons and calls extractSymbolsForLLM()
|
|
20
|
+
* 2. LLM translates and returns aligned symbols
|
|
21
|
+
* 3. Processor calls processLLMTranslations() to apply results
|
|
22
|
+
*/
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.normalizeButtonForTranslation = normalizeButtonForTranslation;
|
|
25
|
+
exports.extractSymbolsFromButton = extractSymbolsFromButton;
|
|
26
|
+
exports.extractAllButtonsForTranslation = extractAllButtonsForTranslation;
|
|
27
|
+
exports.createTranslationPrompt = createTranslationPrompt;
|
|
28
|
+
exports.validateTranslationResults = validateTranslationResults;
|
|
29
|
+
/**
|
|
30
|
+
* Extract symbols from a button for LLM-based translation.
|
|
31
|
+
*
|
|
32
|
+
* This is a format-agnostic helper that processors can use to normalize
|
|
33
|
+
* their button data into a common format for LLM processing.
|
|
34
|
+
*
|
|
35
|
+
* @param buttonId - Unique identifier for the button
|
|
36
|
+
* @param label - Button label text
|
|
37
|
+
* @param message - Button message/speak text
|
|
38
|
+
* @param symbols - Array of symbols from the button
|
|
39
|
+
* @param context - Optional page context
|
|
40
|
+
* @returns Normalized button data for translation
|
|
41
|
+
*/
|
|
42
|
+
function normalizeButtonForTranslation(buttonId, label, message, symbols, context) {
|
|
43
|
+
return {
|
|
44
|
+
buttonId,
|
|
45
|
+
label,
|
|
46
|
+
message,
|
|
47
|
+
textToTranslate: message || label, // Translate message if present, otherwise label
|
|
48
|
+
symbols,
|
|
49
|
+
...context,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Extract symbols from various button formats.
|
|
54
|
+
*
|
|
55
|
+
* This helper handles different ways symbols might be stored in button data:
|
|
56
|
+
* - semanticAction.richText.symbols (gridset format)
|
|
57
|
+
* - symbolLibrary + symbolPath fields
|
|
58
|
+
* - image field with [library]path format
|
|
59
|
+
*
|
|
60
|
+
* @param button - Button object from any AAC format
|
|
61
|
+
* @returns Array of symbol info, or undefined if no symbols
|
|
62
|
+
*/
|
|
63
|
+
function extractSymbolsFromButton(button) {
|
|
64
|
+
const symbols = [];
|
|
65
|
+
// Method 1: Check for semanticAction.richText.symbols (gridset format)
|
|
66
|
+
if (button.semanticAction?.richText?.symbols) {
|
|
67
|
+
const richTextSymbols = button.semanticAction.richText.symbols;
|
|
68
|
+
if (Array.isArray(richTextSymbols) && richTextSymbols.length > 0) {
|
|
69
|
+
symbols.push(...richTextSymbols);
|
|
70
|
+
return symbols;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// Determine the text to attach symbol to
|
|
74
|
+
const text = button.label || button.message || '';
|
|
75
|
+
if (!text) {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
// Method 2: Check for symbolLibrary + symbolPath fields
|
|
79
|
+
if (button.symbolLibrary && button.symbolPath) {
|
|
80
|
+
symbols.push({
|
|
81
|
+
text,
|
|
82
|
+
image: `[${button.symbolLibrary}]${button.symbolPath}`,
|
|
83
|
+
symbolLibrary: button.symbolLibrary,
|
|
84
|
+
symbolPath: button.symbolPath,
|
|
85
|
+
});
|
|
86
|
+
return symbols;
|
|
87
|
+
}
|
|
88
|
+
// Method 3: Check if image field contains a symbol reference
|
|
89
|
+
if (button.image && typeof button.image === 'string' && button.image.startsWith('[')) {
|
|
90
|
+
symbols.push({
|
|
91
|
+
text,
|
|
92
|
+
image: button.image,
|
|
93
|
+
});
|
|
94
|
+
return symbols;
|
|
95
|
+
}
|
|
96
|
+
// No symbols found
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Extract all buttons from a file for LLM translation.
|
|
101
|
+
*
|
|
102
|
+
* This is a convenience method that processors can use to extract all
|
|
103
|
+
* translatable buttons with their symbols in a format-agnostic way.
|
|
104
|
+
*
|
|
105
|
+
* @param buttons - Array of button objects from any AAC format
|
|
106
|
+
* @param contextFn - Optional function to provide page context for each button
|
|
107
|
+
* @returns Array of normalized button data ready for LLM translation
|
|
108
|
+
*/
|
|
109
|
+
function extractAllButtonsForTranslation(buttons, contextFn) {
|
|
110
|
+
const results = [];
|
|
111
|
+
for (const button of buttons) {
|
|
112
|
+
if (!button)
|
|
113
|
+
continue;
|
|
114
|
+
const buttonId = (button.id || button.buttonId || `button_${results.length}`);
|
|
115
|
+
const label = (button.label || '');
|
|
116
|
+
const message = (button.message || '');
|
|
117
|
+
const symbols = extractSymbolsFromButton(button);
|
|
118
|
+
// Only include buttons that have text to translate
|
|
119
|
+
if (!label && !message)
|
|
120
|
+
continue;
|
|
121
|
+
const context = contextFn ? contextFn(button) : undefined;
|
|
122
|
+
results.push(normalizeButtonForTranslation(buttonId, label, message, symbols || [], context));
|
|
123
|
+
}
|
|
124
|
+
return results;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Create a prompt for LLM translation with symbol preservation.
|
|
128
|
+
*
|
|
129
|
+
* This generates a structured prompt that instructs the LLM to translate
|
|
130
|
+
* while preserving symbol-to-word associations.
|
|
131
|
+
*
|
|
132
|
+
* @param buttons - Buttons to translate
|
|
133
|
+
* @param targetLanguage - Target language for translation
|
|
134
|
+
* @returns Prompt string for LLM
|
|
135
|
+
*/
|
|
136
|
+
function createTranslationPrompt(buttons, targetLanguage) {
|
|
137
|
+
const buttonsData = JSON.stringify(buttons, null, 2);
|
|
138
|
+
return `You are a translation assistant for AAC (Augmentative and Alternative Communication) systems.
|
|
139
|
+
|
|
140
|
+
Your task is to translate the following buttons to ${targetLanguage} while preserving symbol associations.
|
|
141
|
+
|
|
142
|
+
Each button has:
|
|
143
|
+
- label: The text shown on the button
|
|
144
|
+
- message: The text spoken when the button is activated
|
|
145
|
+
- textToTranslate: The actual text to translate (usually the message)
|
|
146
|
+
- symbols: Visual symbols attached to specific words
|
|
147
|
+
|
|
148
|
+
IMPORTANT: After translation, you MUST reattach symbols to the correct translated words based on MEANING, not position.
|
|
149
|
+
|
|
150
|
+
Example:
|
|
151
|
+
- Original: "I want apple" with apple symbol on "apple"
|
|
152
|
+
- Spanish: "Yo quiero manzana" with apple symbol on "manzana" (NOT "Yo" or "quiero")
|
|
153
|
+
- French: "Je veux une pomme" with apple symbol on "pomme"
|
|
154
|
+
|
|
155
|
+
The symbols array should contain the translated word that each symbol should be attached to.
|
|
156
|
+
|
|
157
|
+
Buttons to translate:
|
|
158
|
+
${buttonsData}
|
|
159
|
+
|
|
160
|
+
Return ONLY a JSON array with this exact structure:
|
|
161
|
+
[
|
|
162
|
+
{
|
|
163
|
+
"buttonId": "...",
|
|
164
|
+
"translatedLabel": "...",
|
|
165
|
+
"translatedMessage": "...",
|
|
166
|
+
"symbols": [
|
|
167
|
+
{"text": "translated_word", "image": "[library]path"}
|
|
168
|
+
]
|
|
169
|
+
}
|
|
170
|
+
]
|
|
171
|
+
|
|
172
|
+
Ensure all symbol image references are preserved exactly as provided.`;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Validate LLM translation results before applying.
|
|
176
|
+
*
|
|
177
|
+
* @param translations - LLM translation results
|
|
178
|
+
* @param originalButtonIds - Expected button IDs (optional, for validation)
|
|
179
|
+
* @param options - Validation options
|
|
180
|
+
* @throws Error if validation fails
|
|
181
|
+
*/
|
|
182
|
+
function validateTranslationResults(translations, originalButtonIds, options) {
|
|
183
|
+
if (!Array.isArray(translations)) {
|
|
184
|
+
throw new Error('Translation results must be an array');
|
|
185
|
+
}
|
|
186
|
+
const translatedIds = new Set(translations.map((t) => t.buttonId));
|
|
187
|
+
// Check that all original buttons have translations (unless partial is allowed)
|
|
188
|
+
if (originalButtonIds && !options?.allowPartial) {
|
|
189
|
+
for (const id of originalButtonIds) {
|
|
190
|
+
if (!translatedIds.has(id)) {
|
|
191
|
+
throw new Error(`Missing translation for button: ${id}`);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
// Check each translation has required fields
|
|
196
|
+
for (const trans of translations) {
|
|
197
|
+
if (!trans.buttonId) {
|
|
198
|
+
throw new Error('Translation missing buttonId');
|
|
199
|
+
}
|
|
200
|
+
if (!trans.translatedMessage && !trans.translatedLabel) {
|
|
201
|
+
throw new Error(`Translation for ${trans.buttonId} has no translated text`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@willwade/aac-processors",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.14",
|
|
4
4
|
"description": "A comprehensive TypeScript library for processing AAC (Augmentative and Alternative Communication) file formats with translation support",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|