Python 中的語法和拼寫檢查器

在下面的教程中,我們將討論一個名為語言工具的 Python 包,並了解如何使用 Python 編程語言創建一個簡單的語法和拼寫檢查器。

所以,讓我們開始吧。

理解 Python 語言工具庫

LanguageTool 是一個用於語法和拼寫檢查的開源工具,也被稱為 OpenOffice 的拼寫檢查器。這個包允許程序員通過 Python 代碼片段或命令行界面來檢測語法和拼寫錯誤。

如何安裝語言工具庫?

要安裝 Python 庫,我們需要『pip』,一個管理從可信公共存儲庫中安裝模塊所需的包的框架。一旦我們有了【畫中畫】,我們就可以使用來自窗口命令提示符(CMD)或終端的命令安裝語言工具庫,如下所示:

語法:


$ pip install language-tool-python

language_tool_python 庫會默認下載一個 LanguageTool 服務器作為 JAR 文件,並在後台執行,在本地檢測語法錯誤。但是 LanguageTool 也提供了支持的公共 HTTP 校對 API 然而,通話次數是有限制的。

驗證安裝

一旦安裝了庫,我們可以通過創建一個空的 Python 程序文件並編寫一個 import 語句來驗證它,如下所示:

文件:驗證. py


import language_tool_python

現在,保存上述文件,並在終端中使用以下命令執行它:

語法:


$ python verify.py

如果上述 Python 程序文件沒有返回任何錯誤,則庫安裝正確。但是,在出現異常的情況下,請嘗試重新安裝庫,並且還建議參考模塊的官方文檔。

使用 Python 語言工具庫

在下一節中,我們將使用一個實際的例子來理解 Python 中語言工具庫的工作。下面的 Python 腳本演示了語法錯誤的檢測和糾正。我們將使用以下文本:

以上文本包含一些用粗體突出顯示的語法和拼寫錯誤。讓我們考慮下面的 Python 腳本來理解語言工具**實用程序的工作原理:

示例:


# importing the package
import language_tool_python

# using the tool
my_tool = language_tool_python.LanguageTool('en-US')

# given text
my_text = """LanguageTool provides utility to check grammar and spelling errors. We just have to paste the text here and click the 'Check Text' button. Click the colored phrases for for information on potential errors. or we can use this text too see an some of the issues that LanguageTool can dedect. Whot do someone thinks of grammar checkers? Please not that they are not perfect. Style problems get a blue marker: It is 7 P.M. in the evening. The weather was nice on Monday, 22 November 2021""" 

# getting the matches
my_matches = my_tool.check(my_text)

# printing matches
print(my_matches)

輸出:

[Match({'ruleId': 'ENGLISH_WORD_REPEAT_RULE', 'message': 'Possible typo: you repeated a word', 'replacements': ['for'], 'offsetInContext': 43, 'context': "...Text' button. Click the colored phrases for for information on potential errors. or we ...", 'offset': 165, 'errorLength': 7, 'category': 'MISC', 'ruleIssueType': 'duplication', 'sentence': 'Click the colored phrases for for information on potential errors.'}), Match({'ruleId': 'UPPERCASE_SENTENCE_START', 'message': 'This sentence does not start with an uppercase letter.', 'replacements': ['Or'], 'offsetInContext': 43, 'context': '...or for information on potential errors. or we can use this text too see an some of...', 'offset': 206, 'errorLength': 2, 'category': 'CASING', 'ruleIssueType': 'typographical', 'sentence': 'or we can use this text too see an some of the issues that LanguageTool can dedect.'}), Match({'ruleId': 'TOO_TO', 'message': 'Did you mean "to see"?', 'replacements': ['to see'], 'offsetInContext': 43, 'context': '...tential errors. or we can use this text too see an some of the issues that LanguageTool...', 'offset': 230, 'errorLength': 7, 'category': 'CONFUSED_WORDS', 'ruleIssueType': 'misspelling', 'sentence': 'or we can use this text too see an some of the issues that LanguageTool can dedect.'}), Match({'ruleId': 'EN_A_VS_AN', 'message': 'Use "a" instead of 'an' if the following word doesn't start with a vowel sound, e.g. 'a sentence', 'a university'.', 'replacements': ['a'], 'offsetInContext': 43, 'context': '...errors. or we can use this text too see an some of the issues that LanguageTool ca...', 'offset': 238, 'errorLength': 2, 'category': 'MISC', 'ruleIssueType': 'misspelling', 'sentence': 'or we can use this text too see an some of the issues that LanguageTool can dedect.'}), Match({'ruleId': 'MORFOLOGIK_RULE_EN_US', 'message': 'Possible spelling mistake found.', 'replacements': ['detect', 'defect', 'deduct', 'deject'], 'offsetInContext': 43, 'context': '...ome of the issues that LanguageTool can dedect. Whot do someone thinks of grammar chec...', 'offset': 282, 'errorLength': 6, 'category': 'TYPOS', 'ruleIssueType': 'misspelling', 'sentence': 'or we can use this text too see an some of the issues that LanguageTool can dedect.'}), Match({'ruleId': 'MORFOLOGIK_RULE_EN_US', 'message': 'Possible spelling mistake found.', 'replacements': ['Who', 'What', 'Shot', 'Whom', 'Hot', 'WHO', 'Whet', 'Whit', 'Whoa', 'Whop', 'WHT', 'Wot', 'W hot'], 'offsetInContext': 43, 'context': '...he issues that LanguageTool can dedect. Whot do someone thinks of grammar checkers? ...', 'offset': 290, 'errorLength': 4, 'category': 'TYPOS', 'ruleIssueType': 'misspelling', 'sentence': 'Whot do someone thinks of grammar checkers?'}), Match({'ruleId': 'PLEASE_NOT_THAT', 'message': 'Did you mean "note"?', 'replacements': ['note'], 'offsetInContext': 43, 'context': '...eone thinks of grammar checkers? Please not that they are not perfect. Style proble...', 'offset': 341, 'errorLength': 3, 'category': 'TYPOS', 'ruleIssueType': 'misspelling', 'sentence': 'Please not that they are not perfect.'}), Match({'ruleId': 'PM_IN_THE_EVENING', 'message': 'This is redundant. Consider using "P.M."', 'replacements': ['P.M.'], 'offsetInContext': 43, 'context': '...yle problems get a blue marker: It is 7 P.M. in the evening. The weather was nice on Monday, 22 Nov...', 'offset': 414, 'errorLength': 19, 'category': 'REDUNDANCY', 'ruleIssueType': 'style', 'sentence': 'Style problems get a blue marker: It is 7 P.M. in the evening.'})]

說明:

在上面的代碼片段中,我們已經導入了所需的庫,並定義了一個工具,該工具使用 LanguageTool 實用程序來檢查文本中的語法和拼寫錯誤。然後,我們定義了另一個字符串變量來存儲我們想要檢查的文本段落。然後,我們使用 check() 功能檢索匹配,並為用戶打印它們。

因此,我們可以觀察到我們有一個詳細的字典,顯示了規則標識、消息、替換、偏移上下文、上下文、偏移等等。我們可以在語言工具社區找到每個規則標識的詳細解釋。

既然我們已經發現了錯誤,是時候糾正它們了。讓我們考慮下面演示相同內容的 Python 腳本:

示例:


# importing the package
import language_tool_python

# using the tool
my_tool = language_tool_python.LanguageTool('en-US')

# given text
my_text = """LanguageTool provides utility to check grammar and spelling errors. We just have to paste the text here and click the 'Check Text' button. Click the colored phrases for for information on potential errors. or we can use this text too see an some of the issues that LanguageTool can dedect. Whot do someone thinks of grammar checkers? Please not that they are not perfect. Style problems get a blue marker: It is 7 P.M. in the evening. The weather was nice on Monday, 22 November 2021""" 

# getting the matches
my_matches = my_tool.check(my_text)

# defining some variables
myMistakes = []
myCorrections = []
startPositions = []
endPositions = []

# using the for-loop
for rules in my_matches:
    if len(rules.replacements) > 0:
        startPositions.append(rules.offset)
        endPositions.append(rules.errorLength + rules.offset)
        myMistakes.append(my_text[rules.offset : rules.errorLength + rules.offset])
        myCorrections.append(rules.replacements[0])

# creating new object
my_NewText = list(my_text) 

# rewriting the correct passage
for n in range(len(startPositions)):
    for i in range(len(my_text)):
        my_NewText[startPositions[n]] = myCorrections[n]
        if (i > startPositions[n] and i < endPositions[n]):
            my_NewText[i] = ""

my_NewText = "".join(my_NewText)

# printing the text
print(my_NewText)

輸出:

LanguageTool provides utility to check grammar and spelling errors. We just have to paste the text here and click the 'Check Text' button. Click the colored phrases for information on potential errors. Or we can use this text to see a some of the issues that LanguageTool can detect. Who do someone thinks of grammar checkers? Please note that they are not perfect. Style problems get a blue marker: It is 7 P.M.. The weather was nice on Monday, 22 November 2021

說明:

在上面的代碼片段中,我們包含了一些新的變量來處理錯誤、更正、開始位置和結束位置。然後,我們使用循環的來遍歷 my_matches 中的規則,並用它們的更正替換錯誤。然後,我們將這些更正的文本存儲在一個列表中。最後,我們再次使用的循環來遍歷列表中的字符串元素,將它們連接在一起,並為用戶打印結果文本。

因此,我們成功地糾正了在前面的代碼片段中發現的錯誤。

現在,讓我們使用下面的 Python 腳本來觀察我們之前捕獲的錯誤以及它們各自的更正:

示例:


print(list(zip(myMistakes, myCorrections)))

輸出:

[('for for', 'for'), ('or', 'Or'), ('too see', 'to see'), ('an', 'a'), ('dedect', 'detect'), ('Whot', 'Who'), ('not', 'note'), ('P.M. in the evening', 'P.M.')]

說明:

在上面的代碼片段中,我們打印了文本中的錯誤列表及其相應的更正。

自動將建議應用於文本

讓我們考慮一個簡單的例子,演示如何使用 Python 中的語言工具庫將建議自動應用於文本。

示例:


# importing the library
import language_tool_python

# creating the tool
my_tool = language_tool_python.LanguageTool('en-US')

# given text
my_text = 'A quick broun fox jumpps over a a little lazy dog.'

# correction
correct_text = my_tool.correct(my_text)

# printing some texts
print("Original Text:", my_text)
print("Text after correction:", correct_text)

輸出:

Original Text: A quick broun fox jumpps over a a little lazy dog.
Text after correction: A quick brown fox jumps over a little lazy dog.

說明:

在上面的代碼片段中,我們已經導入了所需的庫,並為指定語言為美國英語的語言工具定義了工具。然後我們定義了一個字符串變量,並在其中存儲了一些文本。然後,我們使用工具的糾正()功能自動糾正文本中的錯誤,並為用戶打印結果文本。


原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/128714.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
簡單一點的頭像簡單一點
上一篇 2024-10-03 23:25
下一篇 2024-10-03 23:25

相關推薦

  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29
  • 蝴蝶優化算法Python版

    蝴蝶優化算法是一種基於仿生學的優化算法,模仿自然界中的蝴蝶進行搜索。它可以應用於多個領域的優化問題,包括數學優化、工程問題、機器學習等。本文將從多個方面對蝴蝶優化算法Python版…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智能、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29

發表回復

登錄後才能評論