deeplearning4j-scaleout/deeplearning4j-nlp/src/main/java/org/deeplearning4j/models/embeddings/wordvectors/WordVectorsImpl.java
/**
     * This method returns nearest words for target word, based on tree structure.
     * This method is recommended to use if you're going to call for nearest words multiple times.
     *
     * @param word
     * @param n
     * @param resetTree
     * @return
     */
This method returns nearest words for target word, based on tree structure.
This method is recommended to use if you're going to call for nearest words multiple times.


This method returns nearest words for target word based on tree structure . This method is recommended to use if you ' re going to call for nearest words multiple times .
protected Collection<String> wordsNearest(String word, int n, boolean resetTree) {
        if (!vocab.hasToken(word)) return new ArrayList<>();
        if (vpTree == null || resetTree) {
            List<DataPoint> points = new ArrayList<>();
            for (String label: vocab.words()) {
                points.add(new DataPoint(vocab.indexOf(label), getWordVectorMatrix(label)));
            }
            vpTree = new VPTree(points);
        }
        List<DataPoint> add = new ArrayList<>();
        List<Double> distances = new ArrayList<>();
        vpTree.search(new DataPoint(0, getWordVectorMatrix(word)), n+1, add, distances );
        Collection<String> ret = new ArrayList<>();
        for (DataPoint e: add) {
            String label  = vocab.wordAtIndex(e.getIndex());
            if (!label.equals(word)) ret.add(label);
        }
        return ret;
    }
protected Collection < String > wordsNearest ( String word , int n , boolean resetTree ) { if ( ! vocab . hasToken ( word ) ) return new ArrayList < > ( ) ; if ( vpTree == null || resetTree ) { List < DataPoint > points = new ArrayList < > ( ) ; for ( String label : vocab . words ( ) ) { points . add ( new DataPoint ( vocab . indexOf ( label ) , getWordVectorMatrix ( label ) ) ) ; } vpTree = new VPTree ( points ) ; } List < DataPoint > add = new ArrayList < > ( ) ; List < Double > distances = new ArrayList < > ( ) ; vpTree . search ( new DataPoint ( 0 , getWordVectorMatrix ( word ) ) , n + 1 , add , distances ) ; Collection < String > ret = new ArrayList < > ( ) ; for ( DataPoint e : add ) { String label = vocab . wordAtIndex ( e . getIndex ( ) ) ; if ( ! label . equals ( word ) ) ret . add ( label ) ; } return ret ; }
This method returns nearest words for target word, based on tree structure.
This method returns nearest words for target word based on tree structure .
// build new tree if it wasnt created before, or resetTree == TRUE // we need n+1 to address original datapoint removal