as-services/libs/utils/words-id/autocorrect.js
devthejo 16b7e7d6aa
All checks were successful
/ build (map[dockerfile:./services/hasura/Dockerfile name:hasura]) (push) Successful in 47s
/ build (map[dockerfile:./services/web/Dockerfile name:web]) (push) Successful in 1m47s
/ build (map[dockerfile:./services/watchers/Dockerfile name:watchers]) (push) Successful in 2m37s
/ build (map[dockerfile:./services/files/Dockerfile name:files]) (push) Successful in 2m52s
/ build (map[dockerfile:./services/api/Dockerfile name:api]) (push) Successful in 3m2s
/ build (map[dockerfile:./services/app/Dockerfile name:app]) (push) Successful in 31s
/ build (map[dockerfile:./services/tasks/Dockerfile name:tasks]) (push) Successful in 2m44s
/ deploy (push) Successful in 48s
chore(init): available sources
2025-04-13 10:46:53 +02:00

38 lines
954 B
JavaScript

const { snakeCase, capitalize } = require("lodash")
const BKNodePhonetic = require("./autocorrect/BKNodePhonetic")
const frWordlist = require("./wordlists/fr.json")
const frWordsNode = new BKNodePhonetic(frWordlist[0])
frWordlist.slice(1).forEach((word) => frWordsNode.insert(word))
const wordsNode = frWordsNode
const wordlist = frWordlist
function wordsAutocorrect(words) {
if (typeof words === "string") {
words = snakeCase(words).split("_")
}
words = words.map((word) => {
if (wordlist.includes(word)) {
return word
}
const searchResult = wordsNode.search(word, null, 2)
if (searchResult) {
return searchResult.word
}
return null
})
if (words.some((w) => !w)) {
return false
}
return words.map(capitalize).join("")
}
module.exports = wordsAutocorrect
// console.log(
// ["AbaiserAbrupteChato", "AcuseAssierAssiduler", "AbisalAboutyrAbollier"].map(
// wordsAutocorrect
// )
// )