`
jsntghf
  • 浏览: 2478370 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

英语单词拼写检查

阅读更多

首先,你需要去http://wordnet.princeton.edu/wordnet/download/下载一个WordNet,并进行安装。WordNet是一个大型的英语单词数据库。检查单词拼写是否正确也就是依赖这个库实现的。

 

下面我们来看一下具体实现。

 

首先写一个类。

 

  class Check
    def initialize()
      dic_path = 'D:/software/WordNet/dict/index.sense'
      @words = []
      @words = load_dic(dic_path)
    end

    def load_dic(dic_path)
      words =[]
      File.open(dic_path, 'r') do |f|
        while line = f.gets
          words << line[/.[^\%]+/]
        end
      end

      words.uniq
    end

    def is_word?(word)
      @words.include? word
    end
  end

 

我们可以像下面这样来用它:

 

  if __FILE__ == $0
    check = Check.new
    word = ARGV[0]
    if check.is_word? word
      puts "correctly"
    else
      puts "wrong"
    end
  end

 

最后,我们看一下运行结果:

 

ruby check.rb first    # => correctly
ruby check.rb first0   # => wrong

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics