【发布时间】:2012-12-10 13:00:12
【问题描述】:
我是 Scala/Java 菜鸟,很抱歉,如果这是一个相对简单的解决方案 - 但我正在尝试访问外部文件中的模型(Apache Open NLP 模型),但不确定我在哪里我出错了。 Here's how you'd do it in Java,这就是我正在尝试的:
import java.io._
val nlpModelPath = new java.io.File( "." ).getCanonicalPath + "/lib/models/en-sent.bin"
val modelIn: InputStream = new FileInputStream(nlpModelPath)
效果很好,但是尝试根据该二进制文件中的模型实例化对象是我失败的地方:
val sentenceModel = new modelIn.SentenceModel // type SentenceModel is not a member of java.io.InputStream
val sentenceModel = new modelIn("SentenceModel") // not found: type modelIn
我也尝试过 DataInputStream:
val file = new File(nlpModelPath)
val dis = new DataInputStream(file)
val sentenceModel = dis.SentenceModel() // value SentenceModel is not a member of java.io.DataInputStream
我不确定我缺少什么——也许是某种将 Stream 转换为可以从中提取方法的二进制对象的方法?感谢您的任何指点。
【问题讨论】:
-
你为什么不像在java中那样做呢?例如
val model = new SentenceModel(modelIn)? -
我应该提到我试过了,结果:
not found: type SentenceModel -
在此之前,您必须在源文件顶部添加
import opennlp.tools.sentdetect.SentenceModel(假设您的类路径中已经有 opennlp)。 -
啊!太感谢了。那行得通。那么,你是怎么找到opennlp的包结构的呢?
标签: java scala deserialization