Github user ygcao commented on a diff in the pull request: https://github.com/apache/spark/pull/10152#discussion_r46921737 --- Diff: mllib/src/main/scala/org/apache/spark/mllib/feature/Word2Vec.scala --- @@ -469,7 +469,32 @@ class Word2VecModel private[spark] ( this(Word2VecModel.buildWordIndex(model), Word2VecModel.buildWordVectors(model)) } - private def cosineSimilarity(v1: Array[Float], v2: Array[Float]): Double = { + /** + * get cosineSimilarity of two word. assumed to be from the vocabulary and used after model built, otherwise will error out + * @param v1 one word from the vocabulary + * @param v2 the other word from the vocabulary + * @return the cosinesimilarity score in the vector space of the given two words + */ + def cosineSimilarity(v1: String, v2: String): Double = { + return cosineSimilarity(getVectors(v1), getVectors(v2)) + } + + /** + * get the built vocabulary from the input + * @return a map of word to its index + */ + def getVocabulary:Map[String,Int]={ --- End diff -- GetVocabulary and getwordvectors is useful when you need to join or iterator the built vectors in batch. While getvectors is only useful to lookup vector for one specific known word in vocabulary which throws exception when word is out of vocabulary. The usages are very different. Will look into the dataframe version to see whether it can cover the batch usrcase to decide whether we can work around without adding getter here.
--- 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 infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org