Add offline spell-checking
All checks were successful
Build on Push and create Release on Tag / build (push) Successful in 4m2s

This commit is contained in:
MassiveBox 2025-10-04 16:22:54 +02:00
parent a13ac05afb
commit 032e7f0b8c
Signed by: massivebox
GPG key ID: 9B74D3A59181947D
11 changed files with 252 additions and 64 deletions

45
src/espells.ts Normal file
View file

@ -0,0 +1,45 @@
import {Language, SpellChecker, Suggestion} from "@/spellChecker";
import { Espells } from "espells"
export class ESpellChecker implements SpellChecker {
spellchecker: Espells
loadedLanguages: Language[]
constructor(languages: {aff: string, dic: string, language: Language}[]) {
this.spellchecker = new Espells({aff: languages[0].aff, dic: languages.map(l => l.dic)})
this.loadedLanguages = languages.map(l => l.language)
}
async check(text: string, _: string[]): Promise<Suggestion[]> {
let suggestions: Suggestion[] = []
const regex = /[\w']+/g;
let match;
while ((match = regex.exec(text)) !== null) {
const word = match[0];
const counter = match.index;
const {correct} = this.spellchecker.lookup(word)
if(!correct) {
const hsSuggestions = this.spellchecker.suggest(word)
suggestions.push({
typeName: "UnknownWord",
message: word,
shortMessage: "Misspelled word",
replacements: hsSuggestions,
offset: counter,
length: word.length
})
}
}
return suggestions
}
async getLanguages(): Promise<Language[]> {
return this.loadedLanguages
}
}