- Python - 文字處理
- Python - 文字處理簡介
- Python - 文字處理環境
- Python - 字串不變性
- Python - 排序行
- Python - 段落重新格式化
- Python - 統計段落中的詞元
- Python - 二進位制ASCII轉換
- Python - 字串作為檔案
- Python - 反向讀取檔案
- Python - 過濾重複單詞
- Python - 從文字中提取電子郵件
- Python - 從文字中提取URL
- Python - 美化列印
- Python - 文字處理狀態機
- Python - 首字母大寫和翻譯
- Python - 分詞
- Python - 刪除停用詞
- Python - 同義詞和反義詞
- Python - 文字翻譯
- Python - 單詞替換
- Python - 拼寫檢查
- Python - WordNet介面
- Python - 語料庫訪問
- Python - 詞性標註
- Python - 塊和塊間隙
- Python - 塊分類
- Python - 文字分類
- Python - 二元語法
- Python - 處理PDF
- Python - 處理Word文件
- Python - 讀取RSS feed
- Python - 情感分析
- Python - 搜尋和匹配
- Python - 文字清洗
- Python - 文字換行
- Python - 頻率分佈
- Python - 文字摘要
- Python - 詞幹提取演算法
- Python - 受約束的搜尋
Python - 拼寫檢查
拼寫檢查是任何文字處理或分析的基本要求。Python包pyspellchecker 提供了這項功能,可以查詢可能拼寫錯誤的單詞,並建議可能的更正。
首先,我們需要在Python環境中使用以下命令安裝所需的包。
pip install pyspellchecker
現在我們看看如何使用該包來指出拼寫錯誤的單詞,並對可能的正確單詞提出一些建議。
from spellchecker import SpellChecker
spell = SpellChecker()
# find those words that may be misspelled
misspelled = spell.unknown(['let', 'us', 'wlak','on','the','groun'])
for word in misspelled:
# Get the one `most likely` answer
print(spell.correction(word))
# Get a list of `likely` options
print(spell.candidates(word))
執行上述程式後,我們將得到以下輸出:
group
{'group', 'ground', 'groan', 'grout', 'grown', 'groin'}
walk
{'flak', 'weak', 'walk'}
區分大小寫
如果我們使用Let代替let,這將成為與字典中最接近匹配的單詞的大小寫敏感比較,結果現在看起來不同了。
from spellchecker import SpellChecker
spell = SpellChecker()
# find those words that may be misspelled
misspelled = spell.unknown(['Let', 'us', 'wlak','on','the','groun'])
for word in misspelled:
# Get the one `most likely` answer
print(spell.correction(word))
# Get a list of `likely` options
print(spell.candidates(word))
執行上述程式後,我們將得到以下輸出:
group
{'groin', 'ground', 'groan', 'group', 'grown', 'grout'}
walk
{'walk', 'flak', 'weak'}
get
{'aet', 'ret', 'get', 'cet', 'bet', 'vet', 'pet', 'wet', 'let', 'yet', 'det', 'het', 'set', 'et', 'jet', 'tet', 'met', 'fet', 'net'}
廣告