Github user srowen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17234#discussion_r105351890
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/mllib/feature/Word2Vec.scala ---
    @@ -610,6 +610,71 @@ class Word2VecModel private[spark] (
       }
     
       /**
    +   * Find words similar to the words supplied to 'positive' and dissimilar
    +   * to the words supplied to 'negative'.
    +   * @param positive array of words similar to the results list
    +   * @param negative array of words dissimilar to the results list
    +   * @param num number of synonyms to find
    +   * @return array of (word, cosineSimilarity)
    +   */
    +  def findAnalogies(positive: Array[String] = Array(),
    +                            negative: Array[String] = Array(),
    +                            num: Int = 1): Array[(String, Double)] = {
    +    require(num > 0, "Number of similar words should be > 0")
    +    require(positive.length > 0 || negative.length > 0,
    +      "Either positive or negative argument must be supplied")
    +
    +    var positiveVectors = Array[Array[Double]]()
    +    var negativeVectors = Array[Array[Double]]()
    +
    +    for(pp <- positive)
    +      positiveVectors :+= transform(pp).toArray
    +    for(nn <- negative)
    +      negativeVectors :+= transform(nn).toArray
    +    // Normalize positive and negative vectors before summation
    +    positiveVectors = if (positiveVectors.size > 0) {
    +      positiveVectors.map(x => {
    --- End diff --
    
    There is a fair bit of duplicated code that could be factored into methods.


---
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

Reply via email to