deeplearning4j-scaleout/deeplearning4j-nlp/src/main/java/org/deeplearning4j/models/embeddings/wordvectors/WordVectorsImpl.java
/**
     * Get the top n words most similar to the given word
     * @param word the word to compare
     * @param n the n to get
     * @return the top n words
     */
Get the top n words most similar to the given word
@param word the word to compare
@param n the n to get
@return the top n words
Get the top n words most similar to the given word @param word the word to compare @param n the n to get @return the top n words
public Collection<String> wordsNearestSum(String word,int n) {
        INDArray vec = Transforms.unitVec(this.getWordVectorMatrix(word));
        if(lookupTable() instanceof InMemoryLookupTable) {
            InMemoryLookupTable l = (InMemoryLookupTable) lookupTable();
            INDArray syn0 = l.getSyn0();
            INDArray weights = syn0.norm2(0).rdivi(1).muli(vec);
            INDArray distances = syn0.mulRowVector(weights).sum(1);
            INDArray[] sorted = Nd4j.sortWithIndices(distances,0,false);
            INDArray sort = sorted[0];
            List<String> ret = new ArrayList<>();
            SequenceElement word2 = vocab().wordFor(word);
            if(n > sort.length())
                n = sort.length();
            for(int i = 0; i < n + 1; i++) {
                if(sort.getInt(i) == word2.getIndex())
                    continue;
                String add = vocab().wordAtIndex(sort.getInt(i));
                if(add == null || add.equals("UNK") || add.equals("STOP")) {
                    continue;
                }
                ret.add(vocab().wordAtIndex(sort.getInt(i)));
            }
            return ret;
        }
        if(vec == null)
            return new ArrayList<>();
        Counter<String> distances = new Counter<>();
        for(String s : vocab().words()) {
            if(s.equals(word))
                continue;
            INDArray otherVec = getWordVectorMatrix(s);
            double sim = Transforms.cosineSim(vec,otherVec);
            distances.incrementCount(s, sim);
        }
        distances.keepTopNKeys(n);
        return distances.keySet();
    }
public Collection < String > wordsNearestSum ( String word , int n ) { INDArray vec = Transforms . unitVec ( this . getWordVectorMatrix ( word ) ) ; if ( lookupTable ( ) instanceof InMemoryLookupTable ) { InMemoryLookupTable l = ( InMemoryLookupTable ) lookupTable ( ) ; INDArray syn0 = l . getSyn0 ( ) ; INDArray weights = syn0 . norm2 ( 0 ) . rdivi ( 1 ) . muli ( vec ) ; INDArray distances = syn0 . mulRowVector ( weights ) . sum ( 1 ) ; INDArray [ ] sorted = Nd4j . sortWithIndices ( distances , 0 , false ) ; INDArray sort = sorted [ 0 ] ; List < String > ret = new ArrayList < > ( ) ; SequenceElement word2 = vocab ( ) . wordFor ( word ) ; if ( n > sort . length ( ) ) n = sort . length ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { if ( sort . getInt ( i ) == word2 . getIndex ( ) ) continue ; String add = vocab ( ) . wordAtIndex ( sort . getInt ( i ) ) ; if ( add == null || add . equals ( "UNK" ) || add . equals ( "STOP" ) ) { continue ; } ret . add ( vocab ( ) . wordAtIndex ( sort . getInt ( i ) ) ) ; } return ret ; } if ( vec == null ) return new ArrayList < > ( ) ; Counter < String > distances = new Counter < > ( ) ; for ( String s : vocab ( ) . words ( ) ) { if ( s . equals ( word ) ) continue ; INDArray otherVec = getWordVectorMatrix ( s ) ; double sim = Transforms . cosineSim ( vec , otherVec ) ; distances . incrementCount ( s , sim ) ; } distances . keepTopNKeys ( n ) ; return distances . keySet ( ) ; }
Get the top n words most similar to the given word
 @param word the word to compare
 @param n the n to get
 @return the top n words
Get the top n words most similar to the given word @param word the word to compare @param n the n to get @return the top n words
//there will be a redundant word