Roh*_*ain 3 nlp scala stanford-nlp lda topic-modeling
我一直在尝试使用 TMT 工具箱(stanford nlp group)从训练有素的标记 LDA 模型和 pLDA 进行推理代码。我已经浏览了以下链接中提供的示例:http : //nlp.stanford.edu/software/tmt/tmt-0.3/ http://nlp.stanford.edu/software/tmt/tmt-0.4/
这是我正在尝试用于标记 LDA 推理的代码
val modelPath = file("llda-cvb0-59ea15c7-31-61406081-75faccf7");
val model = LoadCVB0LabeledLDA(modelPath);`
val source = CSVFile("pubmed-oa-subset.csv") ~> IDColumn(1);
val text = {
source ~> // read from the source file
Column(4) ~> // select column containing text
TokenizeWith(model.tokenizer.get) //tokenize with model's tokenizer
}
val labels = {
source ~> // read from the source file
Column(2) ~> // take column two, the year
TokenizeWith(WhitespaceTokenizer())
}
val outputPath = file(modelPath, source.meta[java.io.File].getName.replaceAll(".csv",""));
val dataset = LabeledLDADataset(text,labels,model.termIndex,model.topicIndex);
val perDocTopicDistributions = InferCVB0LabeledLDADocumentTopicDistributions(model, dataset);
val perDocTermTopicDistributions =EstimateLabeledLDAPerWordTopicDistributions(model, dataset, perDocTopicDistributions);
TSVFile(outputPath+"-word-topic-distributions.tsv").write({
for ((terms,(dId,dists)) <- text.iterator zip perDocTermTopicDistributions.iterator) yield {
require(terms.id == dId);
(terms.id,
for ((term,dist) <- (terms.value zip dists)) yield {
term + " " + dist.activeIterator.map({
case (topic,prob) => model.topicIndex.get.get(topic) + ":" + prob
}).mkString(" ");
});
}
});
Run Code Online (Sandbox Code Playgroud)
错误
found : scalanlp.collection.LazyIterable[(String, Array[Double])]
required: Iterable[(String, scalala.collection.sparse.SparseArray[Double])]
EstimateLabeledLDAPerWordTopicDistributions(model, dataset, perDocTopicDistributions);
我知道这是一个类型不匹配错误。但我不知道如何为 Scala 解决这个问题。基本上我不明白我应该如何在 infer 命令的输出之后提取 1. per doc 主题分布 2. per doc 标签分布。
请帮忙。在 pLDA 的情况下也是如此。我到达了推理命令,然后就无能为力了。
Scala 类型系统比 Java 复杂得多,理解它会让你成为更好的程序员。问题出在这里:
val perDocTermTopicDistributions =EstimateLabeledLDAPerWordTopicDistributions(model, dataset, perDocTopicDistributions);
Run Code Online (Sandbox Code Playgroud)
因为模型、数据集或 perDocTopicDistributions 属于以下类型:
scalanlp.collection.LazyIterable[(String, Array[Double])]
Run Code Online (Sandbox Code Playgroud)
而 EstimateLabeledLDAPerWordTopicDistributions.apply 期望
Iterable[(String, scalala.collection.sparse.SparseArray[Double])]
Run Code Online (Sandbox Code Playgroud)
调查这种类型错误的最好方法是查看 ScalaDoc(例如 tmt 的那个:http ://nlp.stanford.edu/software/tmt/tmt-0.4/api/#package ),如果你无法轻松找出问题所在,您应该在代码中明确变量的类型,如下所示:
val perDocTopicDistributions:LazyIterable[(String, Array[Double])] = InferCVB0LabeledLDADocumentTopicDistributions(model, dataset)
Run Code Online (Sandbox Code Playgroud)
如果我们一起看 edu.stanford.nlp.tmt.stage 的 javadoc:
def
EstimateLabeledLDAPerWordTopicDistributions (model: edu.stanford.nlp.tmt.model.llda.LabeledLDA[_, _, _], dataset: Iterable[LabeledLDADocumentParams], perDocTopicDistributions: Iterable[(String, SparseArray[Double])]): LazyIterable[(String, Array[SparseArray[Double]])]
def
InferCVB0LabeledLDADocumentTopicDistributions (model: CVB0LabeledLDA, dataset: Iterable[LabeledLDADocumentParams]): LazyIterable[(String, Array[Double])]
Run Code Online (Sandbox Code Playgroud)
您现在应该很清楚 InferCVB0LabeledLDADocumentTopicDistributions 的返回值不能直接用于提供 EstimateLabeledLDAPerWordTopicDistributions。
我从未使用过 stanford nlp,但这是 api 的设计方式,因此您只需要在调用该函数之前将其转换scalanlp.collection.LazyIterable[(String, Array[Double])]为Iterable[(String, scalala.collection.sparse.SparseArray[Double])]。
如果您查看有关如何进行此转换的 scaladoc,则它非常简单。在 package 阶段,在 package.scala 中我可以阅读 import scalanlp.collection.LazyIterable;
所以我知道在哪里看,事实上在http://www.scalanlp.org/docs/core/data/#scalanlp.collection.LazyIterable你有一个 toIterable 方法,它把 LazyIterable 变成一个 Iterable,你仍然必须将您的内部数组转换为 SparseArray
再次,我查看了 tmt 中 stage 包的 package.scala,我看到:import scalala.collection.sparse.SparseArray;我寻找 scalala 文档:
http://www.scalanlp.org/docs/scalala/0.4.1-SNAPSHOT/#scalala.collection.sparse.SparseArray
事实证明,构造函数对我来说似乎很复杂,所以听起来我必须查看工厂方法的伴随对象。事实证明,我正在寻找的方法就在那里,它在 Scala 中像往常一样被称为 apply。
def
apply [T] (values: T*)(implicit arg0: ClassManifest[T], arg1: DefaultArrayValue[T]): SparseArray[T]
Run Code Online (Sandbox Code Playgroud)
通过使用它,您可以编写具有以下签名的函数:
def f: Array[Double] => SparseArray[Double]
Run Code Online (Sandbox Code Playgroud)
完成此操作后,您可以使用一行代码将 InferCVB0LabeledLDADocumentTopicDistributions 的结果转换为稀疏数组的非延迟迭代:
result.toIterable.map { case (name, values => (name, f(values)) }
Run Code Online (Sandbox Code Playgroud)