Personal Dictionary and Word Replacements
Relevant source files
The following files were used as context for generating this wiki page:- VoiceInk/Services/WordReplacementService.swift
- VoiceInk/Services/DictionaryContextService.swift
- VoiceInk/Views/Dictionary/DictionaryView.swift
- VoiceInk/Views/Dictionary/WordReplacementView.swift
- VoiceInk/Views/Dictionary/DictionarySettingsView.swift
- VoiceInk/Views/Dictionary/EditReplacementSheet.swift
- VoiceInk/Services/AIEnhancementService.swift
- VoiceInk/Services/CloudTranscription/SonioxTranscriptionService.swift
- VoiceInk/Services/AudioFileTranscriptionManager.swift
The Personal Dictionary and Word Replacements system is a key feature in VoiceInk designed to enhance transcription accuracy and automate text formatting. It consists of two distinct but related functionalities accessible through the "Dictionary Settings" view: "Correct Spellings" (the personal dictionary) and "Word Replacements".
The "Correct Spellings" feature allows users to add custom words, names, or jargon to a personal dictionary. This dictionary is then used as context for AI-powered transcription and enhancement services, improving their ability to recognize these specific terms. The "Word Replacements" feature provides a mechanism to automatically find and replace specific words or phrases in the final transcript, which is useful for correcting common mis-transcriptions, expanding abbreviations, or enforcing consistent formatting.
Sources: VoiceInk/Views/Dictionary/DictionarySettingsView.swift:7-27, VoiceInk/README.md:27
Architecture Overview
The system is composed of UI views for user interaction, manager classes for state and data management, and services that consume the data to perform their tasks. Data for both features is persisted in UserDefaults.
The following diagram illustrates the high-level interaction between these components.
This diagram shows that the DictionarySettingsView is the main entry point, directing the user to either the WordReplacementView or DictionaryView. These views use their respective manager classes (WordReplacementManager, DictionaryManager) to manipulate data stored in UserDefaults. Finally, backend services like WordReplacementService and DictionaryContextService read from UserDefaults to apply the user's custom configurations during the transcription and enhancement process.
Sources: VoiceInk/Views/Dictionary/DictionarySettingsView.swift, VoiceInk/Views/Dictionary/WordReplacementView.swift, VoiceInk/Views/Dictionary/DictionaryView.swift
Correct Spellings (Personal Dictionary)
This feature helps transcription models correctly recognize unique or technical vocabulary by providing a list of custom words as context. It is primarily used by the AI Enhancement and some cloud transcription services.
Sources: VoiceInk/Views/Dictionary/DictionaryView.swift:88-94, VoiceInk/Services/DictionaryContextService.swift
Data Model and Storage
Custom dictionary words are stored as an array of DictionaryItem objects, which are encoded to JSON and saved in UserDefaults under the key CustomDictionaryItems.
DictionaryItem Structure
| Property | Type | Description |
|---|---|---|
id | UUID | A unique identifier for the item. |
word | String | The custom word or phrase to be added to the dictionary. |
dateAdded | Date | The timestamp when the item was added. |
Sources: VoiceInk/Views/Dictionary/DictionaryView.swift:3-29, VoiceInk/Services/DictionaryContextService.swift:20-22
Management (DictionaryManager)
The DictionaryManager class is responsible for all operations related to the personal dictionary.
- Initialization: On creation, it loads and decodes the
DictionaryItemarray fromUserDefaults. - Persistence: Any changes (additions or removals) trigger a
saveItemsfunction, which encodes theitemsarray and writes it back toUserDefaults. - Operations:
addWord(String): Adds a new word, ensuring no case-insensitive duplicates exist.removeWord(String): Removes a word from the dictionary.allWords: A computed property that returns a simple[String]array of all words.
Sources: VoiceInk/Views/Dictionary/DictionaryView.swift:32-68
Integration and Usage
The custom words are consumed by services that provide context to transcription or enhancement models.
The DictionaryContextService is central to this process. Its getDictionaryContext() method:
- Retrieves a list of predefined, hard-coded words (e.g., "VoiceInk", "chatGPT").
- Calls
getCustomDictionaryWords()to load the user's custom words fromUserDefaults. - Combines both lists into a single comma-separated string.
- Formats this string into a final context message, like:
Important Vocabulary: VoiceInk, chatGPT, ....
This context string is then used by:
AIEnhancementService: Appends the dictionary context to the system message sent to the AI model.SonioxTranscriptionService: Includes the custom dictionary terms in the API request payload to improve recognition accuracy.
The following diagram shows the data flow for creating and using the dictionary context.
Word Replacements
This feature automatically replaces specified words or phrases in the transcribed text. It is applied after the initial transcription and before the text is finalized.
Sources: VoiceInk/Services/AudioFileTranscriptionManager.swift:115-117, VoiceInk/Views/Dictionary/WordReplacementView.swift:113-122
Data Model and Storage
Word replacements are stored as a [String: String] dictionary in UserDefaults under the key wordReplacements.
- Key: The "original" word or phrase to be replaced. A single key can contain multiple variants separated by commas (e.g.,
"Voicing, Voice ink, Voiceing"). - Value: The "replacement" text.
A boolean flag under the key IsWordReplacementEnabled in UserDefaults controls whether this feature is active.
Sources: VoiceInk/Views/Dictionary/WordReplacementView.swift:15-23, VoiceInk/Views/Dictionary/WordReplacementView.swift:239-242
Management (WordReplacementManager)
The WordReplacementManager class, defined in WordReplacementView.swift, handles the lifecycle of replacement entries.
- State: It publishes the
replacementsdictionary and theisEnabledflag, automatically saving any changes toUserDefaults. - Operations:
addReplacement(original: String, replacement: String): Adds a new key-value pair.removeReplacement(original: String): Removes an entry by its key.updateReplacement(oldOriginal: String, newOriginal: String, newReplacement: String): Updates an existing entry. This involves removing the old key and adding the new one.
Sources: VoiceInk/Views/Dictionary/WordReplacementView.swift:13-43
User Interface
The UI for managing word replacements is handled by a set of SwiftUI views:
WordReplacementView: The main view that displays the list of all replacements. It includes a toggle to enable/disable the feature and options to sort the list by original or replacement text.AddReplacementSheet: A modal sheet for adding a new replacement rule. It provides fields for the original text (allowing comma-separated values) and the replacement text.EditReplacementSheet: A similar modal sheet for editing an existing rule.
Sources: VoiceInk/Views/Dictionary/WordReplacementView.swift, VoiceInk/Views/Dictionary/EditReplacementSheet.swift
Execution Flow (WordReplacementService)
The core logic resides in WordReplacementService.shared.applyReplacements(to: String). This service is called after a transcript is generated but before it is presented to the user or sent for AI enhancement.
The process is as follows:
- The service checks if replacements are enabled and if any exist in
UserDefaults. - It iterates through each
(originalGroup, replacement)pair. - For each
originalGroup, it splits the string by commas to get individual variants. - For each
originalvariant, it determines if word boundaries should be used. TheusesWordBoundarieshelper function returnsfalsefor scripts that don't typically use spaces (like CJK, Thai) andtrueotherwise. - If using word boundaries: A case-insensitive regex pattern
\b<original>\bis created to ensure only whole words are matched. - If not using word boundaries: A simple case-insensitive substring replacement is performed.
- The
modifiedTextis updated with the replacement and the loop continues. - The final, modified text is returned.
The sequence diagram below visualizes this flow.
Here is the core implementation of the replacement logic:
// From VoiceInk/Services/WordReplacementService.swift
func applyReplacements(to text: String) -> String {
guard let replacements = UserDefaults.standard.dictionary(forKey: "wordReplacements") as? [String: String],
!replacements.isEmpty else {
return text // No replacements to apply
}
var modifiedText = text
// Apply replacements (case-insensitive)
for (originalGroup, replacement) in replacements {
// Split comma-separated originals at apply time only
let variants = originalGroup
.split(separator: ",")
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty }
for original in variants {
let usesBoundaries = usesWordBoundaries(for: original)
if usesBoundaries {
// Word-boundary regex for full original string
let pattern = "\\b\(NSRegularExpression.escapedPattern(for: original))\\b"
if let regex = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive) {
let range = NSRange(modifiedText.startIndex..., in: modifiedText)
modifiedText = regex.stringByReplacingMatches(
in: modifiedText,
options: [],
range: range,
withTemplate: replacement
)
}
} else {
// Fallback substring replace for non-spaced scripts
modifiedText = modifiedText.replacingOccurrences(of: original, with: replacement, options: .caseInsensitive)
}
}
}
return modifiedText
}
Sources: VoiceInk/Services/WordReplacementService.swift:7-40
Summary
The Personal Dictionary and Word Replacement features provide powerful tools for users to customize and improve their transcription workflow. The "Correct Spellings" dictionary enhances the accuracy of AI models by providing context for specialized vocabulary. The "Word Replacements" feature offers a robust, rule-based system for post-processing transcripts to correct errors and automate formatting. Both systems are managed through a clear user interface and are deeply integrated into the application's transcription pipeline.