1

我正在尝试在字符串上运行 CRFClassifier 以从字符串中提取实体。我从这里为斯坦福 NLP 实体识别器使用 Ruby 绑定:https ://github.com/tiendung/ruby-nlp

它在自己的类(nlp.rb)上运行良好。当我运行ruby nlp.rb它工作正常。但是,我尝试在我的 rails 应用程序的一个控制器中创建此类的对象,但由于某种原因,我收到以下错误:

java.lang.NoClassDefFoundError: edu/stanford/nlp/ie/crf/CRFClassifier

这是可以自己正常工作但不在控制器内部的代码。

    def initialize
        Rjb::load('stanford-postagger.jar:stanford-ner.jar', ['-Xmx200m'])
        crfclassifier = Rjb::import('edu.stanford.nlp.ie.crf.CRFClassifier')
        maxentTagger = Rjb::import('edu.stanford.nlp.tagger.maxent.MaxentTagger')
        maxentTagger.init("left3words-wsj-0-18.tagger")
        sentence = Rjb::import('edu.stanford.nlp.ling.Sentence')
        @classifier = crfclassifier.getClassifierNoExceptions("ner-eng-ie.crf-4-conll.ser.gz")


    end


    def get_entities(sentence)
        sent = sentence
        @classifier.testStringInlineXML( sent )

    end

两种情况下的代码完全相同。任何人都知道这里发生了什么!?

提前致谢!

4

1 回答 1

1

我认为你需要这个:

rjb::load('/path/to/jar/stanford-postagger.jar:/path/to/jar/stanford-ner.jar', ['-Xmx200m'])

我刚试过这个,它有效。在 lib 中创建一个名为 nlp 的目录。将罐子放在那里,然后创建一个使用完整路径加载罐子的类:

所以你最终得到:

├── lib
│   ├── nlp
│   │   ├── stanford-ner.jar
│   │   └── stanford-postagger.jar
│   └── nlp.rb



require 'rjb'

class NLP
  def initialize
    pos_tagger = File.expand_path('../nlp/stanford-postagger.jar', __FILE__)
    ner = File.expand_path('../nlp/stanford-ner.jar', __FILE__)
    Rjb::load("#{pos_tagger}:#{ner}", ['-Xmx200m'])
    crfclassifier = Rjb::import('edu.stanford.nlp.ie.crf.CRFClassifier')
    maxentTagger = Rjb::import('edu.stanford.nlp.tagger.maxent.MaxentTagger')
    maxentTagger.init("left3words-wsj-0-18.tagger")
    sentence = Rjb::import('edu.stanford.nlp.ling.Sentence')
    @classifier = crfclassifier.getClassifierNoExceptions("ner-eng-ie.crf-4-conll.ser.gz")
  end


  def get_entities(sentence)
    sent = sentence
    @classifier.testStringInlineXML( sent )
  end
end

小试课:

require_relative 'lib/nlp'

n = NLP.new
n.get_entities("Good afternoon Rajat Raina, how are you today?")

输出:

ruby t.rb
Loading classifier from /Users/brendan/code/ruby/ruby-nlp/ner-eng-ie.crf-4-conll.ser.gz ... done [1.2 sec].
Getting data from Good afternoon Rajat Raina, how are you today? (default encoding)
Good afternoon <PERSON>Rajat Raina</PERSON>, how are you today?
于 2014-01-24T21:41:22.983 回答