2012-07-06 155 views
9

我正在尋找一種相對較快的方法來檢查單詞是否拼寫錯誤,無論是使用gem還是API。簡單的拼寫檢查方法/寶石紅寶石?

我試過使用幾種寶石 - raspell,ffi-aspell,hunspell-ffi,spell_cheker和spellchecker - 並且每個都有不同的錯誤。

我對ruby很陌生,希望得到一個簡單的解決方案(我正在處理很多簡短的文本文件,並且想要計算錯誤拼寫的單詞的百分比),它不包括從頭開始構建的東西。

當試圖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

什麼錯誤是你得到些什麼?你有沒有爲aspell寶石安裝aspell? – jmdeldin 2012-07-07 20:43:21

+0

我使用Homebrew安裝了Aspell,並且剛剛發佈了上述ffi-aspell錯誤 - 任何見解? – TaylorT 2012-07-09 14:03:26

回答

6

raspell不再維護,因此如果您有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()假設你在道路上有catwcaspell,以及默認的字典是要使用什麼。單元測試僅適用於Ruby 1.9 - 如果您運行的是1.8,只需將其刪除即可。

+0

非常感謝!我能夠同時獲得ffi-aspell和上述方法。事實證明,我不用安裝字典就可以安裝aspell(通過指定--lang = en)。 – TaylorT 2012-07-09 14:38:14

0

由於jmdeldin說raspell不再維護,ffi-aspell是它的一個分支。

我打幾分鐘,它和它很容易使用:

  1. 實例化一個FFI ::安博泰::拼寫對象,指定的語言
  2. 檢查一個字使用speller.correct?(word)
  3. 是正確的獲取的建議列表使用speller.suggestions(word)

注意一句話:更大的限制我到目前爲止發現的是拼寫器的the interface只能用於單詞。如果你想拼寫檢查整個文件,你需要用文字分割。這不可能是微不足道的,特別是如果你有一個HTML輸入...

(這取決於當然的aspell,所以你需要安裝它使用BREW的aspell安裝或首選的軟件包管理器)