generated from mirrors/plugin-sample-vite-svelte
Add offline spell-checking
All checks were successful
Build on Push and create Release on Tag / build (push) Successful in 4m2s
All checks were successful
Build on Push and create Release on Tag / build (push) Successful in 4m2s
This commit is contained in:
parent
a13ac05afb
commit
032e7f0b8c
11 changed files with 252 additions and 64 deletions
45
src/espells.ts
Normal file
45
src/espells.ts
Normal 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
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue