词义相似度接口
输入两个词，得到两个词的相似度结果。

@param word1 - 词1（GBK编码），最大64字节*
@param word2 - 词1（GBK编码），最大64字节
@param options - 可选参数对象，key: value都为string类型
options - options列表:
mode 预留字段，可选择不同的词义相似度模型。默认值为0，目前仅支持mode=0
@return JSONObject
772243
public JSONObject wordSimEmbedding(String word1, String word2, HashMap<String, Object> options) {
        AipRequest request = new AipRequest();
        preOperation(request);
        
        request.addBody("word_1", word1);
        
        request.addBody("word_2", word2);
        
        if (options != null) {
            request.addBody(options);
        }
        request.setUri(NlpConsts.WORD_SIM_EMBEDDING);
        request.addHeader(Headers.CONTENT_ENCODING, HttpCharacterEncoding.ENCODE_GBK);
        request.addHeader(Headers.CONTENT_TYPE, HttpContentType.JSON_DATA);
        request.setBodyFormat(EBodyFormat.RAW_JSON);
        postOperation(request);
        return requestServer(request);
    }
531340
public int indexOf(String word) {
        if (vocabulary.containsKey(word)) {
            return vocabulary.get(word).getHuffmanNode().getIdx();
        } else
            return -1;
    }
908122
public TermNatures getTermNatures(String word) {
        String[] params = null;
        // 获得词性 ， 先从系统辞典。在从用户自定义辞典
        AnsjItem ansjItem = DATDictionary.getItem(word);
        TermNatures tn = null;

        if (ansjItem != AnsjItem.NULL) {
            tn = ansjItem.termNatures;
        } else if ((params = getParams(word)) != null) {
            tn = new TermNatures(new TermNature(params[0], 1));
        } else if (WordAlert.isEnglish(word)) {
            tn = TermNatures.EN;
        } else if (WordAlert.isNumber(word)) {
            tn = TermNatures.M;
        } else {
            tn = TermNatures.NULL;
        }
        return tn;
    }
75607
protected void taggingWeightWithWordFrequency(List<Word> words1, List<Word> words2){
        if(words1.get(0).getWeight() != null || words2.get(0).getWeight() != null){
            if(LOGGER.isDebugEnabled()){
                LOGGER.debug("词已经被指定权重，不再使用词频进行标注");
            }
            return;
        }
        //词频统计
        Map<String, AtomicInteger> frequency1 = frequency(words1);
        Map<String, AtomicInteger> frequency2 = frequency(words2);
        //输出词频统计信息
        if(LOGGER.isDebugEnabled()){
            LOGGER.debug("词频统计1：\n{}", formatWordsFrequency(frequency1));
            LOGGER.debug("词频统计2：\n{}", formatWordsFrequency(frequency2));
        }
        //权重标注
        words1.parallelStream().forEach(word->{
            word.setWeight(frequency1.get(word.getText()).floatValue());
        });
        words2.parallelStream().forEach(word->{
            word.setWeight(frequency2.get(word.getText()).floatValue());
        });
    }
44626
function ttf2symbol(ttfBuffer, options) {

            options = options || {};

            // 读取ttf二进制流
            if (ttfBuffer instanceof ArrayBuffer) {
                var reader = new TTFReader();
                var ttfObject = reader.read(ttfBuffer);
                reader.dispose();

                return ttfobject2symbol(ttfObject, options);
            }
            // 读取ttfObject
            else if (ttfBuffer.version && ttfBuffer.glyf) {

                return ttfobject2symbol(ttfBuffer, options);
            }

            error.raise(10112);
        }
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;
    }
1207890
def get_pair(self, term1, term2):

        

        key = self.key(term1, term2)
        return self.pairs.get(key, None)
1046041
public ResponseWrapper deleteSensitiveWord(String word) throws APIConnectionException, APIRequestException {
        Preconditions.checkArgument(word.length() <= 10, "one word's max length is 10");
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("word", word);
        return _httpClient.sendDelete(_baseUrl + sensitiveWordPath, jsonObject.toString());
    }
1147636
@SafeVarargs
    public final WordBuilder<I> append(Word<? extends I>... words) {
        if (words.length == 0) {
            return this;
        }

        int allLen = 0;
        for (Word<? extends I> w : words) {
            allLen += w.length();
        }

        ensureAdditionalCapacity(allLen);

        for (Word<? extends I> word : words) {
            int wLen = word.length();
            word.writeToArray(0, array, length, wLen);
            length += wLen;
        }

        return this;
    }
943225
public static String or(final String first, final String second) {
    final StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < first.length(); i++) {
      if ('0' == first.charAt(i) && '0' == second.charAt(i)) {
        buffer.append('0');
      } else {
        buffer.append('1');
      }
    }
    return buffer.toString();
  }
283932
public List<Blob> getBlobFromStringIterable(String blobName1, String blobName2) {
    // [START getBlobFromStringIterable]
    List<String> blobNames = new LinkedList<>();
    blobNames.add(blobName1);
    blobNames.add(blobName2);
    List<Blob> blobs = bucket.get(blobNames);
    for (Blob blob : blobs) {
      if (blob == null) {
        // the blob was not found
      }
    }
    // [END getBlobFromStringIterable]
    return blobs;
  }
792791
public V get(K1 key1, K2 key2) {
        return JMOptional.getOptional(nestedMap, key1).map(map -> map.get(key2))
                .orElse(null);
    }
434120
def basic_word_sim(word1, word2):
    
    return sum([1 for c in word1 if c in word2]) / max(len(word1), len(word2))
18897
private void termsApiUsage() throws JsonProcessingException, ApiException {
        /**
         * 
         */
        LOG.info("The Terms API usage.");
        Terms api = retinaApisInstance.termsApi();
        /**
         * 
         */
        LOG.info("Terms API: getContextsForTerm");
        List<Context> contexts = api.getContexts("apple");
        logContext(contexts);
        contexts = api.getContexts("apple", new Pagination(0, 2), true);
        
        logContext(contexts);
        /**
         * 
         */
        List<Term> terms;
        LOG.info("Terms API: getSimilarTerms");
        
        terms = api.getSimilarTerms("tiger");
        
        logTerms(terms);
        
        terms = api.getSimilarTerms("tiger", null, PosType.NOUN);
        
        logTerms(terms);
        
        //**** ERROR *****
        terms = api.getSimilarTerms("tiger", null, PosType.NOUN, true);
        logTerms(terms);
        /**
         * 
         */
        LOG.info("Terms API: getTerm");
        terms = api.getTerm("apple");
        logTerms(terms);
        System.out.println();
        
        terms = api.getTerm("banana", new Pagination(0, 2), true);
        logTerms(terms);
        System.out.println();
        
        LOG.info("Terms API: getTerm NULL TEST");
        terms = api.getTerm(null);
        logTerms(terms);
    }
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();
    }
1086144
public static List<Component> allSizeComponents(Word word) {
		Set<Component> components = Sets.newHashSet();
		for(int nbComponents=word.getComponents().size();
				nbComponents > 0 ;
				nbComponents--) {
			
			for(int startIndex = 0;
					startIndex <= word.getComponents().size() - nbComponents;
					startIndex++) {
				List<Component> toMerge = Lists.newArrayListWithExpectedSize(nbComponents);
				
				for(int i = 0; i<nbComponents; i++) 
					toMerge.add(word.getComponents().get(startIndex + i));
				
				components.add(merge(word, toMerge));
			}
		}
		return Lists.newArrayList(components);
	}

	/**
	 * 
	 * Merges <code>n</code> consecutive components of a compound
	 * word into a single {@link Component} object. 
	 * 
	 * The <code>lemma</code> of the returned {@link Component} is
	 * the concatenation of the 1st to n-1-th param components' substring 
	 * and the last param component's <code>lemma</code>.
	 * 
	 * 
	 * @param word
	 * 			The compound word
	 * @param components
	 * 			The list of consecutive components of the word to merge
	 * @return
	 * 			The merged component
	 * 
	 * @throws IllegalArgumentException
	 * 				when the <code>components</code> param is empty
	 * @throws IllegalArgumentException
	 * 				when the <code>components</code> are not consecutive
	 * @throws IllegalArgumentException
	 * 				when the components offsets do not match with the <code>word</code> size.
	 */
	public static Component merge(Word word, Iterable<? extends Component> components) {
		Preconditions.checkNotNull(word.getLemma(), ERR_WMSG_WORD_LEMMA_NULL);
		 
		
		Iterator<? extends Component> it = components.iterator();
		Preconditions.checkArgument(it.hasNext(), ERR_MSG_CANNOT_MERGE_AN_EMPTY_SET);
		
		Component lastComponent = it.next();
		int begin = lastComponent.getBegin();
		StringBuilder lemmaBuilder = new StringBuilder();
		while (it.hasNext()) {
			Component cur = it.next();
			Preconditions.checkArgument(
					cur.getBegin() >= lastComponent.getEnd(),
					ERR_MSG_COMPONENTS_OVERLAP,
					lastComponent.getBegin(), lastComponent.getEnd(),
					cur.getBegin(), cur.getEnd()
				);
			
			
			Preconditions.checkArgument(
					cur.getEnd() <= word.getLemma().length(),
					ERR_MSG_COMPONENT_OFFSET_ARE_TOO_BIG,
					cur, word, word.getLemma().length(),
					cur.getBegin(),cur.getEnd()
					);
			lemmaBuilder.append(lastComponent.getSubstring());
			
			if(lastComponent.getEnd() < cur.getBegin())
				/*
				 * Fills the gap with the lemma substring
				 */
				lemmaBuilder.append(word.getLemma().substring(lastComponent.getEnd(), cur.getBegin()));
			
			lastComponent = cur;
		}
		lemmaBuilder.append(lastComponent.getLemma());
		String substring = word.getLemma().substring(begin, lastComponent.getEnd());
		return new Component(begin, lastComponent.getEnd(), substring, lemmaBuilder.toString());
	}
	
	
	/**
	 * @see #merge(Word, Iterable)
	 * 
	 * @param word
	 * @param begin
	 * @param end
	 * @return
	 */
	public static Component merge(Word word, int begin, int end) {
		return merge(word, word.getComponents().subList(begin, end+1));
	}


	
	/**
	 * 
	 * Produces the set of all pairs of non-overlapping components
	 * for a given word.
	 * 
	 * E.g. ab|cd|ef returns:
	 * 		ab+cd, ab+ef, cd+ef, ab+cdef, abcd+ef
	 * 			
	 * 
	 * @param word
	 * 			the compound word
	 * @return
	 * 			the exhaustive list of pairs.
	 */
	public static List<Pair<Component>> innerComponentPairs(Word word) {
		Set<Pair<Component>> pairs = Sets.newHashSet();
		List<Component> components = allSizeComponents(word);
		Component c1,c2;
		Pair<Component> pair;
		for(int i=0; i<components.size(); i++) {
			c1 = components.get(i);
			for(int j=i+1; j<components.size(); j++) {
				c2 = components.get(j);
				pair = new Pair<Component>(c1, c2);
				if(pair.getElement1().getEnd() <= pair.getElement2().getBegin())
					// no overlap
					pairs.add(pair);
			}
		}
		return Lists.newArrayList(pairs);
	}
	
	
	public static String toClassString(String s1, String s2) {
		boolean ordered = s1.compareTo(s2) <= 0;
		StringBuilder sb = new StringBuilder();
		sb.append(ordered ? s1 : s2);
		sb.append(TermSuiteConstants.PLUS);
		sb.append(ordered ? s2 : s1);
		return sb.toString();
	}
	
	
	public static Set<String> toIndexStrings(Pair<Component> pair) {
		Set<String> classes = new HashSet<>(4);
		
		// substring+substring
		classes.add(toClassString(
				pair.getElement1().getSubstring(), 
				pair.getElement2().getSubstring()));
		
		
		// lemma+lemma
		classes.add(toClassString(
			pair.getElement1().getLemma(), 
			pair.getElement2().getLemma()));

		// lemma+substring
		classes.add(toClassString(
			pair.getElement1().getLemma(), 
			pair.getElement2().getSubstring()));

		// substring+lemma
		classes.add(toClassString(
			pair.getElement1().getSubstring(), 
			pair.getElement2().getLemma()));
		
		
		return classes;
	}

	/**
	 * 
	 * <b>WARNING: This method does not behave as {@link #innerComponentPairs(Word)}.</b> 
	 * This method enforces that returned pairs cover the input word completely and 
	 * without any overlap.
	 *
	 * Example 1: with a word that is not a compound, it returns an empty list.
	 * 
	 * Example 2: with a word that is a size-2 compound, it returns the only pair of lemmas possible:
	 * 	
	 * <code>
	 * 	w = "ab|cd"
	 *  returnedPairs are [["ab","cd"]]
	 * </code>
	 * 
	 * Example 3: with a word that is a size-3 compound, it returns two pairs of lemmas:
	 * 	
	 * <code>
	 * 	w = "ab|cd|ef"
	 *  returnedPairs are [["ab","cded"], ["abcd","ef"]]
	 * </code>
	 * 
	 * Example 4: with a word that is a size-n compound, it returns n-1 pairs of lemmas:
	 * 
	 * <code>
	 * 	w = "comp1|comp2|...|compn"
	 *  returnedPairs are [
	 *  	["comp1","comp2comp3...compn"],
	 *  	["comp1comp2","comp3comp4...compn"], 
	 *  	..., 
	 *  	["comp1comp2...compn-1","compn"]
	 *  ]
	 * </code>
	 * 
	 * 
	 * @param word
	 * 			The input compound word
	 */
	public static Set<Pair<Component>> innerContiguousComponentPairs(Word word) {
		Set<Pair<Component>> pairs = new HashSet<>();
		if(word.isCompound()) {
			int n = word.getComponents().size();
			for(int i=0; i<n-1; i++) {
				pairs.add(new Pair<Component>(
						merge(word, word.getComponents().subList(0, i+1)), 
						merge(word, word.getComponents().subList(i+1, n))));
			}
		}
		return pairs;
	}
	
	
	/**
	 * 
	 * Gives all possible component of a compound word
	 * at given index.
	 * 
	 * Example with word ab|cd|ef|gg 
	 * 
	 * word[0] can be any of: ab, abcd, or abcdef 
	 * word[1] can be any of: cd, cdef, cdefgg, ef, efgg, or gg
	 * word[2] can be any of: ef, efgg, gg
	 * word[3] can only be: gg 
	 * 
	 * 
	 * Explanation: 
	 * 
	 * word[0] can be:
	 * 	- aa if word is considered to be composed as ab|cd|ef|gg, 
	 * 	- abcd if word is considered to be composed as abcd|ef|gg or abcd|efgg 
	 * 	- abcdef if word is considered to be composed as abcdef|gg 
	 * 
	 * idem with word[1], word[2] and word[3]
	 * 
	 * @param word
	 * @param i
	 * @return
	 */
	public static Set<Component> getPossibleComponentsAt(Word word, int i) {
		if(word.isCompound()) {
			switch (word.getComponents().size()) {
			case 0:
				return ImmutableSet.of();
			case 1:
				return i==0 ? ImmutableSet.of(word.getComponents().get(0)) : ImmutableSet.of();
			case 2:
				switch(i){
				case 0: return ImmutableSet.of(word.getComponents().get(0));
				case 1: return ImmutableSet.of(word.getComponents().get(1));
				default: return ImmutableSet.of();
				}
			case 3:
				return i < word.getComponents().size() ? 
						SIZE_3_POSSIBLE_BEGIN_END_AT_INDEX.get(i)
								.stream()
								.map(pair -> CompoundUtils.merge(word, pair.getElement1(), pair.getElement2()))
								.collect(Collectors.toSet()) : 
							ImmutableSet.of();
			case 4:
				return i < word.getComponents().size() ? 
						SIZE_4_POSSIBLE_BEGIN_END_AT_INDEX.get(i)
								.stream()
								.map(pair -> CompoundUtils.merge(word, pair.getElement1(), pair.getElement2()))
								.collect(Collectors.toSet()) : 
							ImmutableSet.of();
			default:
				return i < word.getComponents().size() ? 
						ImmutableSet.of(word.getComponents().get(i)) : 
							ImmutableSet.of();
			}
			
		} else
			return ImmutableSet.of();
	}

	private static Map<Integer, List<Pair<Integer>>> SIZE_3_POSSIBLE_BEGIN_END_AT_INDEX = ImmutableMap.of(
			0, ImmutableList.of(new Pair<Integer>(0, 0), new Pair<Integer>(0, 1)),
			1, ImmutableList.of(new Pair<Integer>(1, 1), new Pair<Integer>(1, 2), new Pair<Integer>(2, 2)),
			2, ImmutableList.of(new Pair<Integer>(2, 2))
		);

	private static Map<Integer, List<Pair<Integer>>> SIZE_4_POSSIBLE_BEGIN_END_AT_INDEX = ImmutableMap.of(
			0, ImmutableList.of(new Pair<Integer>(0, 0), new Pair<Integer>(0, 1), new Pair<Integer>(0, 2)),
			1, ImmutableList.of(new Pair<Integer>(1, 1), new Pair<Integer>(1, 2), new Pair<Integer>(1, 3), new Pair<Integer>(2, 2), new Pair<Integer>(2, 3), new Pair<Integer>(3, 3)),
			2, ImmutableList.of(new Pair<Integer>(2, 2), new Pair<Integer>(2, 3), new Pair<Integer>(3, 3)),
			3, ImmutableList.of(new Pair<Integer>(3, 3))
		);

}
682305
public Query setOptionalWords(List<String> words) {
    StringBuilder builder = new StringBuilder();
    for (String word : words) {
      builder.append(word);
      builder.append(",");
    }
    this.optionalWords = builder.toString();
    return this;
  }