Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import java.util.{HashMap => JHashMap}
import org.apache.hadoop.fs.Path

import org.apache.spark.annotation.Since
import org.apache.spark.broadcast.Broadcast
import org.apache.spark.ml.{Estimator, Model}
import org.apache.spark.ml.attribute.{Attribute, AttributeGroup, NumericAttribute}
import org.apache.spark.ml.linalg.{SQLDataTypes, Vectors}
Expand Down Expand Up @@ -297,31 +296,25 @@ class CountVectorizerModel(
@Since("2.0.0")
def setBinary(value: Boolean): this.type = set(binary, value)

/** Dictionary created from [[vocabulary]] and its indices, broadcast once for [[transform()]] */
private var broadcastDict: Option[Broadcast[JHashMap[String, Integer]]] = None

@Since("2.0.0")
override def transform(dataset: Dataset[_]): DataFrame = {
val outputSchema = transformSchema(dataset.schema, logging = true)
if (broadcastDict.isEmpty) {
val dict = new JHashMap[String, Integer](math.ceil(vocabulary.length / 0.75).toInt)
var index = 0
while (index < vocabulary.length) {
dict.put(vocabulary(index), index)
index += 1
}
broadcastDict = Some(dataset.sparkSession.sparkContext.broadcast(dict))
val dict = new JHashMap[String, Integer](math.ceil(vocabulary.length / 0.75).toInt)
var index = 0
while (index < vocabulary.length) {
dict.put(vocabulary(index), index)
index += 1
}
val dictBr = broadcastDict.get
val bcDict = dataset.sparkSession.sparkContext.broadcast(dict)
val localMinTF = $(minTF)
val localBinary = $(binary)
val vectorizer = udf { document: Seq[String] =>
val dict = dictBr.value
val dictSize = dict.size()
val localDict = bcDict.value
val dictSize = localDict.size()
val termCounts = new OpenHashMap[Int, Int]
var tokenCount = 0L
document.foreach { term =>
val index = dict.get(term)
val index = localDict.get(term)
if (index != null) {
termCounts.changeValue(index.intValue(), 1, _ + 1)
}
Expand Down