try to place in center, build out in a spiral trying to place words for N steps
@param word the word being placed
@param start the place to start trying to place the word
194796
protected boolean place(final Word word, final Point start) {
        final Graphics graphics = this.bufferedImage.getGraphics();

        final int maxRadius = computeRadius(dimension, start);
        final Point position = word.getPosition();
        
        for (int r = 0; r < maxRadius; r += 2) {
            for (int x = Math.max(-start.x, -r); x <= Math.min(r, dimension.width - start.x - 1); x++) {
                position.x = start.x + x;

                final int offset = (int) Math.sqrt(r * r - x * x);
                
                // try positive root
                position.y = start.y + offset;
                if (position.y >= 0 && position.y < dimension.height && canPlace(word)) {
                    collisionRaster.mask(word.getCollisionRaster(), position);
                    graphics.drawImage(word.getBufferedImage(), position.x, position.y, null);
                    return true;
                }
                
                // try negative root (if offset != 0)
                position.y = start.y - offset;
                if (offset != 0 && position.y >= 0 && position.y < dimension.height && canPlace(word)) {
                    collisionRaster.mask(word.getCollisionRaster(), position);
                    graphics.drawImage(word.getBufferedImage(), position.x, position.y, null);
                    return true;
                }
            }
        }

        return false;
    }
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);
    }
1166433
@Override
  protected void addProhibitedWords(List<String> words) {
    if(words.size() == 1 && words.get(0).endsWith(".*")) {
      wordStartsToBeProhibited.add(words.get(0).substring(0, words.get(0).length()-2));
    } else {
      super.addProhibitedWords(words);
    }
  }
304779
@Override
  public List<WordData> lookup(CharSequence word) {
    final byte separator = dictionaryMetadata.getSeparator();
    final int prefixBytes = sequenceEncoder.prefixBytes();

    if (!dictionaryMetadata.getInputConversionPairs().isEmpty()) {
      word = applyReplacements(word, dictionaryMetadata.getInputConversionPairs());
    }

    // Reset the output list to zero length.
    formsList.wrap(forms, 0, 0);

    // Encode word characters into bytes in the same encoding as the FSA's.
    charBuffer = BufferUtils.clearAndEnsureCapacity(charBuffer, word.length());
    for (int i = 0; i < word.length(); i++) {
      char chr = word.charAt(i);
      if (chr == separatorChar) {
        // No valid input can contain the separator.
        return formsList;
      }
      charBuffer.put(chr);
    }
    charBuffer.flip();
    try {
      byteBuffer = BufferUtils.charsToBytes(encoder, charBuffer, byteBuffer);
    } catch (UnmappableInputException e) {
      // This should be a rare occurrence, but if it happens it means there is no way
      // the dictionary can contain the input word.
      return formsList;
    }

    // Try to find a partial match in the dictionary.
    final MatchResult match = matcher.match(matchResult, byteBuffer
        .array(), 0, byteBuffer.remaining(), rootNode);

    if (match.kind == SEQUENCE_IS_A_PREFIX) {
      /*
       * The entire sequence exists in the dictionary. A separator should
       * be the next symbol.
       */
      final int arc = fsa.getArc(match.node, separator);

      /*
       * The situation when the arc points to a final node should NEVER
       * happen. After all, we want the word to have SOME base form.
       */
      if (arc != 0 && !fsa.isArcFinal(arc)) {
        // There is such a word in the dictionary. Return its base forms.
        int formsCount = 0;

        finalStatesIterator.restartFrom(fsa.getEndNode(arc));
        while (finalStatesIterator.hasNext()) {
          final ByteBuffer bb = finalStatesIterator.next();
          final byte[] ba = bb.array();
          final int bbSize = bb.remaining();

          if (formsCount >= forms.length) {
            forms = Arrays.copyOf(forms, forms.length + EXPAND_SIZE);
            for (int k = 0; k < forms.length; k++) {
              if (forms[k] == null)
                forms[k] = new WordData(decoder);
            }
          }

          /*
           * Now, expand the prefix/ suffix 'compression' and store
           * the base form.
           */
          final WordData wordData = forms[formsCount++];
          if (dictionaryMetadata.getOutputConversionPairs().isEmpty()) {
            wordData.update(byteBuffer, word);
          } else {
            wordData.update(byteBuffer, applyReplacements(word, dictionaryMetadata.getOutputConversionPairs()));
          }

          /*
           * Find the separator byte's position splitting the inflection instructions
           * from the tag.
           */
          assert prefixBytes <= bbSize : sequenceEncoder.getClass() + " >? " + bbSize;
          int sepPos;
          for (sepPos = prefixBytes; sepPos < bbSize; sepPos++) {
            if (ba[sepPos] == separator) {
              break;
            }
          }

          /*
           * Decode the stem into stem buffer.
           */
          wordData.stemBuffer = sequenceEncoder.decode(wordData.stemBuffer,
                                                   byteBuffer,
                                                   ByteBuffer.wrap(ba, 0, sepPos));

          // Skip separator character.
          sepPos++;

          /*
           * Decode the tag data.
           */
          final int tagSize = bbSize - sepPos;
          if (tagSize > 0) {
            wordData.tagBuffer = BufferUtils.clearAndEnsureCapacity(wordData.tagBuffer, tagSize);
            wordData.tagBuffer.put(ba, sepPos, tagSize);
            wordData.tagBuffer.flip();
          }
        }

        formsList.wrap(forms, 0, formsCount);
      }
    } else {
      /*
       * this case is somewhat confusing: we should have hit the separator
       * first... I don't really know how to deal with it at the time
       * being.
       */
    }
    return formsList;
  }
1193592
@Override
    public void writeWord(final long word) {
        if(wordsWritten == wordCount) {
            throw new RuntimeException("Cannot write more words, backing array full!");
        }

        int bitsLeftInWord = wordLength;

        while(bitsLeftInWord > 0) {
            // Move to the next byte if the current one is fully packed.
            if(bitsLeftInByte == 0) {
                byteIndex++;
                bitsLeftInByte = BITS_PER_BYTE;
            }

            final long consumedMask;
            if(bitsLeftInWord == 64) {
                consumedMask = ~0L;
            } else {
                consumedMask = ((1L << bitsLeftInWord) - 1L);
            }

            // Fix how many bits will be written in this cycle. Choose the
            // smaller of the remaining bits in the word or byte.
            final int numberOfBitsToWrite = Math.min(bitsLeftInByte, bitsLeftInWord);
            final int bitsInByteRemainingAfterWrite = (bitsLeftInByte - numberOfBitsToWrite);

            // In general, we write the highest bits of the word first, so we
            // strip the highest bits that were consumed in previous cycles.
            final long remainingBitsOfWordToWrite = (word & consumedMask);

            final long bitsThatTheByteCanAccept;
            // If there is more left in the word than can be written to this
            // byte, shift off the bits that can't be written off the bottom.
            if(bitsLeftInWord > numberOfBitsToWrite) {
                bitsThatTheByteCanAccept = (remainingBitsOfWordToWrite >>> (bitsLeftInWord - bitsLeftInByte));
            } else {
                // If the byte can accept all remaining bits, there is no need
                // to shift off the bits that won't be written in this cycle.
                bitsThatTheByteCanAccept = remainingBitsOfWordToWrite;
            }

            // Align the word bits to write up against the byte bits that have
            // already been written. This shift may do nothing if the remainder
            // of the byte is being consumed in this cycle.
            final long alignedBits = (bitsThatTheByteCanAccept << bitsInByteRemainingAfterWrite);

            // Update the byte with the alignedBits.
            bytes[byteIndex] |= (byte)alignedBits;

            // Update state with bit count written.
            bitsLeftInWord -= numberOfBitsToWrite;
            bitsLeftInByte = bitsInByteRemainingAfterWrite;
        }

        wordsWritten ++;
    }
1190010
public static void init(String sensitiveWords, char separator, boolean isAsync){
		if(StrUtil.isNotBlank(sensitiveWords)){
			init(StrUtil.split(sensitiveWords, separator), isAsync);
		}
	}
1294327
public static boolean add(String word)
    {
        if (HanLP.Config.Normalization) word = CharTable.convert(word);
        if (contains(word)) return false;
        return insert(word, null);
    }
656350
private boolean includeInStartPosition(final int currentColumn, final int currentRow) {
        
        if(startColumn >=0 && startRow >= 0
                && currentColumn == startColumn && currentRow == startRow) {
            // 行と列の両方の指定がある場合
            return true;
            
        } else if(startColumn >= 0 && startRow < 0 && currentColumn == startColumn) {
            // 列の指定のみ
            return true;
            
        } else if(startColumn < 0 && startRow >= 0 && currentRow == startRow) {
            // 行の指定のみ
            return true;
            
        }
        
        return false;
        
    }
597644
public WordBuilder<I> repeatAppend(int num, Word<I> word) {
        if (num == 0) {
            return this;
        }

        int wLen = word.length();
        int allLen = wLen * num;

        ensureAdditionalCapacity(allLen);

        for (int i = num; i > 0; i--) {
            word.writeToArray(0, array, length, wLen);
            length += wLen;
        }

        return this;
    }
630853
def add_word(self, word, value):
		
		if not word:
			return

		node = self.root
		for c in word:
			try:
				node = node.children[c]
			except KeyError:
				n = TrieNode(c)
				node.children[c] = n
				node = n

		node.output = value
1053284
protected void configureStopWords (String stopWords)
    {
        List<String> patterns = Lists.newArrayList();
        for (StringTokenizer st = new StringTokenizer(stopWords); st.hasMoreTokens(); ) {
            patterns.add(getStopWordRegexp(st.nextToken()));
        }
        String pattern = patterns.isEmpty()
            ? ".\\A" // matches nothing
            : "(" + Joiner.on('|').join(patterns) + ")";
        setStopPattern(pattern);
    }
1244684
def _commonWordStart(self, words):
        
        if not words:
            return ''

        length = 0
        firstWord = words[0]
        otherWords = words[1:]
        for index, char in enumerate(firstWord):
            if not all([word[index] == char for word in otherWords]):
                break
            length = index + 1

        return firstWord[:length]
1245971
public static boolean setAttribute(String word, CoreDictionary.Attribute attribute)
    {
        if (attribute == null) return false;

        if (CoreDictionary.trie.set(word, attribute)) return true;
        if (CustomDictionary.dat.set(word, attribute)) return true;
        if (CustomDictionary.trie == null)
        {
            CustomDictionary.add(word);
        }
        CustomDictionary.trie.put(word, attribute);
        return true;
    }
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))
		);

}
923105
public void addWords(Collection<String> words){
		if(false == (words instanceof Set)){
			words = new HashSet<>(words);
		}
		for (String word : words) {
			addWord(word);
		}
	}
1370152
public boolean add( T word )
    {
        Entry<T> o = new Entry<T>(word, tail.prev, tail);
        tail.prev.next = o;
        tail.prev = o;
        
        //set the size and set the index
        size++;
        index.put(word.getValue(), word);
        
        return true;
    }
430825
public ArrayList<String> findReplacements(String word) {
    final List<CandidateData> result = findReplacementCandidates(word);

    final ArrayList<String> resultSuggestions = new ArrayList<String>(result.size());
    for (CandidateData cd : result) {
      resultSuggestions.add(cd.getWord());
    }
    return resultSuggestions;
  }