Find synonyms (i.e. word-vectors with the highest cosine similarity)

@param target String of desired word
@param cnt Number of synonyms to find
976798
public Map<String, Float> findSynonyms(String target, int cnt) {
    float[] vec = transform(target);

    if ((vec == null) || (cnt == 0))
      return Collections.emptyMap();

    int[] synonyms = new int[cnt];
    float[] scores = new float[cnt];


    int min = 0;
    for (int i = 0; i < cnt; i++) {
      synonyms[i] = i;
      scores[i] = cosineSimilarity(vec, i * vec.length, _output._vecs);
      if (scores[i] < scores[min])
        min = i;
    }

    final int vocabSize = _output._vocab.size();
    for (int i = cnt; i < vocabSize; i++) {
      float score = cosineSimilarity(vec, i * vec.length, _output._vecs);
      if ((score <= scores[min]) || (score >= 0.999999))
        continue;
      synonyms[min] = i;
      scores[min] = score;
      // find a new min
      min = 0;
      for (int j = 1; j < cnt; j++)
        if (scores[j] < scores[min])
          min = j;
    }

    Map<String, Float> result = new HashMap<>(cnt);
    for (int i = 0; i < cnt; i++)
      result.put(_output._words[synonyms[i]].toString(), scores[i]);
    return result;
  }
421603
@Override
    public List<String> similarWordsInVocabTo(String word, double accuracy) {
        List<String> ret = new ArrayList<>();
        for (String s : vocabCache.words()) {
            if (MathUtils.stringSimilarity(word, s) >= accuracy)
                ret.add(s);
        }
        return ret;
    }
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();
    }
649369
@Override
    public Collection<String> wordsNearest(INDArray words, int top) {
        if (lookupTable instanceof InMemoryLookupTable) {
            InMemoryLookupTable l = (InMemoryLookupTable) lookupTable;

            INDArray syn0 = l.getSyn0();

            if (!normalized) {
                synchronized (this) {
                    if (!normalized) {
                        syn0.diviColumnVector(syn0.norm2(1));
                        normalized = true;
                    }
                }
            }

            INDArray similarity = Transforms.unitVec(words).mmul(syn0.transpose());

            List<Double> highToLowSimList = getTopN(similarity, top + 20);

            List<WordSimilarity> result = new ArrayList<>();

            for (int i = 0; i < highToLowSimList.size(); i++) {
                String word = vocabCache.wordAtIndex(highToLowSimList.get(i).intValue());
                if (word != null && !word.equals("UNK") && !word.equals("STOP")) {
                    INDArray otherVec = lookupTable.vector(word);
                    double sim = Transforms.cosineSim(words, otherVec);

                    result.add(new WordSimilarity(word, sim));
                }
            }

            Collections.sort(result, new SimilarityComparator());

            return getLabels(result, top);
        }

        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();


    }
621484
public static List<WordInfo> extractWords(String text, int size)
    {
        return extractWords(text, size, false);
    }
102603
public static <T extends Levenshtein> T cosine(String baseTarget, String compareTarget) {
		return cosine(baseTarget, compareTarget, null);
	}
47008
public void writeOntTxt(PrintWriter pw) {
        for (String id : terms.keySet()) {
            OntologyTerm term = terms.get(id);
            Set<String> synSet = new HashSet<String>();
            synSet.add(term.getName());
            for (Synonym s : term.getSynonyms()) {
                String st = s.getType();
                if (id.startsWith("PTCO")
                        || (st != null && !st
                                .matches(".*(InChI|SMILES|FORMULA).*"))) {
                    String synonym = s.getSyn();
                    if (!synonym.matches("\\S")) {// reject ontology terms
                                                  // consisting of a single
                                                  // letter as these produce too
                                                  // many false positives
                        synSet.add(synonym);
                    }
                }
            }

            pw.println("[" + id + "]");
            for (String syn : synSet)
                pw.println(syn);
            pw.println();
        }
        pw.flush();
    }
1146677
private void processWordsInNP(ArrayList<Pair<String>> wordsInPhrase) {
	if( wordsInPhrase.size() > 1 ) {
	    // this is from Grefenstette's pseudo code
	    for (int i = 0; i < wordsInPhrase.size()-1; i++) {

		if (inStartSet(wordsInPhrase.get(i).x) ) {
		
		    for (int j = i+1; j < wordsInPhrase.size(); j++ ) {
			
			if (inReceiveSet( wordsInPhrase.get(j).x ) ) {

			    wordRelationsWriter.
				println(wordsInPhrase.get(j).y + " "
					+ wordsInPhrase.get(i).y);
			    
// 			    System.out.println(wordsInPhrase.get(j).y + " "
// 					       + wordsInPhrase.get(i).y);
			    
			    addRelation(wordsInPhrase.get(j).y, 
					wordsInPhrase.get(i).y);
			}
		    }
		}
	    }
	}
    }
602270
def findSynonyms(self, word, num):
        
        if not isinstance(word, basestring):
            word = _convert_to_vector(word)
        return self._call_java("findSynonyms", word, num)
984514
static List<Target> span2TargetList(Span<Term> span) {
	List<Target> list = new ArrayList<Target>();
	for (Term t : span.getTargets()) {
	    list.add(KAFDocument.createTarget(t, (t==span.getHead())));
	}
	return list;
    }
232623
protected List<CmsPair<CmsResource, CmsResource>> getMatchingResources(String source, String target)
    throws CmsException {

        List<CmsResource> sourceResources = readTree(source);
        Map<String, CmsResource> sourceRelative = getResourcesByRelativePath(sourceResources, source);

        List<CmsResource> targetResources = readTree(target);
        Map<String, CmsResource> targetRelative = getResourcesByRelativePath(targetResources, target);

        List<CmsPair<CmsResource, CmsResource>> result = new ArrayList<CmsPair<CmsResource, CmsResource>>();
        sourceRelative.keySet().retainAll(targetRelative.keySet());
        for (Map.Entry<String, CmsResource> entry : sourceRelative.entrySet()) {
            String key = entry.getKey();
            CmsResource sourceRes = entry.getValue();
            CmsResource targetRes = targetRelative.get(key);
            result.add(CmsPair.create(sourceRes, targetRes));
        }
        return result;
    }
865349
public Vector query(String content)
    {
        if (content == null || content.length() == 0) return null;
        List<Term> termList = NotionalTokenizer.segment(content);
        Vector result = new Vector(dimension());
        int n = 0;
        for (Term term : termList)
        {
            Vector vector = wordVectorModel.vector(term.word);
            if (vector == null)
            {
                continue;
            }
            ++n;
            result.addToSelf(vector);
        }
        if (n == 0)
        {
            return null;
        }
        result.normalize();
        return result;
    }
59136
public double similarityToLabel(List<VocabWord> document, String label) {
        if (document.isEmpty())
            throw new IllegalStateException("Document has no words inside");

        /*
        INDArray arr = Nd4j.create(document.size(), this.layerSize);
        for (int i = 0; i < document.size(); i++) {
            arr.putRow(i, getWordVectorMatrix(document.get(i).getWord()));
        }*/

        INDArray docMean = inferVector(document); //arr.mean(0);

        INDArray otherVec = getWordVectorMatrix(label);
        double sim = Transforms.cosineSim(docMean, otherVec);
        return sim;
    }
510644
private void linkSpaces(ArrayList spaces) {
		for (int source=0;source<spaces.size();source++) {
			Space a = (Space) spaces.get(source);
			
			for (int target=source+1;target<spaces.size();target++) {
				Space b = (Space) spaces.get(target);
				
				if (a.hasJoinedEdge(b)) {
					a.link(b);
					b.link(a);
				}
			}
		}
	}
1368922
protected void searchNode(P target, KdTree.Node n) {
		while( n != null) {
			checkBestDistance(n, target);

			if( n.isLeaf() )
				break;

			// select the most promising branch to investigate first
			KdTree.Node nearer,further;

			double splitValue = distance.valueAt( (P)n.point , n.split );

			if( distance.valueAt( target , n.split ) <= splitValue ) {
				nearer = n.left;
				further = n.right;
			} else {
				nearer = n.right;
				further = n.left;
			}

			// See if it is possible for 'further' to contain a better node
			double dx = splitValue - distance.valueAt(target, n.split );
			if( further != null && canImprove(dx*dx) ) {
				addToQueue(dx*dx, further, target );
			}

			n = nearer;
		}
	}
535299
private void findTargets(final IndexTerm term) {
        final List<IndexTerm> subTerms = term.getSubTerms();
        List<IndexTermTarget> subTargets = null;
        if (subTerms != null && ! subTerms.isEmpty()){
            for (final IndexTerm subTerm : subTerms) {
                subTargets = subTerm.getTargetList();
                if (subTargets != null && !subTargets.isEmpty()) {
                    //findTargets(subTerm);
                    //add targets(child term)
                    term.addTargets(subTerm.getTargetList());
                } else {
                    //term.addTargets(subTerm.getTargetList());
                    //recursive search child's child term
                    findTargets(subTerm);
                }
                //add target to parent indexterm
                term.addTargets(subTerm.getTargetList());
            }

        }
    }
353819
def _prttex_summary_cnts(self, prt, cnts):
        """"""
        # Count level(shortest path to root) and depth(longest path to root)
        # values for all unique GO Terms.
        prt.write("\n\n% LaTeX Table for GO counts at each level and depth in the GO DAG\n")
        prt.write(r"\begin{table}[bt]" "\n")
        prt.write(r"\begin{tabular}{|r |r |r |r |r |r |r|}" "\n")

        title = self.title.replace('_', r'\_')
        prt.write(r"\hline" "\n")
        prt.write(r"\rowcolor{gray!10}" "\n")
        prt.write(" ".join([r"\multicolumn{7}{|l|}{", title, r"} \\", "\n"]))
        prt.write(r"\hline" "\n")
        prt.write(r"\rowcolor{gray!10}" "\n")
        prt.write(r"Depth &" "\n")
        prt.write(r"\multicolumn{3}{c|}{Depth} &" "\n")
        prt.write(r"\multicolumn{3}{c|}{Level} \\" "\n")
        prt.write(r"\cline{2-7}" "\n")
        prt.write(r"\rowcolor{gray!10}" "\n")
        prt.write(r"or Level & BP & MF & CC & BP & MF & CC \\" "\n")
        prt.write(r"\hline" "\n")

        max_val = max(max(dep for dep in cnts['depth']), max(lev for lev in cnts['level']))
        for i in range(max_val+1):
            vals = ['{:>5}'.format(cnts[desc][i][ns]) for desc in cnts for ns in self.nss]
            self.log.write('{:>02} & {} \\\\\n'.format(i, ' & '.join(vals)))
            if i%2 == 0:
                prt.write(r"\rowcolor{gray!7}" "\n")
        prt.write(r"\hline" "\n")
        prt.write(r"\end{tabular}" "\n")
        prt.write(r"\end{table}" "\n")