执行Open-NLP Ruby Gem时出错

时间:2014-07-09 11:07:11

标签: ruby binding opennlp

我们尝试根据https://github.com/louismullie/open-nlp/blob/master/README.md执行的ruby代码:

#gem inclusion

require 'open-nlp'

# Set an alternative path to look for the JAR files.
# Default is gem's bin folder.
OpenNLP.jar_path = '/home/USER/.rvm/gems/ruby-2.1.2/gems/open-nlp-0.1.5/bin/'

# Set an alternative path to look for the model files.
# Default is gem's bin folder.
OpenNLP.model_path = '/home/USER/.rvm/gems/ruby-2.1.2/gems/open-nlp-0.1.5/bin/'

# Pass some alternative arguments to the Java VM.
# Default is ['-Xms512M', '-Xmx1024M'].
OpenNLP.jvm_args = ['-Xms512M', '-Xmx1024M']

# Redirect VM output to log.txt
OpenNLP.log_file = 'log.txt'

# Set default models for a language.
OpenNLP.use :language

#Maximum entropy tokenizer, chunker and POS tagger
OpenNLP.load

chunker   = OpenNLP::ChunkerME.new
tokenizer = OpenNLP::TokenizerME.new
tagger    = OpenNLP::POSTaggerME.new

sent   = "The death of the poet was kept from his poems."

tokens = tokenizer.tokenize(sent).to_a
# => %w[The death of the poet was kept from his poems .]

tags   = tagger.tag(tokens).to_a
# => %w[DT NN IN DT NN VBD VBN IN PRP$ NNS .]

chunks = chunker.chunk(tokens, tags).to_a
# => %w[B-NP I-NP B-PP B-NP I-NP B-VP I-VP B-PP B-NP I-NP O]

这就是输出:

/home/USER/.rvm/gems/ruby-2.1.2/gems/open-nlp-0.1.5/lib/open-nlp/bindings.rb:125:in `+': no implicit conversion of nil into String (TypeError)
    from /home/USER/.rvm/gems/ruby-2.1.2/gems/open-nlp-0.1.5/lib/open-nlp/bindings.rb:125:in `load_model'
    from /home/USER/.rvm/gems/ruby-2.1.2/gems/open-nlp-0.1.5/lib/open-nlp/bindings.rb:110:in `get_model'
    from /home/USER/.rvm/gems/ruby-2.1.2/gems/open-nlp-0.1.5/lib/open-nlp/base.rb:13:in `initialize'
    from opennlp_example.rb:26:in `new'
    from opennlp_example.rb:26:in `<main>'

因为它只是示例(Simple tokenizer示例运行正常)...我们希望有人了解更多信息

1 个答案:

答案 0 :(得分:1)

查看code似乎OpenNLP::Config::DefaultModels[name][OpenNLP.language]nil

  def self.load_model(name, file = nil)
    if self.models[name] && file ==
      self.model_files[name]
      return self.models[name]
    end
    models = OpenNLP::Config::DefaultModels[name]
    file ||= models[self.language] # <- this is the problematic line
    path = self.model_path + file
    stream = FileInputStream.new(path)

我怀疑检查OpenNLP.language会产生:language,来自以下行:

# Set default models for a language.
OpenNLP.use :language

由于use的实施是:

  def self.use(language)
    self.language = language
  end

底线 - 尝试从代码中删除OpenNLP.use :language,或将其更改为OpenNLP.use :english