2 回答
TA贡献1777条经验 获得超10个赞
功劳归于 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);
这将返回要查看的字符串子集,它们可能拼写错误或误报。
误报将表示为:
首字母缩略词
单词的非美国英语变体
例如,未识别的专有名词、星期几和月份。
包含括号的字符串。这可以通过将它们从单词中删除来增强。
显然,这并不适用于所有情况,但我添加了一个忽略这个字符串函数,如果它包含开发人员想要忽略的特殊单词或短语,您可以利用它。
TA贡献1809条经验 获得超8个赞
您的问题被标记为 NodeJS 和 Python。这是 NodeJS 特定的部分,但我认为它与 python 非常相似。
Windows(从 Windows 8 开始)和 Mac OS X 确实有内置的拼写检查引擎。
Windows:“Windows 拼写检查 API”是一个 C/C++ API。要将它与 NodeJS 一起使用,您需要创建一个绑定。
Mac OS X:“NSSpellChecker”是 AppKit 的一部分,用于 GUI 应用程序。这是一个Objective-C API,因此您需要再次创建一个绑定。
Linux:这里没有“特定于操作系统的”API。大多数应用程序使用 Hunspell,但也有其他选择。这又是一个 C/C++ 库,因此需要绑定。
幸运的是,已经有一个名为拼写检查器的模块,它具有上述所有功能的绑定。这将使用其安装平台的内置系统,但有多个缺点:
1) 必须构建本机扩展。这个已经通过 node-pre-gyp 完成了二进制文件,但这些需要为特定平台安装。如果您在 Mac OS X 上开发,运行npm install
以获取包,然后在 Linux 上部署您的应用程序(使用 - 目录node_modules
),它将无法工作。
2) 使用内置拼写检查将使用操作系统规定的默认值,这可能不是您想要的。例如,使用的语言可能由所选的操作系统语言决定。对于 UI 应用程序(例如使用 Electron 构建),这可能没问题,但如果您想使用操作系统语言以外的语言进行服务器端拼写检查,则可能会很困难。
在基本层面,拼写检查一些文本归结为:
标记字符串(例如通过空格)
根据已知正确单词列表检查每个标记
(奖励)收集错误令牌的建议并为用户提供选项。
您可以自己编写第 1 部分。第 2 部分和第 3 部分需要“已知正确单词列表”或字典。幸运的是,已经有一种格式和工具可以使用它:
simple-spellchecker可以使用
.dic
-files。nspell是 Hunspell 的 JS 实现,带有自己的字典包。
例如,可以在此 repo 中找到其他词典
有了这个,你就可以选择语言,你不需要构建/下载任何本机代码,你的应用程序将在每个平台上运行相同。如果您在服务器上进行拼写检查,这可能是您最灵活的选择。
添加回答
举报