日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python拼寫檢查如何更簡單的使用

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

專注于為中小企業(yè)提供成都網(wǎng)站制作、網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)密云免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千多家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

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

Google 大牛 Peter Norvig 寫了一篇 How to Write a Spelling Corrector 很值得一看,大牛就是大牛,21行 Python拼寫檢查問題,而且還不用外部工具,只需要事先讀入一個(gè)詞典文件。本文程序的 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 = 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)

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


文章名稱:Python拼寫檢查如何更簡單的使用
網(wǎng)站路徑:http://www.5511xx.com/article/coidgoc.html