2012-12-18 5 views
15

के लिए वर्तनी परीक्षक मैं पाइथन और एनएलटीके के साथ बिल्कुल नया हूं। मैं ऐसे एप्लिकेशन से व्यस्त हूं जो वर्तनी जांच कर सकता है (सही वर्तनी वाले शब्द के साथ गलत वर्तनी वाले शब्द को प्रतिस्थापित करता है), मैं वर्तमान में पाइथन-2.7, पायइन्चेंट और एनएलटीके लाइब्रेरी पर एनचेंट लाइब्रेरी का उपयोग कर रहा हूं। नीचे दिया गया कोड वह वर्ग है जो सुधार/प्रतिस्थापन को संभालता है।पायथन

from nltk.metrics import edit_distance 

class SpellingReplacer(object): 
    def __init__(self, dict_name = 'en_GB', max_dist = 2): 
     self.spell_dict = enchant.Dict(dict_name) 
     self.max_dist = 2 

    def replace(self, word): 
     if self.spell_dict.check(word): 
      return word 
     suggestions = self.spell_dict.suggest(word) 

     if suggestions and edit_distance(word, suggestions[0]) <= self.max_dist: 
      return suggestions[0] 
     else: 
      return word 

मैं एक समारोह है कि शब्दों की एक सूची में ले जाता है और करता है डीईएफ़ प्रत्येक शब्द पर बदल सकते हैं और शब्दों की एक सूची है, लेकिन वर्तनी सही लौट लिखा है।

def spell_check(word_list): 
    checked_list = [] 
    for item in word_list: 
     replacer = SpellingReplacer() 
     r = replacer.replace(item) 
     checked_list.append(r) 
    return checked_list 

>>> word_list = ['car', 'colour'] 
>>> spell_check(words) 
['car', 'color'] 

अब मैं इस तरह वास्तव में नहीं है क्योंकि यह बहुत सही नहीं है और मैं एक तरह से वर्तनी की जांच और शब्द पर प्रतिस्थापन प्राप्त करने के लिए की तलाश में हूँ। मुझे कुछ ऐसी चीज चाहिए जो "कैसर" जैसी वर्तनी की गलतियों को उठा सके? वहाँ वर्तनी जांच करने के लिए बेहतर तरीके हैं? यदि ऐसा है, तो वो क्या हैं? उदाहरण के लिए Google इसे कैसे करता है क्योंकि उनका वर्तनी सुझावक बहुत अच्छा है? कोई भी सुझाव

उत्तर

17

मैं ध्यान से this post by Peter Norvig पढ़ने से शुरू करने की सलाह दूंगा। (मैं कुछ इसी तरह किया था और मैं इसे अत्यंत उपयोगी पाया।)

निम्नलिखित समारोह, विशेष रूप से विचारों कि अब आप अपने वर्तनी परीक्षक और अधिक परिष्कृत करने की जरूरत है में:, बंटवारे, हटाने transposing, और डालने अनियमित शब्द उन्हें 'सही' करने के लिए।

def edits1(word): 
    splits  = [(word[:i], word[i:]) for i in range(len(word) + 1)] 
    deletes = [a + b[1:] for a, b in splits if b] 
    transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1] 
    replaces = [a + c + b[1:] for a, b in splits for c in alphabet if b] 
    inserts = [a + c + b  for a, b in splits for c in alphabet] 
    return set(deletes + transposes + replaces + inserts) 

ध्यान दें: ऊपर Norvig की वर्तनी पढ़नेवाला

और अच्छी खबर से एक टुकड़ा है कि आप संवर्द्धित करने के लिए जोड़ सकते हैं और अपने वर्तनी की जांच करने में सुधार रख सकते है।

उम्मीद है कि मदद करता है।

0

जादू corrector->

अगर आप कहीं और स्टोर कोड में पथ बदल मैं कुछ ग्राफिक्स को शामिल किया है और साथ ही tkinter का उपयोग कर आप अपने डेस्कटॉप पर एक कोष आयात करने की आवश्यकता है और यह केवल गैर शब्द से निपटने के लिए है त्रुटियों !!

def min_edit_dist(word1,word2): 
    len_1=len(word1) 
    len_2=len(word2) 
    x = [[0]*(len_2+1) for _ in range(len_1+1)]#the matrix whose last element ->edit distance 
    for i in range(0,len_1+1): 
     #initialization of base case values 
     x[i][0]=i 
     for j in range(0,len_2+1): 
      x[0][j]=j 
    for i in range (1,len_1+1): 
     for j in range(1,len_2+1): 
      if word1[i-1]==word2[j-1]: 
       x[i][j] = x[i-1][j-1] 
      else : 
       x[i][j]= min(x[i][j-1],x[i-1][j],x[i-1][j-1])+1 
    return x[i][j] 
from Tkinter import * 


def retrieve_text(): 
    global word1 
    word1=(app_entry.get()) 
    path="C:\Documents and Settings\Owner\Desktop\Dictionary.txt" 
    ffile=open(path,'r') 
    lines=ffile.readlines() 
    distance_list=[] 
    print "Suggestions coming right up count till 10" 
    for i in range(0,58109): 
     dist=min_edit_dist(word1,lines[i]) 
     distance_list.append(dist) 
    for j in range(0,58109): 
     if distance_list[j]<=2: 
      print lines[j] 
      print" " 
    ffile.close() 
if __name__ == "__main__": 
    app_win = Tk() 
    app_win.title("spell") 
    app_label = Label(app_win, text="Enter the incorrect word") 
    app_label.pack() 
    app_entry = Entry(app_win) 
    app_entry.pack() 
    app_button = Button(app_win, text="Get Suggestions", command=retrieve_text) 
    app_button.pack() 
    # Initialize GUI loop 
    app_win.mainloop() 
0

आप वर्तनी के बारे अजगर में जाँच autocorrect lib उपयोग कर सकते हैं।
उदाहरण उपयोग:

from autocorrect import spell 

print spell('caaaar') 
print spell(u'mussage') 
print spell(u'survice') 
print spell(u'hte') 

परिणाम:

caesar 
message 
service 
the