在jruby中实现自定义Java类 [英] implementing custom java class in jruby

查看:55
本文介绍了在jruby中实现自定义Java类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从斯坦福NLP解析器中实现Java类的集合在jRuby中

I am trying to implement a collection of java classes from the Stanford NLP Parser in jRuby

我能够在jRuby中实现常规Java,但无法在Stanford Parser类中实现

I am able to implement regular Java in jRuby, but not the Stanford Parser classes

#my requires/imports/includes, included multiple versions to be safe
require 'java'
include Java
require 'c:\\stanford-parser\\stanfordparser.jar'
require 'C:\\Stanford-Parser\\Current\\stanford-parser.jar'
require 'c:\\sun\\stanfordparser'
require 'rubygems'
include_class 'edu.stanford.nlp.parser.lexparser.LexicalizedParser'


#try to create an object of the java class i am importing, LexicalizedParser
lp = edu.stanford.nlp.parser.lexparser.LexicalizedParser
#the line above is what causes the error

#check if regular Java is working
list = java.util.ArrayList.new 
a = "1"
b = "2"
list.add(a)
list.add(b)
d = list[0]
puts d # all of this works

我收到此错误

~\rubyjavatest\lib\main.rb:15: undefined local variable or method `edu' for main:Object (NameError)

(〜代表我切断了整个路径以缩短此时间)

(the ~ represents I cut out the whole path to shorten this)

如果我尝试此操作,也可以:

also if I try this:

lp = java::edu::stanford::nlp.parser::lexparser::LexicalizedParser

我收到此错误

~\rubyjavatest\lib\main.rb:15: cannot load Java class java.edu.stanford.nlp.parser.lexparser.LexicalizedParser (NameError)

任何帮助都会很棒!

推荐答案

尝试一下:lp = LexicalizedParser.new

您需要像使用ArrrayList一样调用new.另外,在调用include_class之后,您无需列出完全限定的类名.

You need to call new like you did with ArrrayList. Also, you shouldn't need to list the fully qualified class name after you call include_class.

我对Stanford NLP解析器不熟悉,因此我认为这是可行的.可能有必要将其他参数传递给构造函数.

I am not familiar with the Stanford NLP Parser, so I am assuming this will work. It might be necessary to pass additional parameters to the constructor.

这篇关于在jruby中实现自定义Java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆