首页 > 解决方案 > 在本地机器上使用拼写检查器?

问题描述

我注意到给定机器(Mac、Linux 或 Windows)上的常见应用程序都有各自的拼写检查器。从各种 IDE 到 MS Word/Office,再到笔记软件,应有尽有。

我正在尝试利用我们各自机器的内置实用程序来分析字符串的语法正确性。看来我不能只使用机器上的东西,而且可能不得不下载一个字典来比较。

我不确定是否有更好的方法来实现这一点。我正在考虑尝试在本地做事,但我并不反对执行 api 或 curl 请求来确定字符串中的单词是否拼写正确。

我在看:

我正在查看 Node 包,并注意到拼写检查模块也封装了单词表。

有没有办法完全利用内置的机器词典,或者如果我下载一个词典/单词表进行比较会是理想的吗?

我在想一个单词表可能是最好的选择,但我不想重新发明轮子。其他人做了什么来完成类似的工作?

标签: pythonnode.jsstringspell-checking

解决方案


功劳归于 Lukas Knuth。我想给出一个明确的如何使用字典和 nspell。

安装以下 2 个依赖项:

npm install nspell dictionary-en-us

这是我为解决问题而编写的示例文件。

// Node File

//  node spellcheck.js [path]
//  path: [optional] either absolute or local path from pwd/cwd

//  if you run the file from within Seg.Ui.Frontend/ it works as well.
//    node utility/spellcheck.js
//  OR from the utility directory using a path:
//    node spellcheck.js ../src/assets/i18n/en.json

var fs = require("fs");
var dictionary = require("dictionary-en-us");
var nspell = require("nspell");
var process = require("process");
// path to use if not defined.
var path = "src/assets/i18n/en.json"

let strings = [];
function getStrings(json){
    let keys = Object.keys(json);
    for (let idx of keys){
        let val = json[idx];
        if (isObject(val)) getStrings(val);
        if (isString(val)) strings.push(val)
    }
}

function sanitizeStrings(strArr){
    let set = new Set();
    for (let sentence of strArr){
        sentence.split(" ").forEach(word => {
            word = word.trim().toLowerCase();
            if (word.endsWith(".") || word.endsWith(":") || word.endsWith(",")) word = word.slice(0, -1);
            if (ignoreThisString(word)) return;
            if (word == "") return;
            if (isNumber(word)) return;
            set.add(word)
        });
    }
    return [ ...set ];
}

function ignoreThisString(word){
    // we need to ignore special cased strings, such as items with
    //  Brackets, Mustaches, Question Marks, Single Quotes, Double Quotes
    let regex = new RegExp(/[\{\}\[\]\'\"\?]/, "gi");
    return regex.test(word);
}

function spellcheck(err, dict){
    if (err) throw err;
    var spell = nspell(dict);
    let misspelled_words = strings.filter( word => {
        return !spell.correct(word)
    });
    misspelled_words.forEach( word => console.log(`Plausible Misspelled Word: ${word}`))
    return misspelled_words;
}

function isObject(obj) { return obj instanceof Object }
function isString(obj) { return typeof obj === "string" }
function isNumber(obj) { return !!parseInt(obj, 10)}

function main(args){
    //node file.js path
    if (args.length >= 3) path = args[2]
    if (!fs.existsSync(path)) {
        console.log(`The path does not exist: ${process.cwd()}/${path}`);
        return;
    }
    var content = fs.readFileSync(path)
    var json = JSON.parse(content);
    getStrings(json);
    // console.log(`String Array (length: ${strings.length}): ${strings}`)
    strings = sanitizeStrings(strings);
    console.log(`String Array (length: ${strings.length}): ${strings}\n\n`)

    dictionary(spellcheck);
}
main(process.argv);

这将返回要查看的字符串子集,它们可能拼写错误或误报。

误报将表示为:

  • 首字母缩略词
  • 单词的非美国英语变体
  • 例如,无法识别的专有名词、星期几和月份。
  • 包含括号的字符串。这可以通过将它们从单词中删除来增强。

显然,这并不适用于所有情况,但我添加了一个忽略此字符串功能,如果说它包含开发人员希望忽略的特殊单词或短语,您可以利用它。

这意味着作为节点脚本运行。


推荐阅读