我正在寻找一种在 python 中创建一个函数的方法,在该函数中传递一个字符串,然后它返回它是否拼写正确。我不想查字典。相反,我希望它检查 Google 的拼写建议。这样,名人名字和其他各种专有名词将被视为拼写正确。
到目前为止,这就是我所处的位置。它在大多数情况下都有效,但它会混淆一些名人的名字。例如,“cee lo green”或“posner”之类的内容会被标记为不正确。
import httplib
import xml.dom.minidom
data = """
<spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1">
<text> %s </text>
</spellrequest>
"""
def spellCheck(word_to_spell):
con = httplib.HTTPSConnection("www.google.com")
con.request("POST", "/tbproxy/spell?lang=en", data % word_to_spell)
response = con.getresponse()
dom = xml.dom.minidom.parseString(response.read())
dom_data = dom.getElementsByTagName('spellresult')[0]
if dom_data.childNodes:
for child_node in dom_data.childNodes:
result = child_node.firstChild.data.split()
for word in result:
if word_to_spell.upper() == word.upper():
return True;
return False;
else:
return True;
Peter Norvig 告诉您如何实施Python 中的拼写检查器。 http://norvig.com/spell-correct.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)