@willwade/aac-processors 0.0.30 → 0.1.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 +52 -852
- package/dist/browser/core/baseProcessor.js +241 -0
- package/dist/browser/core/stringCasing.js +179 -0
- package/dist/browser/core/treeStructure.js +255 -0
- package/dist/browser/index.browser.js +73 -0
- package/dist/browser/processors/applePanelsProcessor.js +582 -0
- package/dist/browser/processors/astericsGridProcessor.js +1509 -0
- package/dist/browser/processors/dotProcessor.js +221 -0
- package/dist/browser/processors/gridset/commands.js +962 -0
- package/dist/browser/processors/gridset/crypto.js +53 -0
- package/dist/browser/processors/gridset/password.js +49 -0
- package/dist/browser/processors/gridset/pluginTypes.js +277 -0
- package/dist/browser/processors/gridset/resolver.js +137 -0
- package/dist/browser/processors/gridset/symbolAlignment.js +276 -0
- package/dist/browser/processors/gridset/symbols.js +464 -0
- package/dist/browser/processors/gridsetProcessor.js +2002 -0
- package/dist/browser/processors/obfProcessor.js +705 -0
- package/dist/browser/processors/opmlProcessor.js +274 -0
- package/dist/browser/types/aac.js +38 -0
- package/dist/browser/utilities/analytics/utils/idGenerator.js +89 -0
- package/dist/browser/utilities/translation/translationProcessor.js +200 -0
- package/dist/browser/utils/io.js +95 -0
- package/dist/browser/validation/baseValidator.js +156 -0
- package/dist/browser/validation/gridsetValidator.js +356 -0
- package/dist/browser/validation/obfValidator.js +500 -0
- package/dist/browser/validation/validationTypes.js +46 -0
- package/dist/cli/index.js +5 -5
- package/dist/core/analyze.d.ts +2 -2
- package/dist/core/analyze.js +2 -2
- package/dist/core/baseProcessor.d.ts +5 -4
- package/dist/core/baseProcessor.js +22 -27
- package/dist/core/treeStructure.d.ts +5 -5
- package/dist/core/treeStructure.js +1 -4
- package/dist/index.browser.d.ts +37 -0
- package/dist/index.browser.js +99 -0
- package/dist/index.d.ts +1 -48
- package/dist/index.js +1 -136
- package/dist/index.node.d.ts +48 -0
- package/dist/index.node.js +152 -0
- package/dist/processors/applePanelsProcessor.d.ts +5 -4
- package/dist/processors/applePanelsProcessor.js +58 -62
- package/dist/processors/astericsGridProcessor.d.ts +7 -6
- package/dist/processors/astericsGridProcessor.js +31 -42
- package/dist/processors/dotProcessor.d.ts +5 -4
- package/dist/processors/dotProcessor.js +25 -33
- package/dist/processors/excelProcessor.d.ts +4 -3
- package/dist/processors/excelProcessor.js +6 -3
- package/dist/processors/gridset/crypto.d.ts +18 -0
- package/dist/processors/gridset/crypto.js +57 -0
- package/dist/processors/gridset/helpers.d.ts +1 -1
- package/dist/processors/gridset/helpers.js +18 -8
- package/dist/processors/gridset/password.d.ts +20 -3
- package/dist/processors/gridset/password.js +29 -12
- package/dist/processors/gridset/symbols.js +63 -46
- package/dist/processors/gridset/wordlistHelpers.d.ts +3 -3
- package/dist/processors/gridset/wordlistHelpers.js +21 -20
- package/dist/processors/gridsetProcessor.d.ts +7 -12
- package/dist/processors/gridsetProcessor.js +116 -77
- package/dist/processors/obfProcessor.d.ts +9 -7
- package/dist/processors/obfProcessor.js +131 -56
- package/dist/processors/obfsetProcessor.d.ts +5 -4
- package/dist/processors/obfsetProcessor.js +10 -16
- package/dist/processors/opmlProcessor.d.ts +5 -4
- package/dist/processors/opmlProcessor.js +27 -34
- package/dist/processors/snapProcessor.d.ts +8 -7
- package/dist/processors/snapProcessor.js +15 -12
- package/dist/processors/touchchatProcessor.d.ts +8 -7
- package/dist/processors/touchchatProcessor.js +22 -17
- package/dist/types/aac.d.ts +0 -2
- package/dist/types/aac.js +2 -0
- package/dist/utils/io.d.ts +12 -0
- package/dist/utils/io.js +107 -0
- package/dist/validation/gridsetValidator.js +10 -9
- package/dist/validation/snapValidator.js +28 -35
- package/docs/BROWSER_USAGE.md +618 -0
- package/docs/PAGESET_GETTING_STARTED.md +185 -0
- package/examples/README.md +77 -0
- package/examples/browser-test-server.js +81 -0
- package/examples/browser-test.html +331 -0
- package/examples/vitedemo/QUICKSTART.md +75 -0
- package/examples/vitedemo/README.md +157 -0
- package/examples/vitedemo/index.html +531 -0
- package/examples/vitedemo/package-lock.json +1221 -0
- package/examples/vitedemo/package.json +18 -0
- package/examples/vitedemo/src/main.ts +871 -0
- package/examples/vitedemo/test-files/example.dot +14 -0
- package/examples/vitedemo/test-files/example.grd +1 -0
- package/examples/vitedemo/test-files/example.gridset +0 -0
- package/examples/vitedemo/test-files/example.obz +0 -0
- package/examples/vitedemo/test-files/example.opml +18 -0
- package/examples/vitedemo/test-files/simple.obf +53 -0
- package/examples/vitedemo/tsconfig.json +24 -0
- package/examples/vitedemo/vite.config.ts +34 -0
- package/package.json +21 -4
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Symbol Alignment for Translation
|
|
3
|
+
*
|
|
4
|
+
* Utilities to preserve symbol positions during text translation.
|
|
5
|
+
* When translating AAC gridset messages that contain symbols attached
|
|
6
|
+
* to specific words, we need to maintain the symbol-to-word associations
|
|
7
|
+
* across languages.
|
|
8
|
+
*
|
|
9
|
+
* Example:
|
|
10
|
+
* English: "I want apple juice" with apple symbol on "apple"
|
|
11
|
+
* Spanish: "Yo quiero jugo de manzana" with apple symbol on "manzana"
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Parse a message to extract text and symbol anchors.
|
|
15
|
+
*
|
|
16
|
+
* This handles various formats:
|
|
17
|
+
* 1. Plain text with no symbols
|
|
18
|
+
* 2. Rich text with embedded symbol markers (future enhancement)
|
|
19
|
+
* 3. Text where symbols are tracked separately (via richText.symbols)
|
|
20
|
+
*
|
|
21
|
+
* For now, this assumes symbols are tracked separately in the richText structure.
|
|
22
|
+
* The text itself is plain, and we need to tokenize it to find word positions.
|
|
23
|
+
*
|
|
24
|
+
* @param message - The message text (may contain words or be plain)
|
|
25
|
+
* @param richTextSymbols - Optional symbols from richText.symbols array
|
|
26
|
+
* @returns Parsed message with word positions and symbol anchors
|
|
27
|
+
*/
|
|
28
|
+
export function parseMessageWithSymbols(message, richTextSymbols) {
|
|
29
|
+
// Normalize whitespace for consistent tokenization
|
|
30
|
+
const normalizedMessage = message.trim().replace(/\s+/g, ' ');
|
|
31
|
+
// Tokenize into words, preserving punctuation
|
|
32
|
+
const words = [];
|
|
33
|
+
const wordPositions = [];
|
|
34
|
+
// Split by whitespace but track positions
|
|
35
|
+
let currentPos = 0;
|
|
36
|
+
const parts = normalizedMessage.split(/(\s+)/); // Keep delimiters
|
|
37
|
+
for (const part of parts) {
|
|
38
|
+
if (part.trim().length > 0) {
|
|
39
|
+
// This is a word
|
|
40
|
+
const startPos = currentPos;
|
|
41
|
+
const endPos = currentPos + part.length;
|
|
42
|
+
words.push(part);
|
|
43
|
+
wordPositions.push({ start: startPos, end: endPos, word: part });
|
|
44
|
+
currentPos = endPos;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
// This is whitespace
|
|
48
|
+
currentPos += part.length;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
// Extract symbol anchors from richText.symbols if provided
|
|
52
|
+
const symbols = [];
|
|
53
|
+
if (richTextSymbols && richTextSymbols.length > 0) {
|
|
54
|
+
for (const sym of richTextSymbols) {
|
|
55
|
+
// Find which word this symbol is attached to
|
|
56
|
+
const wordIndex = words.findIndex((w) => w === sym.text);
|
|
57
|
+
if (wordIndex !== -1) {
|
|
58
|
+
const pos = wordPositions[wordIndex];
|
|
59
|
+
symbols.push({
|
|
60
|
+
symbolRef: sym.image || '',
|
|
61
|
+
wordIndex,
|
|
62
|
+
originalWord: sym.text,
|
|
63
|
+
startPos: pos.start,
|
|
64
|
+
endPos: pos.end,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
// Fuzzy match - find closest word (handles case differences, punctuation)
|
|
69
|
+
const normalizedSymText = sym.text.toLowerCase().replace(/[^\w]/g, '');
|
|
70
|
+
const fuzzyIndex = words.findIndex((w) => w.toLowerCase().replace(/[^\w]/g, '') === normalizedSymText);
|
|
71
|
+
if (fuzzyIndex !== -1) {
|
|
72
|
+
const pos = wordPositions[fuzzyIndex];
|
|
73
|
+
symbols.push({
|
|
74
|
+
symbolRef: sym.image || '',
|
|
75
|
+
wordIndex: fuzzyIndex,
|
|
76
|
+
originalWord: words[fuzzyIndex],
|
|
77
|
+
startPos: pos.start,
|
|
78
|
+
endPos: pos.end,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
text: normalizedMessage,
|
|
86
|
+
words,
|
|
87
|
+
symbols,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Align words from original text to translated text.
|
|
92
|
+
*
|
|
93
|
+
* This is a simple alignment strategy that works for many cases:
|
|
94
|
+
* 1. Exact word matching (for cognates, names, numbers)
|
|
95
|
+
* 2. Position-based alignment (assumes similar word order)
|
|
96
|
+
*
|
|
97
|
+
* For more accurate alignment, you could integrate with:
|
|
98
|
+
* - Translation APIs that return alignment (e.g., Google Translate's word alignment)
|
|
99
|
+
* - Statistical machine translation alignment tools
|
|
100
|
+
* - Bilingual dictionaries
|
|
101
|
+
*
|
|
102
|
+
* @param originalWords - Words from the original text
|
|
103
|
+
* @param translatedWords - Words from the translated text
|
|
104
|
+
* @returns Alignment mapping between original and translated word indices
|
|
105
|
+
*/
|
|
106
|
+
export function alignWords(originalWords, translatedWords) {
|
|
107
|
+
const alignment = [];
|
|
108
|
+
// Strategy 1: Try to match identical words (numbers, names, cognates)
|
|
109
|
+
const matchedTranslatedIndices = new Set();
|
|
110
|
+
for (let origIdx = 0; origIdx < originalWords.length; origIdx++) {
|
|
111
|
+
const origWord = originalWords[origIdx];
|
|
112
|
+
const normalizedOrig = origWord.toLowerCase().replace(/[^\w]/g, '');
|
|
113
|
+
// Try to find this word in the translation
|
|
114
|
+
for (let transIdx = 0; transIdx < translatedWords.length; transIdx++) {
|
|
115
|
+
if (matchedTranslatedIndices.has(transIdx))
|
|
116
|
+
continue;
|
|
117
|
+
const transWord = translatedWords[transIdx];
|
|
118
|
+
const normalizedTrans = transWord.toLowerCase().replace(/[^\w]/g, '');
|
|
119
|
+
// Exact match (case-insensitive, ignoring punctuation)
|
|
120
|
+
if (normalizedOrig === normalizedTrans && normalizedOrig.length > 0) {
|
|
121
|
+
alignment.push({
|
|
122
|
+
originalWord: origWord,
|
|
123
|
+
translatedWord: transWord,
|
|
124
|
+
originalIndex: origIdx,
|
|
125
|
+
translatedIndex: transIdx,
|
|
126
|
+
});
|
|
127
|
+
matchedTranslatedIndices.add(transIdx);
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
// Strategy 2: For unmatched words, use positional alignment
|
|
133
|
+
// This is a simple fallback that assumes similar word order
|
|
134
|
+
for (let origIdx = 0; origIdx < originalWords.length; origIdx++) {
|
|
135
|
+
if (alignment.find((a) => a.originalIndex === origIdx))
|
|
136
|
+
continue; // Already matched
|
|
137
|
+
// Find the closest unmatched position in translation
|
|
138
|
+
let bestTransIdx = -1;
|
|
139
|
+
let minDistance = Infinity;
|
|
140
|
+
for (let transIdx = 0; transIdx < translatedWords.length; transIdx++) {
|
|
141
|
+
if (matchedTranslatedIndices.has(transIdx))
|
|
142
|
+
continue;
|
|
143
|
+
// Calculate relative position
|
|
144
|
+
const relativeOrigPos = origIdx / originalWords.length;
|
|
145
|
+
const relativeTransPos = transIdx / translatedWords.length;
|
|
146
|
+
const distance = Math.abs(relativeOrigPos - relativeTransPos);
|
|
147
|
+
if (distance < minDistance) {
|
|
148
|
+
minDistance = distance;
|
|
149
|
+
bestTransIdx = transIdx;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (bestTransIdx !== -1) {
|
|
153
|
+
alignment.push({
|
|
154
|
+
originalWord: originalWords[origIdx],
|
|
155
|
+
translatedWord: translatedWords[bestTransIdx],
|
|
156
|
+
originalIndex: origIdx,
|
|
157
|
+
translatedIndex: bestTransIdx,
|
|
158
|
+
});
|
|
159
|
+
matchedTranslatedIndices.add(bestTransIdx);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return alignment;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Reattach symbols to translated text based on word alignment.
|
|
166
|
+
*
|
|
167
|
+
* @param translatedText - The translated plain text
|
|
168
|
+
* @param originalParsed - The original parsed message with symbols
|
|
169
|
+
* @param alignment - Word alignment between original and translation
|
|
170
|
+
* @returns Translated text with symbols embedded (as rich text structure)
|
|
171
|
+
*/
|
|
172
|
+
export function reattachSymbols(translatedText, originalParsed, alignment) {
|
|
173
|
+
// Tokenize the translated text
|
|
174
|
+
const translatedWords = translatedText
|
|
175
|
+
.trim()
|
|
176
|
+
.replace(/\s+/g, ' ')
|
|
177
|
+
.split(/\s+/)
|
|
178
|
+
.filter((w) => w.length > 0);
|
|
179
|
+
// Create the rich text symbols array
|
|
180
|
+
const richTextSymbols = [];
|
|
181
|
+
for (const symbol of originalParsed.symbols) {
|
|
182
|
+
// Find the alignment for this word
|
|
183
|
+
const wordAlignment = alignment.find((a) => a.originalIndex === symbol.wordIndex);
|
|
184
|
+
if (wordAlignment && wordAlignment.translatedIndex < translatedWords.length) {
|
|
185
|
+
const translatedWord = translatedWords[wordAlignment.translatedIndex];
|
|
186
|
+
// Attach the symbol to the translated word
|
|
187
|
+
richTextSymbols.push({
|
|
188
|
+
text: translatedWord,
|
|
189
|
+
image: symbol.symbolRef,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
// Fallback: keep symbol on original word if no alignment found
|
|
194
|
+
richTextSymbols.push({
|
|
195
|
+
text: symbol.originalWord,
|
|
196
|
+
image: symbol.symbolRef,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return {
|
|
201
|
+
text: translatedText,
|
|
202
|
+
richTextSymbols,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Complete pipeline: translate a message while preserving symbol positions.
|
|
207
|
+
*
|
|
208
|
+
* @param originalMessage - The original message text
|
|
209
|
+
* @param translatedText - The translated text (from translation API)
|
|
210
|
+
* @param richTextSymbols - Original symbols from richText.symbols
|
|
211
|
+
* @returns Object with translated text and aligned symbols
|
|
212
|
+
*/
|
|
213
|
+
export function translateWithSymbols(originalMessage, translatedText, richTextSymbols) {
|
|
214
|
+
// Step 1: Parse original message
|
|
215
|
+
const parsedOriginal = parseMessageWithSymbols(originalMessage, richTextSymbols);
|
|
216
|
+
// If no symbols, return as-is
|
|
217
|
+
if (parsedOriginal.symbols.length === 0) {
|
|
218
|
+
return {
|
|
219
|
+
text: translatedText,
|
|
220
|
+
richTextSymbols: [],
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
// Step 2: Tokenize translated text
|
|
224
|
+
const translatedWords = translatedText
|
|
225
|
+
.trim()
|
|
226
|
+
.replace(/\s+/g, ' ')
|
|
227
|
+
.split(/\s+/)
|
|
228
|
+
.filter((w) => w.length > 0);
|
|
229
|
+
// Step 3: Align words
|
|
230
|
+
const alignment = alignWords(parsedOriginal.words, translatedWords);
|
|
231
|
+
// Step 4: Reattach symbols
|
|
232
|
+
const result = reattachSymbols(translatedText, parsedOriginal, alignment);
|
|
233
|
+
return result;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Extract symbols from a button for use during translation.
|
|
237
|
+
*
|
|
238
|
+
* This helper extracts symbols from either:
|
|
239
|
+
* - button.semanticAction.richText.symbols
|
|
240
|
+
* - button.image (if it's a symbol library reference)
|
|
241
|
+
*
|
|
242
|
+
* @param button - The AAC button
|
|
243
|
+
* @returns Array of symbol attachments
|
|
244
|
+
*/
|
|
245
|
+
export function extractSymbolsFromButton(button) {
|
|
246
|
+
// First check richText structure
|
|
247
|
+
if (button.semanticAction?.richText?.symbols) {
|
|
248
|
+
return button.semanticAction.richText.symbols;
|
|
249
|
+
}
|
|
250
|
+
// Check if button has a symbol library reference as image
|
|
251
|
+
if (button.symbolLibrary && button.symbolPath) {
|
|
252
|
+
// Create a symbol attachment for the label/message
|
|
253
|
+
const text = button.label || button.message || '';
|
|
254
|
+
if (text) {
|
|
255
|
+
return [
|
|
256
|
+
{
|
|
257
|
+
text,
|
|
258
|
+
image: `[${button.symbolLibrary}]${button.symbolPath}`,
|
|
259
|
+
},
|
|
260
|
+
];
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
// Check if image field contains a symbol reference
|
|
264
|
+
if (button.image && button.image.startsWith('[')) {
|
|
265
|
+
const text = button.label || button.message || '';
|
|
266
|
+
if (text) {
|
|
267
|
+
return [
|
|
268
|
+
{
|
|
269
|
+
text,
|
|
270
|
+
image: button.image,
|
|
271
|
+
},
|
|
272
|
+
];
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return undefined;
|
|
276
|
+
}
|