[英]Leveraging Spell Checker on local machine?
我注意到給定機器(Mac、Linux 或 Windows)上的常見應用程序都有各自的拼寫檢查器。 從各種 IDE,到 MS Word/Office,再到筆記軟件,應有盡有。
我正在嘗試利用我們各自機器的內置實用程序來分析字符串的語法正確性。 似乎我不能只使用機器上的內容,並且可能不得不下載一個字典來進行比較。
我不確定是否有更好的方法來實現這一點。 我正在考慮嘗試在本地做事,但我並不反對通過 api 或 curl 請求來確定字符串中的單詞是否拼寫正確。
我在看:
我正在查看 Node 包,並注意到拼寫檢查模塊也封裝了單詞表。
有沒有辦法完全利用內置的機器字典,或者如果我下載字典/詞表進行比較是否理想?
我認為單詞表可能是最好的選擇,但我不想重新發明輪子。 其他人做了什么來實現類似的目標?
功勞歸於 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);
這將返回要查看的字符串子集,它們可能拼寫錯誤或誤報。
誤報將表示為:
顯然,這並不適用於所有情況,但我添加了一個忽略此字符串函數,如果它包含開發人員想要忽略的特殊單詞或短語,您可以利用它。
這意味着作為節點腳本運行。
您的問題被標記為 NodeJS 和 Python。 這是 NodeJS 特定的部分,但我想它與 python 非常相似。
Windows(從 Windows 8 開始)和 Mac OS X 確實有內置的拼寫檢查引擎。
幸運的是,已經有一個名為拼寫檢查器的模塊,它具有上述所有功能的綁定。 這將使用其安裝平台的內置系統,但有多個缺點:
1) 必須構建本機擴展。 這個已經通過 node-pre-gyp 完成了二進制文件,但這些需要為特定平台安裝。 如果你在 Mac OS X 上開發,運行npm install
來獲取包,然后在 Linux 上部署你的應用程序(使用node_modules
),它不會工作。
2) 使用內置拼寫檢查將使用操作系統規定的默認值,這可能不是您想要的。 例如,使用的語言可能由所選的操作系統語言決定。 對於 UI 應用程序(例如使用 Electron 構建),這可能沒問題,但如果您想使用操作系統語言以外的語言進行服務器端拼寫檢查,則可能會很困難。
在基本層面,拼寫檢查一些文本歸結為:
您可以自己編寫第 1 部分。 第 2 部分和第 3 部分需要“已知正確單詞列表”或字典。 幸運的是,已經有一種格式和工具可以使用它:
.dic
文件。有了這個,你就可以選擇語言,你不需要構建/下載任何本機代碼,你的應用程序將在每個平台上運行相同。 如果您在服務器上進行拼寫檢查,這可能是您最靈活的選擇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.