我正在寻找一种相对快速的方法来检查单词是否拼写错误,无论是使用gem还是API。
我尝试过使用几种宝石 - raspell,ffi-aspell,hunspell-ffi,spell_cheker和spellchecker--每种都有不同的错误。
我对ruby很新,希望有一个简单的解决方案(我正在处理大量的短文本文件,并希望计算错误拼写的单词的百分比),这不包括从头开始构建的东西。 / p>
尝试使用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种宝石 - 所以我至少可以花时间调试最佳方案。
答案 0 :(得分: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()
假设您的路径中包含cat
,wc
和aspell
,并且默认字典是您要使用的字典。单元测试仅适用于Ruby 1.9 - 如果你运行1.8,只需将其删除即可。
答案 1 :(得分:0)
正如jmdeldin所说raspell不再被维护,ffi-aspell就是它的一个分支。
我玩了几分钟,它很容易使用:
speller.correct?(word)
speller.suggestions(word)
注意:到目前为止,我发现的更大限制是拼写器的the interface仅适用于单词。如果要拼写检查整个文档,则需要将其拆分为单词。这不可能是微不足道的,特别是如果你有一个HTML输入......
(当然,这取决于aspell所以你需要使用brew install aspell或你首选的包管理器来安装它)