@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
package/README.md
CHANGED
|
@@ -199,6 +199,7 @@ console.log("Translation complete!");
|
|
|
199
199
|
The library includes an optional high-performance analytics engine for evaluating AAC board sets based on the **AAC Effort Algorithm (v0.2)**.
|
|
200
200
|
|
|
201
201
|
#### **Key Metrics Features**
|
|
202
|
+
|
|
202
203
|
- **Effort Scores**: Calculate the physical/cognitive cost of any word (Distance, Field Size, Motor Planning).
|
|
203
204
|
- **Vocabulary Coverage**: Compare board sets against core vocabulary lists (e.g., Anderson & Bitner).
|
|
204
205
|
- **Sentence Analysis**: Measure the effort required to construct common test sentences.
|
|
@@ -207,10 +208,10 @@ The library includes an optional high-performance analytics engine for evaluatin
|
|
|
207
208
|
For detailed documentation, see the **[AAC Metrics Guide](./src/optional/analytics/docs/AAC_METRICS_GUIDE.md)** and **[Vocabulary Analysis Guide](./src/optional/analytics/docs/VOCABULARY_ANALYSIS_GUIDE.md)**.
|
|
208
209
|
|
|
209
210
|
```typescript
|
|
210
|
-
import { ObfsetProcessor, Analytics } from
|
|
211
|
+
import { ObfsetProcessor, Analytics } from "@willwade/aac-processors";
|
|
211
212
|
|
|
212
213
|
const processor = new ObfsetProcessor();
|
|
213
|
-
const tree = processor.loadIntoTree(
|
|
214
|
+
const tree = processor.loadIntoTree("my_pageset.obfset");
|
|
214
215
|
|
|
215
216
|
// Run clinical effort analysis
|
|
216
217
|
const result = new Analytics.MetricsCalculator().analyze(tree);
|
|
@@ -738,6 +739,7 @@ npm run coverage:report
|
|
|
738
739
|
### 🛠️ Utility Scripts
|
|
739
740
|
|
|
740
741
|
A wide range of utility scripts for batch processing, audio generation, and advanced analysis are available in the **[scripts/](./scripts/README.md)** directory. These include:
|
|
742
|
+
|
|
741
743
|
- **Analysis**: Pageset reporting and vocabulary extraction.
|
|
742
744
|
- **Audio**: Automated TTS generation and audio-enhanced pageset creation.
|
|
743
745
|
- **Conversion**: TSV-to-Gridset and other format-shifting tools.
|
|
@@ -845,13 +847,10 @@ Inspired by the Python AACProcessors project
|
|
|
845
847
|
|
|
846
848
|
- [ ] **Adaptive Metrics** - Expand scanning analysis to include dwell times and more complex switch logic configurations.
|
|
847
849
|
|
|
848
|
-
|
|
849
850
|
### Low Priority
|
|
850
851
|
|
|
851
|
-
|
|
852
852
|
- [ ] **Batch processing CLI** - Process multiple files/directories in parallel.
|
|
853
853
|
|
|
854
|
-
|
|
855
854
|
### Contributing
|
|
856
855
|
|
|
857
856
|
Want to help with any of these items? See our [Contributing Guidelines](#-contributing) and pick an issue that interests you!
|
|
@@ -1,3 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base Processor for AAC File Formats
|
|
3
|
+
*
|
|
4
|
+
* This module provides base functionality for processing AAC (Augmentative and Alternative
|
|
5
|
+
* Communication) files across various formats (gridset, OBF, Snap, TouchChat, etc.).
|
|
6
|
+
*
|
|
7
|
+
* ## LLM-Based Translation with Symbol Preservation
|
|
8
|
+
*
|
|
9
|
+
* All processor formats support LLM-based translation that preserves symbol-to-word
|
|
10
|
+
* associations across languages. This is critical for AAC systems where visual symbols
|
|
11
|
+
* are attached to specific words.
|
|
12
|
+
*
|
|
13
|
+
* ### Usage Example:
|
|
14
|
+
*
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { extractAllButtonsForTranslation, createTranslationPrompt } from '../optional/translation/translationProcessor';
|
|
17
|
+
*
|
|
18
|
+
* // 1. Extract buttons from your format
|
|
19
|
+
* const buttons = extractAllButtonsForTranslation(myFormatButtons, (button) => ({
|
|
20
|
+
* pageId: button.pageId,
|
|
21
|
+
* pageName: button.pageName
|
|
22
|
+
* }));
|
|
23
|
+
*
|
|
24
|
+
* // 2. Create prompt for LLM
|
|
25
|
+
* const prompt = createTranslationPrompt(buttons, 'Spanish');
|
|
26
|
+
*
|
|
27
|
+
* // 3. Send to LLM (Gemini, GPT, etc.) and get response
|
|
28
|
+
* const llmResponse = await callLLMAPI(prompt);
|
|
29
|
+
*
|
|
30
|
+
* // 4. Apply translations to your format
|
|
31
|
+
* processor.processLLMTranslations(filePath, llmResponse, outputPath);
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* ### Format-Specific Implementation:
|
|
35
|
+
*
|
|
36
|
+
* Each processor should implement:
|
|
37
|
+
* - `extractSymbolsForLLM()` - Uses extractAllButtonsForTranslation() utility
|
|
38
|
+
* - `processLLMTranslations()` - Applies translations using format-specific logic
|
|
39
|
+
*
|
|
40
|
+
* See `src/utilities/translation/translationProcessor.ts` for shared utilities.
|
|
41
|
+
*/
|
|
1
42
|
import { AACTree, AACButton } from './treeStructure';
|
|
2
43
|
import { StringCasing } from './stringCasing';
|
|
3
44
|
import { ValidationResult } from '../validation/validationTypes';
|
|
@@ -1,4 +1,45 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Base Processor for AAC File Formats
|
|
4
|
+
*
|
|
5
|
+
* This module provides base functionality for processing AAC (Augmentative and Alternative
|
|
6
|
+
* Communication) files across various formats (gridset, OBF, Snap, TouchChat, etc.).
|
|
7
|
+
*
|
|
8
|
+
* ## LLM-Based Translation with Symbol Preservation
|
|
9
|
+
*
|
|
10
|
+
* All processor formats support LLM-based translation that preserves symbol-to-word
|
|
11
|
+
* associations across languages. This is critical for AAC systems where visual symbols
|
|
12
|
+
* are attached to specific words.
|
|
13
|
+
*
|
|
14
|
+
* ### Usage Example:
|
|
15
|
+
*
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import { extractAllButtonsForTranslation, createTranslationPrompt } from '../optional/translation/translationProcessor';
|
|
18
|
+
*
|
|
19
|
+
* // 1. Extract buttons from your format
|
|
20
|
+
* const buttons = extractAllButtonsForTranslation(myFormatButtons, (button) => ({
|
|
21
|
+
* pageId: button.pageId,
|
|
22
|
+
* pageName: button.pageName
|
|
23
|
+
* }));
|
|
24
|
+
*
|
|
25
|
+
* // 2. Create prompt for LLM
|
|
26
|
+
* const prompt = createTranslationPrompt(buttons, 'Spanish');
|
|
27
|
+
*
|
|
28
|
+
* // 3. Send to LLM (Gemini, GPT, etc.) and get response
|
|
29
|
+
* const llmResponse = await callLLMAPI(prompt);
|
|
30
|
+
*
|
|
31
|
+
* // 4. Apply translations to your format
|
|
32
|
+
* processor.processLLMTranslations(filePath, llmResponse, outputPath);
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* ### Format-Specific Implementation:
|
|
36
|
+
*
|
|
37
|
+
* Each processor should implement:
|
|
38
|
+
* - `extractSymbolsForLLM()` - Uses extractAllButtonsForTranslation() utility
|
|
39
|
+
* - `processLLMTranslations()` - Applies translations using format-specific logic
|
|
40
|
+
*
|
|
41
|
+
* See `src/utilities/translation/translationProcessor.ts` for shared utilities.
|
|
42
|
+
*/
|
|
2
43
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
44
|
exports.BaseProcessor = void 0;
|
|
4
45
|
const treeStructure_1 = require("./treeStructure");
|
package/dist/index.d.ts
CHANGED
|
@@ -3,8 +3,8 @@ export * from './core/baseProcessor';
|
|
|
3
3
|
export * from './core/stringCasing';
|
|
4
4
|
export * from './processors';
|
|
5
5
|
export * from './validation';
|
|
6
|
-
export * as Analytics from './
|
|
7
|
-
export { collectUnifiedHistory, listGrid3Users as listHistoryGrid3Users, listSnapUsers as listHistorySnapUsers, } from './
|
|
6
|
+
export * as Analytics from './utilities/analytics';
|
|
7
|
+
export { collectUnifiedHistory, listGrid3Users as listHistoryGrid3Users, listSnapUsers as listHistorySnapUsers, } from './utilities/analytics/history';
|
|
8
8
|
import { BaseProcessor } from './core/baseProcessor';
|
|
9
9
|
/**
|
|
10
10
|
* Factory function to get the appropriate processor for a file extension
|
package/dist/index.js
CHANGED
|
@@ -36,8 +36,8 @@ __exportStar(require("./core/baseProcessor"), exports);
|
|
|
36
36
|
__exportStar(require("./core/stringCasing"), exports);
|
|
37
37
|
__exportStar(require("./processors"), exports);
|
|
38
38
|
__exportStar(require("./validation"), exports);
|
|
39
|
-
exports.Analytics = __importStar(require("./
|
|
40
|
-
var history_1 = require("./
|
|
39
|
+
exports.Analytics = __importStar(require("./utilities/analytics"));
|
|
40
|
+
var history_1 = require("./utilities/analytics/history");
|
|
41
41
|
Object.defineProperty(exports, "collectUnifiedHistory", { enumerable: true, get: function () { return history_1.collectUnifiedHistory; } });
|
|
42
42
|
Object.defineProperty(exports, "listHistoryGrid3Users", { enumerable: true, get: function () { return history_1.listGrid3Users; } });
|
|
43
43
|
Object.defineProperty(exports, "listHistorySnapUsers", { enumerable: true, get: function () { return history_1.listSnapUsers; } });
|
|
@@ -0,0 +1,125 @@
|
|
|
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
|
+
* Represents a symbol anchored to a specific word in the text
|
|
15
|
+
*/
|
|
16
|
+
export interface SymbolAnchor {
|
|
17
|
+
symbolRef: string;
|
|
18
|
+
wordIndex: number;
|
|
19
|
+
originalWord: string;
|
|
20
|
+
startPos: number;
|
|
21
|
+
endPos: number;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Parsed message with symbol anchors
|
|
25
|
+
*/
|
|
26
|
+
export interface ParsedMessage {
|
|
27
|
+
text: string;
|
|
28
|
+
words: string[];
|
|
29
|
+
symbols: SymbolAnchor[];
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Translation result with preserved symbols
|
|
33
|
+
*/
|
|
34
|
+
export interface TranslatedMessage {
|
|
35
|
+
text: string;
|
|
36
|
+
alignment: {
|
|
37
|
+
originalWord: string;
|
|
38
|
+
translatedWord: string;
|
|
39
|
+
originalIndex: number;
|
|
40
|
+
translatedIndex: number;
|
|
41
|
+
}[];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Parse a message to extract text and symbol anchors.
|
|
45
|
+
*
|
|
46
|
+
* This handles various formats:
|
|
47
|
+
* 1. Plain text with no symbols
|
|
48
|
+
* 2. Rich text with embedded symbol markers (future enhancement)
|
|
49
|
+
* 3. Text where symbols are tracked separately (via richText.symbols)
|
|
50
|
+
*
|
|
51
|
+
* For now, this assumes symbols are tracked separately in the richText structure.
|
|
52
|
+
* The text itself is plain, and we need to tokenize it to find word positions.
|
|
53
|
+
*
|
|
54
|
+
* @param message - The message text (may contain words or be plain)
|
|
55
|
+
* @param richTextSymbols - Optional symbols from richText.symbols array
|
|
56
|
+
* @returns Parsed message with word positions and symbol anchors
|
|
57
|
+
*/
|
|
58
|
+
export declare function parseMessageWithSymbols(message: string, richTextSymbols?: Array<{
|
|
59
|
+
text: string;
|
|
60
|
+
image?: string;
|
|
61
|
+
}>): ParsedMessage;
|
|
62
|
+
/**
|
|
63
|
+
* Align words from original text to translated text.
|
|
64
|
+
*
|
|
65
|
+
* This is a simple alignment strategy that works for many cases:
|
|
66
|
+
* 1. Exact word matching (for cognates, names, numbers)
|
|
67
|
+
* 2. Position-based alignment (assumes similar word order)
|
|
68
|
+
*
|
|
69
|
+
* For more accurate alignment, you could integrate with:
|
|
70
|
+
* - Translation APIs that return alignment (e.g., Google Translate's word alignment)
|
|
71
|
+
* - Statistical machine translation alignment tools
|
|
72
|
+
* - Bilingual dictionaries
|
|
73
|
+
*
|
|
74
|
+
* @param originalWords - Words from the original text
|
|
75
|
+
* @param translatedWords - Words from the translated text
|
|
76
|
+
* @returns Alignment mapping between original and translated word indices
|
|
77
|
+
*/
|
|
78
|
+
export declare function alignWords(originalWords: string[], translatedWords: string[]): TranslatedMessage['alignment'];
|
|
79
|
+
/**
|
|
80
|
+
* Reattach symbols to translated text based on word alignment.
|
|
81
|
+
*
|
|
82
|
+
* @param translatedText - The translated plain text
|
|
83
|
+
* @param originalParsed - The original parsed message with symbols
|
|
84
|
+
* @param alignment - Word alignment between original and translation
|
|
85
|
+
* @returns Translated text with symbols embedded (as rich text structure)
|
|
86
|
+
*/
|
|
87
|
+
export declare function reattachSymbols(translatedText: string, originalParsed: ParsedMessage, alignment: TranslatedMessage['alignment']): {
|
|
88
|
+
text: string;
|
|
89
|
+
richTextSymbols: Array<{
|
|
90
|
+
text: string;
|
|
91
|
+
image?: string;
|
|
92
|
+
}>;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Complete pipeline: translate a message while preserving symbol positions.
|
|
96
|
+
*
|
|
97
|
+
* @param originalMessage - The original message text
|
|
98
|
+
* @param translatedText - The translated text (from translation API)
|
|
99
|
+
* @param richTextSymbols - Original symbols from richText.symbols
|
|
100
|
+
* @returns Object with translated text and aligned symbols
|
|
101
|
+
*/
|
|
102
|
+
export declare function translateWithSymbols(originalMessage: string, translatedText: string, richTextSymbols?: Array<{
|
|
103
|
+
text: string;
|
|
104
|
+
image?: string;
|
|
105
|
+
}>): {
|
|
106
|
+
text: string;
|
|
107
|
+
richTextSymbols: Array<{
|
|
108
|
+
text: string;
|
|
109
|
+
image?: string;
|
|
110
|
+
}>;
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* Extract symbols from a button for use during translation.
|
|
114
|
+
*
|
|
115
|
+
* This helper extracts symbols from either:
|
|
116
|
+
* - button.semanticAction.richText.symbols
|
|
117
|
+
* - button.image (if it's a symbol library reference)
|
|
118
|
+
*
|
|
119
|
+
* @param button - The AAC button
|
|
120
|
+
* @returns Array of symbol attachments
|
|
121
|
+
*/
|
|
122
|
+
export declare function extractSymbolsFromButton(button: any): Array<{
|
|
123
|
+
text: string;
|
|
124
|
+
image?: string;
|
|
125
|
+
}> | undefined;
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Symbol Alignment for Translation
|
|
4
|
+
*
|
|
5
|
+
* Utilities to preserve symbol positions during text translation.
|
|
6
|
+
* When translating AAC gridset messages that contain symbols attached
|
|
7
|
+
* to specific words, we need to maintain the symbol-to-word associations
|
|
8
|
+
* across languages.
|
|
9
|
+
*
|
|
10
|
+
* Example:
|
|
11
|
+
* English: "I want apple juice" with apple symbol on "apple"
|
|
12
|
+
* Spanish: "Yo quiero jugo de manzana" with apple symbol on "manzana"
|
|
13
|
+
*/
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.parseMessageWithSymbols = parseMessageWithSymbols;
|
|
16
|
+
exports.alignWords = alignWords;
|
|
17
|
+
exports.reattachSymbols = reattachSymbols;
|
|
18
|
+
exports.translateWithSymbols = translateWithSymbols;
|
|
19
|
+
exports.extractSymbolsFromButton = extractSymbolsFromButton;
|
|
20
|
+
/**
|
|
21
|
+
* Parse a message to extract text and symbol anchors.
|
|
22
|
+
*
|
|
23
|
+
* This handles various formats:
|
|
24
|
+
* 1. Plain text with no symbols
|
|
25
|
+
* 2. Rich text with embedded symbol markers (future enhancement)
|
|
26
|
+
* 3. Text where symbols are tracked separately (via richText.symbols)
|
|
27
|
+
*
|
|
28
|
+
* For now, this assumes symbols are tracked separately in the richText structure.
|
|
29
|
+
* The text itself is plain, and we need to tokenize it to find word positions.
|
|
30
|
+
*
|
|
31
|
+
* @param message - The message text (may contain words or be plain)
|
|
32
|
+
* @param richTextSymbols - Optional symbols from richText.symbols array
|
|
33
|
+
* @returns Parsed message with word positions and symbol anchors
|
|
34
|
+
*/
|
|
35
|
+
function parseMessageWithSymbols(message, richTextSymbols) {
|
|
36
|
+
// Normalize whitespace for consistent tokenization
|
|
37
|
+
const normalizedMessage = message.trim().replace(/\s+/g, ' ');
|
|
38
|
+
// Tokenize into words, preserving punctuation
|
|
39
|
+
const words = [];
|
|
40
|
+
const wordPositions = [];
|
|
41
|
+
// Split by whitespace but track positions
|
|
42
|
+
let currentPos = 0;
|
|
43
|
+
const parts = normalizedMessage.split(/(\s+)/); // Keep delimiters
|
|
44
|
+
for (const part of parts) {
|
|
45
|
+
if (part.trim().length > 0) {
|
|
46
|
+
// This is a word
|
|
47
|
+
const startPos = currentPos;
|
|
48
|
+
const endPos = currentPos + part.length;
|
|
49
|
+
words.push(part);
|
|
50
|
+
wordPositions.push({ start: startPos, end: endPos, word: part });
|
|
51
|
+
currentPos = endPos;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
// This is whitespace
|
|
55
|
+
currentPos += part.length;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
// Extract symbol anchors from richText.symbols if provided
|
|
59
|
+
const symbols = [];
|
|
60
|
+
if (richTextSymbols && richTextSymbols.length > 0) {
|
|
61
|
+
for (const sym of richTextSymbols) {
|
|
62
|
+
// Find which word this symbol is attached to
|
|
63
|
+
const wordIndex = words.findIndex((w) => w === sym.text);
|
|
64
|
+
if (wordIndex !== -1) {
|
|
65
|
+
const pos = wordPositions[wordIndex];
|
|
66
|
+
symbols.push({
|
|
67
|
+
symbolRef: sym.image || '',
|
|
68
|
+
wordIndex,
|
|
69
|
+
originalWord: sym.text,
|
|
70
|
+
startPos: pos.start,
|
|
71
|
+
endPos: pos.end,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
// Fuzzy match - find closest word (handles case differences, punctuation)
|
|
76
|
+
const normalizedSymText = sym.text.toLowerCase().replace(/[^\w]/g, '');
|
|
77
|
+
const fuzzyIndex = words.findIndex((w) => w.toLowerCase().replace(/[^\w]/g, '') === normalizedSymText);
|
|
78
|
+
if (fuzzyIndex !== -1) {
|
|
79
|
+
const pos = wordPositions[fuzzyIndex];
|
|
80
|
+
symbols.push({
|
|
81
|
+
symbolRef: sym.image || '',
|
|
82
|
+
wordIndex: fuzzyIndex,
|
|
83
|
+
originalWord: words[fuzzyIndex],
|
|
84
|
+
startPos: pos.start,
|
|
85
|
+
endPos: pos.end,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
text: normalizedMessage,
|
|
93
|
+
words,
|
|
94
|
+
symbols,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Align words from original text to translated text.
|
|
99
|
+
*
|
|
100
|
+
* This is a simple alignment strategy that works for many cases:
|
|
101
|
+
* 1. Exact word matching (for cognates, names, numbers)
|
|
102
|
+
* 2. Position-based alignment (assumes similar word order)
|
|
103
|
+
*
|
|
104
|
+
* For more accurate alignment, you could integrate with:
|
|
105
|
+
* - Translation APIs that return alignment (e.g., Google Translate's word alignment)
|
|
106
|
+
* - Statistical machine translation alignment tools
|
|
107
|
+
* - Bilingual dictionaries
|
|
108
|
+
*
|
|
109
|
+
* @param originalWords - Words from the original text
|
|
110
|
+
* @param translatedWords - Words from the translated text
|
|
111
|
+
* @returns Alignment mapping between original and translated word indices
|
|
112
|
+
*/
|
|
113
|
+
function alignWords(originalWords, translatedWords) {
|
|
114
|
+
const alignment = [];
|
|
115
|
+
// Strategy 1: Try to match identical words (numbers, names, cognates)
|
|
116
|
+
const matchedTranslatedIndices = new Set();
|
|
117
|
+
for (let origIdx = 0; origIdx < originalWords.length; origIdx++) {
|
|
118
|
+
const origWord = originalWords[origIdx];
|
|
119
|
+
const normalizedOrig = origWord.toLowerCase().replace(/[^\w]/g, '');
|
|
120
|
+
// Try to find this word in the translation
|
|
121
|
+
for (let transIdx = 0; transIdx < translatedWords.length; transIdx++) {
|
|
122
|
+
if (matchedTranslatedIndices.has(transIdx))
|
|
123
|
+
continue;
|
|
124
|
+
const transWord = translatedWords[transIdx];
|
|
125
|
+
const normalizedTrans = transWord.toLowerCase().replace(/[^\w]/g, '');
|
|
126
|
+
// Exact match (case-insensitive, ignoring punctuation)
|
|
127
|
+
if (normalizedOrig === normalizedTrans && normalizedOrig.length > 0) {
|
|
128
|
+
alignment.push({
|
|
129
|
+
originalWord: origWord,
|
|
130
|
+
translatedWord: transWord,
|
|
131
|
+
originalIndex: origIdx,
|
|
132
|
+
translatedIndex: transIdx,
|
|
133
|
+
});
|
|
134
|
+
matchedTranslatedIndices.add(transIdx);
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
// Strategy 2: For unmatched words, use positional alignment
|
|
140
|
+
// This is a simple fallback that assumes similar word order
|
|
141
|
+
for (let origIdx = 0; origIdx < originalWords.length; origIdx++) {
|
|
142
|
+
if (alignment.find((a) => a.originalIndex === origIdx))
|
|
143
|
+
continue; // Already matched
|
|
144
|
+
// Find the closest unmatched position in translation
|
|
145
|
+
let bestTransIdx = -1;
|
|
146
|
+
let minDistance = Infinity;
|
|
147
|
+
for (let transIdx = 0; transIdx < translatedWords.length; transIdx++) {
|
|
148
|
+
if (matchedTranslatedIndices.has(transIdx))
|
|
149
|
+
continue;
|
|
150
|
+
// Calculate relative position
|
|
151
|
+
const relativeOrigPos = origIdx / originalWords.length;
|
|
152
|
+
const relativeTransPos = transIdx / translatedWords.length;
|
|
153
|
+
const distance = Math.abs(relativeOrigPos - relativeTransPos);
|
|
154
|
+
if (distance < minDistance) {
|
|
155
|
+
minDistance = distance;
|
|
156
|
+
bestTransIdx = transIdx;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (bestTransIdx !== -1) {
|
|
160
|
+
alignment.push({
|
|
161
|
+
originalWord: originalWords[origIdx],
|
|
162
|
+
translatedWord: translatedWords[bestTransIdx],
|
|
163
|
+
originalIndex: origIdx,
|
|
164
|
+
translatedIndex: bestTransIdx,
|
|
165
|
+
});
|
|
166
|
+
matchedTranslatedIndices.add(bestTransIdx);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return alignment;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Reattach symbols to translated text based on word alignment.
|
|
173
|
+
*
|
|
174
|
+
* @param translatedText - The translated plain text
|
|
175
|
+
* @param originalParsed - The original parsed message with symbols
|
|
176
|
+
* @param alignment - Word alignment between original and translation
|
|
177
|
+
* @returns Translated text with symbols embedded (as rich text structure)
|
|
178
|
+
*/
|
|
179
|
+
function reattachSymbols(translatedText, originalParsed, alignment) {
|
|
180
|
+
// Tokenize the translated text
|
|
181
|
+
const translatedWords = translatedText
|
|
182
|
+
.trim()
|
|
183
|
+
.replace(/\s+/g, ' ')
|
|
184
|
+
.split(/\s+/)
|
|
185
|
+
.filter((w) => w.length > 0);
|
|
186
|
+
// Create the rich text symbols array
|
|
187
|
+
const richTextSymbols = [];
|
|
188
|
+
for (const symbol of originalParsed.symbols) {
|
|
189
|
+
// Find the alignment for this word
|
|
190
|
+
const wordAlignment = alignment.find((a) => a.originalIndex === symbol.wordIndex);
|
|
191
|
+
if (wordAlignment && wordAlignment.translatedIndex < translatedWords.length) {
|
|
192
|
+
const translatedWord = translatedWords[wordAlignment.translatedIndex];
|
|
193
|
+
// Attach the symbol to the translated word
|
|
194
|
+
richTextSymbols.push({
|
|
195
|
+
text: translatedWord,
|
|
196
|
+
image: symbol.symbolRef,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
// Fallback: keep symbol on original word if no alignment found
|
|
201
|
+
richTextSymbols.push({
|
|
202
|
+
text: symbol.originalWord,
|
|
203
|
+
image: symbol.symbolRef,
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return {
|
|
208
|
+
text: translatedText,
|
|
209
|
+
richTextSymbols,
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Complete pipeline: translate a message while preserving symbol positions.
|
|
214
|
+
*
|
|
215
|
+
* @param originalMessage - The original message text
|
|
216
|
+
* @param translatedText - The translated text (from translation API)
|
|
217
|
+
* @param richTextSymbols - Original symbols from richText.symbols
|
|
218
|
+
* @returns Object with translated text and aligned symbols
|
|
219
|
+
*/
|
|
220
|
+
function translateWithSymbols(originalMessage, translatedText, richTextSymbols) {
|
|
221
|
+
// Step 1: Parse original message
|
|
222
|
+
const parsedOriginal = parseMessageWithSymbols(originalMessage, richTextSymbols);
|
|
223
|
+
// If no symbols, return as-is
|
|
224
|
+
if (parsedOriginal.symbols.length === 0) {
|
|
225
|
+
return {
|
|
226
|
+
text: translatedText,
|
|
227
|
+
richTextSymbols: [],
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
// Step 2: Tokenize translated text
|
|
231
|
+
const translatedWords = translatedText
|
|
232
|
+
.trim()
|
|
233
|
+
.replace(/\s+/g, ' ')
|
|
234
|
+
.split(/\s+/)
|
|
235
|
+
.filter((w) => w.length > 0);
|
|
236
|
+
// Step 3: Align words
|
|
237
|
+
const alignment = alignWords(parsedOriginal.words, translatedWords);
|
|
238
|
+
// Step 4: Reattach symbols
|
|
239
|
+
const result = reattachSymbols(translatedText, parsedOriginal, alignment);
|
|
240
|
+
return result;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Extract symbols from a button for use during translation.
|
|
244
|
+
*
|
|
245
|
+
* This helper extracts symbols from either:
|
|
246
|
+
* - button.semanticAction.richText.symbols
|
|
247
|
+
* - button.image (if it's a symbol library reference)
|
|
248
|
+
*
|
|
249
|
+
* @param button - The AAC button
|
|
250
|
+
* @returns Array of symbol attachments
|
|
251
|
+
*/
|
|
252
|
+
function extractSymbolsFromButton(button) {
|
|
253
|
+
// First check richText structure
|
|
254
|
+
if (button.semanticAction?.richText?.symbols) {
|
|
255
|
+
return button.semanticAction.richText.symbols;
|
|
256
|
+
}
|
|
257
|
+
// Check if button has a symbol library reference as image
|
|
258
|
+
if (button.symbolLibrary && button.symbolPath) {
|
|
259
|
+
// Create a symbol attachment for the label/message
|
|
260
|
+
const text = button.label || button.message || '';
|
|
261
|
+
if (text) {
|
|
262
|
+
return [
|
|
263
|
+
{
|
|
264
|
+
text,
|
|
265
|
+
image: `[${button.symbolLibrary}]${button.symbolPath}`,
|
|
266
|
+
},
|
|
267
|
+
];
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
// Check if image field contains a symbol reference
|
|
271
|
+
if (button.image && button.image.startsWith('[')) {
|
|
272
|
+
const text = button.label || button.message || '';
|
|
273
|
+
if (text) {
|
|
274
|
+
return [
|
|
275
|
+
{
|
|
276
|
+
text,
|
|
277
|
+
image: button.image,
|
|
278
|
+
},
|
|
279
|
+
];
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return undefined;
|
|
283
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { BaseProcessor, ProcessorOptions, ExtractStringsResult, TranslatedString, SourceString } from '../core/baseProcessor';
|
|
2
2
|
import { AACTree } from '../core/treeStructure';
|
|
3
|
+
import { type ButtonForTranslation, type LLMLTranslationResult } from '../utilities/translation/translationProcessor';
|
|
3
4
|
import { ValidationResult } from '../validation/validationTypes';
|
|
4
5
|
declare class GridsetProcessor extends BaseProcessor {
|
|
5
6
|
constructor(options?: ProcessorOptions);
|
|
@@ -18,6 +19,31 @@ declare class GridsetProcessor extends BaseProcessor {
|
|
|
18
19
|
extractTexts(filePathOrBuffer: string | Buffer): string[];
|
|
19
20
|
loadIntoTree(filePathOrBuffer: string | Buffer): AACTree;
|
|
20
21
|
processTexts(filePathOrBuffer: string | Buffer, translations: Map<string, string>, outputPath: string): Buffer;
|
|
22
|
+
/**
|
|
23
|
+
* Extract symbol information from a gridset for LLM-based translation.
|
|
24
|
+
* Returns a structured format showing which buttons have symbols and their context.
|
|
25
|
+
*
|
|
26
|
+
* This method uses shared translation utilities that work across all AAC formats.
|
|
27
|
+
*
|
|
28
|
+
* @param filePathOrBuffer - Path to gridset file or buffer
|
|
29
|
+
* @returns Array of symbol information for LLM processing
|
|
30
|
+
*/
|
|
31
|
+
extractSymbolsForLLM(filePathOrBuffer: string | Buffer): ButtonForTranslation[];
|
|
32
|
+
/**
|
|
33
|
+
* Apply LLM translations with symbol information.
|
|
34
|
+
* The LLM should provide translations with symbol attachments in the correct positions.
|
|
35
|
+
*
|
|
36
|
+
* This method uses shared translation utilities that work across all AAC formats.
|
|
37
|
+
*
|
|
38
|
+
* @param filePathOrBuffer - Path to gridset file or buffer
|
|
39
|
+
* @param llmTranslations - Array of LLM translations with symbol info
|
|
40
|
+
* @param outputPath - Where to save the translated gridset
|
|
41
|
+
* @param options - Translation options (e.g., allowPartial for testing)
|
|
42
|
+
* @returns Buffer of the translated gridset
|
|
43
|
+
*/
|
|
44
|
+
processLLMTranslations(filePathOrBuffer: string | Buffer, llmTranslations: LLMLTranslationResult[], outputPath: string, options?: {
|
|
45
|
+
allowPartial?: boolean;
|
|
46
|
+
}): Buffer;
|
|
21
47
|
saveFromTree(tree: AACTree, outputPath: string): void;
|
|
22
48
|
private calculateColumnDefinitions;
|
|
23
49
|
private calculateRowDefinitions;
|