2 回答

TA貢獻1777條經(jīng)驗 獲得超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);
這將返回要查看的字符串子集,它們可能拼寫錯誤或誤報。
誤報將表示為:
首字母縮略詞
單詞的非美國英語變體
例如,未識別的專有名詞、星期幾和月份。
包含括號的字符串。這可以通過將它們從單詞中刪除來增強。
顯然,這并不適用于所有情況,但我添加了一個忽略這個字符串函數(shù),如果它包含開發(fā)人員想要忽略的特殊單詞或短語,您可以利用它。

TA貢獻1809條經(jīng)驗 獲得超8個贊
您的問題被標記為 NodeJS 和 Python。這是 NodeJS 特定的部分,但我認為它與 python 非常相似。
Windows(從 Windows 8 開始)和 Mac OS X 確實有內(nèi)置的拼寫檢查引擎。
Windows:“Windows 拼寫檢查 API”是一個 C/C++ API。要將它與 NodeJS 一起使用,您需要創(chuàng)建一個綁定。
Mac OS X:“NSSpellChecker”是 AppKit 的一部分,用于 GUI 應用程序。這是一個Objective-C API,因此您需要再次創(chuàng)建一個綁定。
Linux:這里沒有“特定于操作系統(tǒng)的”API。大多數(shù)應用程序使用 Hunspell,但也有其他選擇。這又是一個 C/C++ 庫,因此需要綁定。
幸運的是,已經(jīng)有一個名為拼寫檢查器的模塊,它具有上述所有功能的綁定。這將使用其安裝平臺的內(nèi)置系統(tǒng),但有多個缺點:
1) 必須構(gòu)建本機擴展。這個已經(jīng)通過 node-pre-gyp 完成了二進制文件,但這些需要為特定平臺安裝。如果您在 Mac OS X 上開發(fā),運行npm install
以獲取包,然后在 Linux 上部署您的應用程序(使用 - 目錄node_modules
),它將無法工作。
2) 使用內(nèi)置拼寫檢查將使用操作系統(tǒng)規(guī)定的默認值,這可能不是您想要的。例如,使用的語言可能由所選的操作系統(tǒng)語言決定。對于 UI 應用程序(例如使用 Electron 構(gòu)建),這可能沒問題,但如果您想使用操作系統(tǒng)語言以外的語言進行服務器端拼寫檢查,則可能會很困難。
在基本層面,拼寫檢查一些文本歸結(jié)為:
標記字符串(例如通過空格)
根據(jù)已知正確單詞列表檢查每個標記
(獎勵)收集錯誤令牌的建議并為用戶提供選項。
您可以自己編寫第 1 部分。第 2 部分和第 3 部分需要“已知正確單詞列表”或字典。幸運的是,已經(jīng)有一種格式和工具可以使用它:
simple-spellchecker可以使用
.dic
-files。nspell是 Hunspell 的 JS 實現(xiàn),帶有自己的字典包。
例如,可以在此 repo 中找到其他詞典
有了這個,你就可以選擇語言,你不需要構(gòu)建/下載任何本機代碼,你的應用程序?qū)⒃诿總€平臺上運行相同。如果您在服務器上進行拼寫檢查,這可能是您最靈活的選擇。
添加回答
舉報