Python: check whether a word is spelled correctly [closed]

点点圈 提交于 2020-01-09 09:03:35

问题


I'm looking for a an easy way to check whether a certain string is a correctly-spelled English word. For example, 'looked' would return True while 'hurrr' would return False. I don't need spelling suggestions or any spelling-correcting features. Just a simple function that takes a string and returns a boolean value.


回答1:


Two possible ways of doing it:

  1. Have your own file which has all the valid words. Load the file into a set and compare each word to see whether it exists in it (word in set)
  2. (The better way) Use PyEnchant, a spell checking library for Python

PyEnchant is not actively maintained now.




回答2:


I was looking for the same functionality and struggled to find an existing library that works in Windows, 64 bit. PyEnchant, although a great library, isn't currently active and doesn't work in 64 bit. Other libraries I found didn't work in Windows.

I finally found a solution that I hope others will find valuable.

The solution...

  • Use nltk
  • Extract the word list from nltk.corpus.brown
  • Convert the word list to a set (for efficient searching)
  • Use the in keyword to determine if your string is in the set

from nltk.corpus import brown
word_list = brown.words()
word_set = set(word_list)

# Check if word is in set
"looked" in word_set  # Returns True
"hurrr" in word_set  # Returns False

Use a timer check and you'll see this takes virtually no time to search the set. A test on 1,000 words took 0.004 seconds.




回答3:


I personally used: http://textblob.readthedocs.io/en/dev/ It is an active project and according to the website:

Spelling correction is based on Peter Norvig’s “How to Write a Spelling Corrector”[1] as implemented in the pattern library. It is about 70% accurate




回答4:


Yahoo provides spell checking API through YQL.

Its pretty simple and you get 5000 queries/ip address/day for non-commercial use (FREE)



来源:https://stackoverflow.com/questions/4500752/python-check-whether-a-word-is-spelled-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!