州名拼写检查代码不适用于两个单词的州名

State name spellcheck code not working with two word state names

我正在审查其他人的状态拼写检查器。他们运行的测试数据似乎工作正常,但尝试不同的数据集,它似乎无法通过州名中的第一个单词“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

相反,它没有将 North WhateverState 视为有效,并且 returns:

'North' is not a valid state, please enter a correct spelling:

是否需要专门针对两个单词名称的条件?

在你的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])