自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python 拼寫檢查如何更簡單的使用

開發(fā) 后端
Python 拼寫檢查在使用的時候需要我們不斷的學(xué)習(xí),下面我們就看看如何才能掌握這項技能。希望大家有所收獲。

Python 拼寫檢查在使用的時候有些問題一直在困擾著我們。其實只有不斷的學(xué)習(xí)才能更好的使用這門語言。這幾天在翻舊代碼時發(fā)現(xiàn)以前寫的注釋部分有很多單詞拼寫錯誤,這些單詞錯得不算離譜,應(yīng)該可以用工具自動糾錯絕大部分。

Python 拼寫檢查腳本很容易,如果能很好利用 aspell/ispell 這些現(xiàn)成的小工具就更簡單了。

Google 大牛 Peter Norvig 寫了一篇 How to Write a Spelling Corrector 很值得一看,大牛就是大牛,21行 Python拼寫檢查問題,而且還不用外部工具,只需要事先讀入一個詞典文件。本文程序的 edits1 函數(shù)就是從牛人家那里 copy 的。

  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 = Noneshell = 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) 

以上就是對Python 拼寫檢查的相關(guān)解決方案。

【編輯推薦】

  1. Python編程語言維和受到眾人的追捧
  2. Python輸入方式具體的三種實現(xiàn)方式
  3. Python正則表達(dá)式如何刪除代碼行
  4. Python字符串如何進(jìn)行代碼替換
  5. Python腳本在其他語言環(huán)境中的應(yīng)用方案
責(zé)任編輯:張浩 來源: IT168
相關(guān)推薦

2016-11-14 15:02:28

拼寫檢查安全

2009-12-08 19:34:26

PHP拼寫檢查函數(shù)庫

2011-03-31 11:15:57

JavaGoogle API

2009-09-23 10:14:22

Hibernate

2020-07-28 15:20:43

PythonUI代碼

2019-04-04 14:05:20

consolejs前端

2021-06-29 15:52:03

PythonPOST

2024-04-11 11:37:25

人工智能機(jī)器學(xué)習(xí)自動化流程

2019-06-18 07:15:22

Linux拼寫look命令

2024-02-27 19:22:00

cookieStorCookie事件

2022-08-29 18:34:46

Pythonsubprocess系統(tǒng)

2013-04-26 11:17:48

2012-09-25 09:28:36

程序員代碼代碼整潔

2020-06-16 13:22:22

AI創(chuàng)新深度學(xué)習(xí)

2019-08-07 12:40:57

Linux命令存儲性能

2024-01-17 17:36:06

Linuxsystemd

2021-12-21 21:58:24

數(shù)字故宮小程序

2021-10-07 11:02:25

微軟Edge瀏覽器

2022-10-31 07:09:15

拷貝代碼項目
點贊
收藏

51CTO技術(shù)棧公眾號