-
-
Notifications
You must be signed in to change notification settings - Fork 39
feat: add selectable multilingual spelling dictionaries #657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FuJacob
wants to merge
1
commit into
main
Choose a base branch
from
codex/multilingual-spelling-dictionaries
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import Foundation | ||
|
|
||
| /// A bundled SymSpell frequency dictionary that Cotabby can use for ranked typo correction. | ||
| /// | ||
| /// The raw value is the ISO 639-1 language code persisted in `UserDefaults`. Keeping the durable | ||
| /// representation as a standard language code makes future migrations straightforward and avoids | ||
| /// coupling stored preferences to display labels or resource filenames. | ||
| nonisolated enum SpellingDictionaryLanguage: String, CaseIterable, Codable, Hashable, Identifiable, Sendable { | ||
| case english = "en" | ||
| case german = "de" | ||
| case spanish = "es" | ||
| case french = "fr" | ||
| case hebrew = "he" | ||
| case italian = "it" | ||
| case russian = "ru" | ||
|
|
||
| var id: String { rawValue } | ||
|
|
||
| /// English name used in logs, documentation, and accessibility descriptions. | ||
| var displayName: String { | ||
| switch self { | ||
| case .english: return "English" | ||
| case .german: return "German" | ||
| case .spanish: return "Spanish" | ||
| case .french: return "French" | ||
| case .hebrew: return "Hebrew" | ||
| case .italian: return "Italian" | ||
| case .russian: return "Russian" | ||
| } | ||
| } | ||
|
|
||
| /// Native-script label shown in Settings so speakers can identify their language quickly. | ||
| var settingsLabel: String { | ||
| switch self { | ||
| case .english: return "English" | ||
| case .german: return "Deutsch (German)" | ||
| case .spanish: return "Español (Spanish)" | ||
| case .french: return "Français (French)" | ||
| case .hebrew: return "עברית (Hebrew)" | ||
| case .italian: return "Italiano (Italian)" | ||
| case .russian: return "Русский (Russian)" | ||
| } | ||
| } | ||
|
|
||
| /// Resource basename from the upstream SymSpell frequency-dictionary folder. | ||
| var resourceName: String { | ||
| switch self { | ||
| case .english: return "frequency_dictionary_en_82_765" | ||
| case .german: return "de-100k" | ||
| case .spanish: return "es-100l" | ||
| case .french: return "fr-100k" | ||
| case .hebrew: return "he-100k" | ||
| case .italian: return "it-100k" | ||
| case .russian: return "ru-100k" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Pure catalog rules for the spelling-dictionary setting. | ||
| /// | ||
| /// This is intentionally separate from `LanguageCatalog`: response languages steer model output, | ||
| /// while spelling dictionaries decide which deterministic correction indexes may be queried. A | ||
| /// multilingual writer may reasonably enable several response languages but keep autocorrection | ||
| /// limited to one conservative dictionary. | ||
| nonisolated enum SpellingDictionaryCatalog { | ||
| static let defaultEnabledCodes = [SpellingDictionaryLanguage.english.rawValue] | ||
|
|
||
| /// Drops unknown and duplicate codes, then returns the result in stable catalog order. | ||
| /// | ||
| /// Stable ordering keeps persisted values, snapshots, tests, and Settings rendering | ||
| /// deterministic even when callers build the input from a `Set`. | ||
| static func normalize(_ codes: [String]) -> [String] { | ||
| let requested = Set(codes.map { | ||
| $0.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() | ||
| }) | ||
| return SpellingDictionaryLanguage.allCases | ||
| .filter { requested.contains($0.rawValue) } | ||
| .map(\.rawValue) | ||
| } | ||
|
|
||
| static func languages(for codes: [String]) -> [SpellingDictionaryLanguage] { | ||
| normalize(codes).compactMap(SpellingDictionaryLanguage.init(rawValue:)) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
es-100l(letterl) diverges from the*-100kpattern every other language follows (de-100k,fr-100k,he-100k,it-100k,ru-100k). The actual file on disk is alsoes-100l.txt, so this works correctly at runtime, but anyone expectinges-100kwhen browsing the bundle or the resource directory will look for the wrong file. A comment on the case would prevent future confusion.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!