com.mayabot.nlp.segment.plugins.pos.CommonPosModel.kt Maven / Gradle / Ivy
package com.mayabot.nlp.segment.plugins.pos
import com.mayabot.nlp.perceptron.PerceptronModel
import com.mayabot.nlp.perceptron.PerceptronRunner
import java.io.File
/**
* 通用的词性标注。
* 格式 word/pos word/pos
*/
open class CommonPosModel(val labels: Array,
val perceptron: PerceptronModel) {
init {
perceptron.decodeQuickMode(true)
}
protected val runner = PerceptronRunner(PosPerceptronDef(labels))
/**
* 解码
*/
fun decodeWithIndex(list: List): IntArray {
return runner.decode(perceptron, list)
}
fun save(dir: File) {
perceptron.save(dir)
}
fun learn(sample: String) {
runner.learnModel(perceptron,sample)
}
/**
* 解码
*/
fun decode(list: List): List {
val decodeResult = runner.decode(perceptron, list)
return decodeResult.map { labels[it] }
}
companion object {
fun train(labels: List,
trainFile: File,
evaluateFile: File?,
iter: Int,
threadNum: Int): PerceptronModel {
val runner = PerceptronRunner(PosPerceptronDef(labels.toTypedArray()))
return runner.train(trainFile, evaluateFile, iter, threadNum, true)
}
}
}