2012-07-06 19 views
9

मैं यह देखने के लिए अपेक्षाकृत तेज़ तरीका ढूंढ रहा हूं कि शब्द गलत वर्तनी हैं या तो एक मणि या एपीआई का उपयोग कर रहे हैं।रूबी में सरल वर्तनी जांच विधि/मणि?

मैंने कई रत्नों - रास्पेल, एफएफआई-एस्पेल, हुनस्पेल-एफएफआई, स्पेल_चेकर, और स्पेलचेकर का उपयोग करने की कोशिश की है - और प्रत्येक में एक अलग त्रुटि है।

मैं रूबी के लिए बहुत नया हूं और एक साधारण समाधान की उम्मीद कर रहा हूं (मैं बहुत छोटी टेक्स्ट फाइलों को संसाधित कर रहा हूं और गलत शब्दों के शब्दों की गणना करना चाहता हूं) जिसमें स्क्रैच से कुछ निर्माण शामिल नहीं है।

जब ffi-aspell की कोशिश कर रहा है, मैं निम्नलिखित त्रुटि मिलती है:

/Users/ntaylorthompson/.rvm/gems/ruby-1.9.2-p320/gems/ffi-aspell-0.0.3/lib/ffi/aspell/speller.rb:121: [BUG] Segmentation fault 
ruby 1.9.2p320 (2012-04-20 revision 35421) [x86_64-darwin11.4.0] 

-- control frame ---------- 
c:0005 p:---- s:0019 b:0019 l:000018 d:000018 CFUNC :speller_check 
c:0004 p:0113 s:0013 b:0013 l:000012 d:000012 METHOD /Users/ntaylorthompson/.rvm/gems/ruby-1.9.2-p320/gems/ffi-aspell-0.0.3/lib/ffi/aspell/speller.rb:121 
c:0003 p:0049 s:0007 b:0007 l:0005a8 d:0005d0 EVAL ffi-aspell_test.rb:5 
c:0002 p:---- s:0004 b:0004 l:000003 d:000003 FINISH 
c:0001 p:0000 s:0002 b:0002 l:0005a8 d:0005a8 TOP 
--------------------------- 
-- Ruby level backtrace information ---------------------------------------- 
ffi-aspell_test.rb:5:in `<main>' 
/Users/ntaylorthompson/.rvm/gems/ruby-1.9.2-p320/gems/ffi-aspell-0.0.3/lib/ffi/aspell/speller.rb:121:in `correct?' 
/Users/ntaylorthompson/.rvm/gems/ruby-1.9.2-p320/gems/ffi-aspell-0.0.3/lib/ffi/aspell/speller.rb:121:in `speller_check' 

-- C level backtrace information ------------------------------------------- 

[NOTE] 
You may have encountered a bug in the Ruby interpreter or extension libraries. 
Bug reports are welcome. 
For details: http://www.ruby-lang.org/bugreport.html 

Abort trap: 6 

मैं इसकी सराहना करेंगे तो (1) के ऊपर या (2) एक सिफारिश उन लोगों के लिए एक वैकल्पिक दृष्टिकोण का एक सुझाव जिनमें से उपयोग करने के लिए उपरोक्त 5 रत्नों में से - इसलिए मैं कम से कम समय को सर्वश्रेष्ठ विकल्प डिबग करने में व्यतीत कर सकता हूं।

+1

क्या है त्रुटियों आप कर रहे हैं मिल रहा? क्या आपके पास एस्पेल रत्न के लिए एस्पेल स्थापित है? – jmdeldin

+0

मैंने होमब्री के साथ एस्पेल स्थापित किया, और मैंने अभी ऊपर एफएफआई-एस्पेल त्रुटि पोस्ट की है - कोई अंतर्दृष्टि? – TaylorT

उत्तर

6

रास्पेल अब बनाए रखा नहीं गया है, इसलिए यदि आपके पास libaspell शीर्षलेख उपलब्ध हैं तो ffi-aspell एक अच्छा विकल्प है।

यदि आप पुस्तकालयों को काम करने के लिए नहीं मिल पा रहे हैं, तो आप केवल aspell बाइनरी तक पहुंच सकते हैं। निम्न विधि सिर्फ इतना है कि (इकाई परीक्षण शामिल है) करना होगा:

# Returns the percentage of incorrect words per document 
# 
def spellcheck(filename) 
    fail "File #{filename} does not exist" unless File.exists?(filename) 

    words = Float(`wc -w #{filename}`.split.first) 
    wrong = Float(`cat #{filename} | aspell --list | wc -l`.split.first) 

    wrong/words 
end 

if $0 == __FILE__ 
    require 'minitest/autorun' 
    require 'tempfile' 

    describe :spellcheck do 
    def write(str) 
     @file.write str 
     @file.read 
    end 

    before do 
     @file = Tempfile.new('document') 
    end 

    it 'fails when given a bad path' do 
     -> { spellcheck('/tmp/does/not/exist') }.must_raise RuntimeError 
    end 

    it 'returns 0.0 if there are no misspellings' do 
     write 'The quick brown fox' 
     spellcheck(@file.path).must_equal 0.0 
    end 

    it 'returns 0.5 if 2/4 words are misspelled' do 
     write 'jumped over da lacie' 
     spellcheck(@file.path).must_be_close_to 0.5, 1e-8 
    end 

    it 'returns 1.0 if everything is misspelled' do 
     write 'Da quyck bown foxx jmped oer da lassy dogg' 
     spellcheck(@file.path).must_equal 1.0, 1e-8 
    end 

    after do 
     @file.close 
     @file.unlink 
    end 
    end 
end 

spellcheck() मान लिया गया है कि आप अपने रास्ते पर cat, wc, और aspell है, और डिफ़ॉल्ट शब्दकोश क्या आप उपयोग करना चाहते है। इकाई परीक्षण केवल रूबी 1.9 के लिए है - यदि आप 1.8 चला रहे हैं, तो बस इसे हटाएं।

+0

बहुत बहुत धन्यवाद! मैं एफएफआई-एस्पेल और काम करने के लिए ऊपर की विधि दोनों प्राप्त करने में सक्षम था। यह पता चला है कि मैं शब्दकोश स्थापित किए बिना एस्पेल स्थापित किया है (- lang = en निर्दिष्ट करके)। – TaylorT

0

जैसा कि jmdeldin ने कहा रास्पेल अब बनाए रखा नहीं है, ffi-aspell इसका एक कांटा है।

मैं इसके साथ कुछ मिनट खेला और इसका इस्तेमाल करने के लिए काफी आसान है:

  1. भाषा निर्दिष्ट
  2. चेक अगर एक शब्द speller.correct?(word)
  3. का उपयोग कर सही है एक FFI :: Aspell :: स्पेलर वस्तु को दर्शाता है एक शब्द का उपयोग कर speller.suggestions(word)

नोट के लिए सुझावों की एक सूची प्राप्त करें: मैं बड़ा सीमा ' अब तक पाया गया है कि स्पेलर के the interface केवल शब्दों पर काम करता है। यदि आप एक संपूर्ण दस्तावेज़ की जांच करना चाहते हैं तो आपको इसे शब्दों में विभाजित करने की आवश्यकता होगी। यह तुच्छ नहीं हो सकता है, खासकर यदि आप एक HTML इनपुट है ...

(यह निश्चित रूप से aspell पर निर्भर करता है, ताकि आप स्थापित यह काढ़ा स्थापित aspell या अपने पसंदीदा पैकेज प्रबंधक का उपयोग करने की आवश्यकता है)

संबंधित मुद्दे