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
175428
@Override
    public Collection<String> wordsNearestSum(String word, int n) {
        //INDArray vec = Transforms.unitVec(this.lookupTable.vector(word));
        INDArray vec = this.lookupTable.vector(word);
        return wordsNearestSum(vec, n);
    }
916494
private final int getIndexFor(String word) {
	Integer index = termToIndex.get(word);
	if (index == null) {	 
	    synchronized(this) {
		// recheck to see if the term was added while blocking
		index = termToIndex.get(word);
		// if another thread has not already added this word while the
		// current thread was blocking waiting on the lock, then add it.
		if (index == null) {
		    int i = wordIndexCounter++;
                    // Add a new counter for this term.  Because the
                    // wordIndexCounter starts at zero, so the next index will
                    // be the last index in the termCounts list.
                    termCounts.add(new AtomicInteger(0));
		    termToIndex.put(word, i);
		    return i; // avoid the auto-boxing to assign i to index
		}
	    }
	}
	return index;
    }
816552
public double[] getWordVector(String word) {
        INDArray r = getWordVectorMatrix(word);
        if (r == null)
            return null;
        return r.dup().data().asDouble();
    }
1239590
private Map<String, AtomicInteger> frequency(List<Word> words){
        Map<String, AtomicInteger> frequency =new HashMap<>();
        words.forEach(word->{
            frequency.computeIfAbsent(word.getText(), k -> new AtomicInteger()).incrementAndGet();
        });
        return frequency;
    }
596777
def most_similar(self, word, number=5):
        

        if self.word_vectors is None:
            raise Exception('Model must be fit before querying')

        if self.dictionary is None:
            raise Exception('No word dictionary supplied')

        try:
            word_idx = self.dictionary[word]
        except KeyError:
            raise Exception('Word not in dictionary')

        return self._similarity_query(self.word_vectors[word_idx], number)[1:]
171531
@Override
    public int wordFrequency(@NonNull String word) {
        // TODO: proper wordFrequency impl should return long, instead of int
        T element = extendedVocabulary.get(word);
        if (element != null)
            return (int) element.getElementFrequency();
        return 0;
    }
730065
public List<String> similarWordsInVocabTo(String word, double accuracy) {
        return this.modelUtils.similarWordsInVocabTo(word, accuracy);
    }
174764
public static float bigram(List<Word> words){
        if(words.size() > 1){
            float score=0;
            for(int i=0; i<words.size()-1; i++){
                score += getScore(words.get(i).getText(), words.get(i+1).getText());
            }
            return score;
        }
        return 0;
    }
1212956
public void addWord(String word) {
		WordTree parent = null;
		WordTree current = this;
		WordTree child;
		char currentChar = 0;
		int length = word.length();
		for(int i = 0; i < length; i++){
			currentChar = word.charAt(i);
			if(false == StopChar.isStopChar(currentChar)){//只处理合法字符
				child = current.get(currentChar);
				if(child == null){
					//无子类，新建一个子节点后存放下一个字符
					child = new WordTree();
					current.put(currentChar, child);
				}
				parent = current;
				current = child;
			}
		}
		if(null != parent){
			parent.setEnd(currentChar);
		}
	}
321853
@Override
    public String words() {
        int numberOfWords = DEFAULT_RANDOM_NUMBERS_POOL[new Random().nextInt(DEFAULT_RANDOM_NUMBERS_POOL.length)];
        return words(numberOfWords);
    }
68014
@Override
    public String words(int numberOfWords) {

        if (numberOfWords < 1) {
            throw new IllegalArgumentException(INVALID_ARGUMENT_ERROR_MSG);
        }

        List<String> loremWords = new ArrayList<>();

        for(int i = 0; i < numberOfWords; i++) {
            loremWords.add(word());
        }

        return TextUtils.join(" ", loremWords);
    }
915637
public CompletionContext drop(int nbWords) {
		return new CompletionContext(new ArrayList<String>(words.subList(nbWords, words.size())), wordIndex-nbWords, position);
	}
1080553
private Map<String, Double> distance1Generation(String word) {
        if (word == null || word.length() < 1) throw new RuntimeException("Input words Error: " + word);

        Map<String, Double> result = new HashMap<String, Double>();

        String prev;
        String last;

        for (int i = 0; i < word.length(); i++) {
            // Deletion
            prev = word.substring(0, i);
            last = word.substring(i + 1, word.length());
            result.put(prev + last, 1.0);

            // transposition
            if ((i + 1) < word.length()) {
                prev = word.substring(0, i);
                last = word.substring(i + 2, word.length());
                String trans = prev + word.charAt(i + 1) + word.charAt(i) + last;
                result.put(trans, 1.0);
            }

            // alter
            prev = word.substring(0, i);
            last = word.substring(i + 1, word.length());
            for (int j = 0; j < 26; j++) {
                result.put(prev + (char) (j + 97) + last, 1.0);
            }

            // insertion
            prev = word.substring(0, i);
            last = word.substring(i + 1, word.length());
            for (int j = 0; j < 26; j++) {
                result.put(prev + (char) (j + 97) + word.charAt(i) + last, 1.0);
                result.put(prev + word.charAt(i) + (char) (j + 97) + last, 1.0);
            }

        }

        result.remove(word);
        return result;
    }
92892
public static List<Word> split(Word word){
        String value = GENERIC_TRIE.get(word.getText());
        if(value==null){
            return null;
        }
        List<Word> words = new ArrayList<>();
        for(String val : value.split("\\s+")){
            words.add(new Word(val));
        }
        if(words.isEmpty()){
            return null;
        }
        return words;
    }
86737
public static Word compile(Word word)
    {
        word.value = PosTagCompiler.compile(word.label, word.value);
//        switch (word.label)
//        {
//            case "ns":
//            case "nsf":
//            {
//                word.value = Predefine.TAG_PLACE;
//            }
//            break;
////            case "nz":
//            case "nx":
//            {
//                    word.value = Predefine.TAG_PROPER;
//            }
//            break;
//            case "nt":
//            case "ntc":
//            case "ntcf":
//            case "ntcb":
//            case "ntch":
//            case "nto":
//            case "ntu":
//            case "nts":
//            case "nth":
//            {
//                word.value = Predefine.TAG_GROUP;
//            }
//            break;
//            case "m":
//            case "mq":
//            {
//                    word.value = Predefine.TAG_NUMBER;
//            }
//            break;
//            case "x":
//            {
//                word.value = Predefine.TAG_CLUSTER;
//            }
//            break;
//            case "xx":
//            {
//                word.value = Predefine.TAG_OTHER;
//            }
//            break;
//            case "t":
//            {
//                    word.value = Predefine.TAG_TIME;
//            }
//            break;
//            case "nr":
//            case "nrf":
//            {
//                word.value = Predefine.TAG_PEOPLE;
//            }
//            break;
//        }
        return word;
    }
1094274
@Override
    public Collection<String> wordsNearestSum(INDArray words, int top) {

        if (lookupTable instanceof InMemoryLookupTable) {
            InMemoryLookupTable l = (InMemoryLookupTable) lookupTable;
            INDArray syn0 = l.getSyn0();
            INDArray weights = syn0.norm2(0).rdivi(1).muli(words);
            INDArray distances = syn0.mulRowVector(weights).sum(1);
            INDArray[] sorted = Nd4j.sortWithIndices(distances, 0, false);
            INDArray sort = sorted[0];
            List<String> ret = new ArrayList<>();

            // FIXME: int cast
            if (top > sort.length())
                top = (int) sort.length();
            //there will be a redundant word
            int end = top;
            for (int i = 0; i < end; i++) {
                String add = vocabCache.wordAtIndex(sort.getInt(i));
                if (add == null || add.equals("UNK") || add.equals("STOP")) {
                    end++;
                    if (end >= sort.length())
                        break;
                    continue;
                }
                ret.add(vocabCache.wordAtIndex(sort.getInt(i)));
            }
            return ret;
        }

        Counter<String> distances = new Counter<>();

        for (String s : vocabCache.words()) {
            INDArray otherVec = lookupTable.vector(s);
            double sim = Transforms.cosineSim(words, otherVec);
            distances.incrementCount(s, (float) sim);
        }

        distances.keepTopNElements(top);
        return distances.keySet();
    }
1366343
def _get_word_ngrams(n, sentences):
    
    assert len(sentences) > 0
    assert n > 0

    words = _split_into_words(sentences)
    return _get_ngrams(n, words)