Jen*_*ger 1 java scala deserialization
我是一个Scala/Java noob,很抱歉,如果这是一个相对简单的解决方案 - 但我正在尝试访问外部文件中的模型(Apache Open NLP模型),并且不确定我哪里出错了.以下是您在Java中的使用方法,以下是我正在尝试的内容:
import java.io._
val nlpModelPath = new java.io.File( "." ).getCanonicalPath + "/lib/models/en-sent.bin"
val modelIn: InputStream = new FileInputStream(nlpModelPath)
Run Code Online (Sandbox Code Playgroud)
哪个工作正常,但尝试基于该二进制文件中的模型实例化对象是我失败的地方:
val sentenceModel = new modelIn.SentenceModel // type SentenceModel is not a member of java.io.InputStream
val sentenceModel = new modelIn("SentenceModel") // not found: type modelIn
Run Code Online (Sandbox Code Playgroud)
我也尝试了一个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
Run Code Online (Sandbox Code Playgroud)
我不确定我缺少什么 - 也许是一些方法将Stream转换为一些二进制对象,我可以从中提取方法?谢谢你的任何指示.
问题是你使用了错误的语法(请不要把它当作个人使用,但是如果你打算坚持使用java或scala一段时间,为什么不先阅读一些初学java书籍,或者甚至只读一本教程? )
你将用java编写的代码
SentenceModel model = new SentenceModel(modelIn);
Run Code Online (Sandbox Code Playgroud)
在scala中看起来类似:
val model: SentenceModel = new SentenceModel(modelIn)
// or just
val model = new SentenceModel(modelIn)
Run Code Online (Sandbox Code Playgroud)
你用这种语法得到的问题是你忘了导入SentenceModel的定义,所以编译器根本不知道什么是SentenceModel.
加
import opennlp.tools.sentdetect.SentenceModel
Run Code Online (Sandbox Code Playgroud)
在.scala文件的顶部,这将解决它.