我正在審查別人的州拼寫檢查器。他們運行的測驗資料似乎運行良好,但嘗試不同的資料集,它似乎無法超過州名中的第一個單詞“North”。
我需要代碼才能使用兩個單詞來處理州名。
這是代碼:
import sys
!pip install pyspellchecker
from spellchecker import SpellChecker
#from google.colab import files
import pandas as pd
import io
#Implement spellcheck.
spell=SpellChecker()
for ind in newDF.index:
stateWordList = newDF['State'][ind].split()
if len(stateWordList) == 1:
#print(True)
if stateWordList[0] in spell:
pass
else:
correctState = input("'{}' is not a valid state, please enter a correct spelling:".format(stateWordList[0]))
newDF.at[ind, 'State'] = correctState
else:
misspelledState = False in (stateWord in spell for stateWord in stateWordList)
if misspelledState == True:
pass
else:
correctState = input("'{}' is not a valid state, please enter a correct spelling:".format(stateWordList[0]))
newDF.at[ind, 'State'] = correctState
相反,它沒有將 NorthwhateverState 視為有效,并回傳:
'North' is not a valid state, please enter a correct spelling:
是否需要專門針對兩個單詞名稱的條件?
uj5u.com熱心網友回復:
在你的else陳述中,你有一個邏輯錯誤
else:
misspelledState = False in (stateWord in spell for stateWord in stateWordList)
if misspelledState == True:
pass
else:
correctState = input("'{}' is not a valid state, please enter a correct spelling:".format(stateWordList[0]))
newDF.at[ind, 'State'] = correctState
讓我們看看misspelledState = False in (stateWord in spell for stateWord in stateWordList),如果所有單詞stateWordList都拼寫好,你正在檢查misspelledState = False in (True, True, ...),結果將是False。
然后轉到if-else條件,它將轉到else輸出更正訊息的條件:
if misspelledState == True:
pass
else:
correctState = input("'{}' is not a valid state, please enter a correct spelling:".format(stateWordList[0]))
newDF.at[ind, 'State'] = correctState
您可以使用
misspelledState = all([stateWord in spell for stateWord in stateWordList])
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/466432.html
標籤:python-3.x 熊猫 拼写
