How to Use Python spelling check more easily

Source: Internet
Author: User

Some problems have been plaguing us during the use of Python spelling checks. In fact, only continuous learning can make better use of this language. In the past few days, I found many spelling mistakes in the comments I wrote in the previous Code. These mistakes are not outrageous. I should be able to use tools to automatically correct most of them.

It is easy to use the Python spelling check script. It is easier to use the ready-made tools such as aspell and ispell.

Google Daniel Peter Norvig wrote an article on How to Write a Spelling Corrector which is worth seeing. Daniel is Daniel, who has 21 lines of Python Spelling check problems and does not need external tools, you only need to read a dictionary file in advance. The edits1 function of this program is copied from niujia.

 
 
  1. #!/usr/bin/python  
  2. # A simple spell checker  
  3. # written by http://www.vpsee.com   
  4. import os, sys, subprocess, signal  
  5. alphabet = 'abcdefghijklmnopqrstuvwxyz' 
  6. def found(word, args, cwd = None, shell = True):  
  7. child = subprocess.Popen(args,  
  8. shellshell = shell,  
  9. stdin = subprocess.PIPE,  
  10. stdout = subprocess.PIPE,  
  11. cwdcwd = cwd,  
  12. universal_newlines = True)  
  13. child.stdout.readline()  
  14. (stdout, stderr) = child.communicate(word)  
  15. if ": " in stdout:  
  16. # remove \n\n  
  17. stdoutstdout = stdout.rstrip("\n")  
  18. # remove left part until :  
  19. left, candidates = stdout.split(": ", 1)  
  20. candidatescandidates = candidates.split(", ")  
  21. # making an error on the first letter of a word is less  
  22. # probable, so we remove those candidates and append them  
  23. # to the tail of queue, make them less priority  
  24. for item in candidates:  
  25. if item[0] != word[0]:  
  26. candidates.remove(item)  
  27. candidates.append(item)  
  28. return candidates  
  29. else:  
  30. return None  
  31. # copy from http://norvig.com/spell-correct.html  
  32. def edits1(word):  
  33. n = len(word)  
  34. return set([word[0:i]+word[i+1:] for i in range(n)] +  
  35. [word[0:i]+word[i+1]+word[i]+word[i+2:] for i in range(n-1)] +  
  36. [word[0:i]+c+word[i+1:] for i in range(n) for c in alphabet] +  
  37. [word[0:i]+c+word[i:] for i in range(n+1) for c in alphabet])  
  38. def correct(word):  
  39. candidates1 = found(word, 'aspell -a')  
  40. if not candidates1:  
  41. print "no suggestion"  
  42. return   
  43. candidates2 = edits1(word)  
  44. candidates = []  
  45. for word in candidates1:  
  46. if word in candidates2:  
  47. candidates.append(word)  
  48. if not candidates:  
  49. print "suggestion: %s" % candidates1[0]  
  50. else:  
  51. print "suggestion: %s" % max(candidates)  
  52. def signal_handler(signal, frame):  
  53. sys.exit(0)  
  54. if __name__ == '__main__':  
  55. signal.signal(signal.SIGINT, signal_handler)  
  56. while True:  
  57. input = raw_input()  
  58. correct(input) 

The above is a solution for Python spelling check.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.