Github user mengxr commented on a diff in the pull request:
https://github.com/apache/spark/pull/5291#discussion_r27503515
--- Diff:
mllib/src/main/scala/org/apache/spark/mllib/feature/Word2Vec.scala ---
@@ -483,3 +496,66 @@ class Word2VecModel private[mllib] (
model
}
}
+
+@Experimental
+object Word2VecModel extends Loader[Word2VecModel] {
+
+ private object SaveLoadV1_0 {
+
+ val formatVersionV1_0 = "1.0"
+
+ val classNameV1_0 = "org.apache.spark.mllib.feature.Word2VecModel"
+
+ case class Data(word: String, vector: Array[Float])
+
+ def load(sc: SparkContext, path: String): Word2VecModel = {
+ val dataPath = Loader.dataPath(path)
+ val sqlContext = new SQLContext(sc)
+ val dataFrame = sqlContext.parquetFile(dataPath)
+
+ val dataArray = dataFrame.select("word", "vector").collect()
+
+ // Check schema explicitly since erasure makes it hard to use
match-case for checking.
+ Loader.checkSchema[Data](dataFrame.schema)
+
+ val word2VecMap = dataArray.map(i => (i.getString(0),
i.getSeq[Float](1).toArray)).toMap
+ return new Word2VecModel(word2VecMap)
+ }
+
+ def save(sc: SparkContext, path: String, model: Map[String,
Array[Float]]) = {
+
+ val sqlContext = new SQLContext(sc)
+ import sqlContext.implicits._
+
+ val vectorSize = model.values.head.size
+ val metadata = compact(render
+ (("class" -> classNameV1_0) ~ ("version" -> formatVersionV1_0) ~
+ ("vectorSize" -> vectorSize)))
--- End diff --
Shall we also save `numWords`?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]