forked from alteryx/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-12016] [MLLIB] [PYSPARK] Wrap Word2VecModel when loading it in…
… pyspark JIRA: https://issues.apache.org/jira/browse/SPARK-12016 We should not directly use Word2VecModel in pyspark. We need to wrap it in a Word2VecModelWrapper when loading it in pyspark. Author: Liang-Chi Hsieh <[email protected]> Closes apache#10100 from viirya/fix-load-py-wordvecmodel.
- Loading branch information
Showing
3 changed files
with
67 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
mllib/src/main/scala/org/apache/spark/mllib/api/python/Word2VecModelWrapper.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.mllib.api.python | ||
|
||
import java.util.{ArrayList => JArrayList, List => JList, Map => JMap} | ||
import scala.collection.JavaConverters._ | ||
|
||
import org.apache.spark.SparkContext | ||
import org.apache.spark.api.java.JavaRDD | ||
import org.apache.spark.mllib.feature.Word2VecModel | ||
import org.apache.spark.mllib.linalg.{Vector, Vectors} | ||
|
||
/** | ||
* Wrapper around Word2VecModel to provide helper methods in Python | ||
*/ | ||
private[python] class Word2VecModelWrapper(model: Word2VecModel) { | ||
def transform(word: String): Vector = { | ||
model.transform(word) | ||
} | ||
|
||
/** | ||
* Transforms an RDD of words to its vector representation | ||
* @param rdd an RDD of words | ||
* @return an RDD of vector representations of words | ||
*/ | ||
def transform(rdd: JavaRDD[String]): JavaRDD[Vector] = { | ||
rdd.rdd.map(model.transform) | ||
} | ||
|
||
def findSynonyms(word: String, num: Int): JList[Object] = { | ||
val vec = transform(word) | ||
findSynonyms(vec, num) | ||
} | ||
|
||
def findSynonyms(vector: Vector, num: Int): JList[Object] = { | ||
val result = model.findSynonyms(vector, num) | ||
val similarity = Vectors.dense(result.map(_._2)) | ||
val words = result.map(_._1) | ||
List(words, similarity).map(_.asInstanceOf[Object]).asJava | ||
} | ||
|
||
def getVectors: JMap[String, JList[Float]] = { | ||
model.getVectors.map({case (k, v) => (k, v.toList.asJava)}).asJava | ||
} | ||
|
||
def save(sc: SparkContext, path: String): Unit = model.save(sc, path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters