ตัดคำภาษาไทยด้วยวิธี longest matching
481376
def segment(text: str, custom_dict: Trie = None) -> List[str]:
    """"""
    if not text or not isinstance(text, str):
        return []

    if not custom_dict:
        custom_dict = DEFAULT_DICT_TRIE

    return LongestMatchTokenizer(custom_dict).tokenize(text)
1226744
def bigram_compare(word1_bigrams, word2_bigrams)
      most_bigrams = [word1_bigrams.count, word2_bigrams.count].max
      num_matching(word1_bigrams, word2_bigrams).to_f / most_bigrams
    end
307125
def findAllMatches(self, needle, similarity):
        
        positions = []
        # Use findBestMatch to get the best match
        while True:
            best_match = self.findBestMatch(needle, similarity)
            if best_match is None: # No more matches
                break
            # Found a match. Add it to our list
            positions.append(best_match) # (position, confidence)

            # Erase the found match from the haystack.
            # Repeat this process until no other matches are found
            x, y = best_match[0]
            w = needle.shape[1]
            h = needle.shape[0]
            roi = (x, y, w, h)
            # numpy 2D slice
            roi_slice = (slice(roi[1], roi[1]+roi[3]), slice(roi[0], roi[0]+roi[2]))
            self.haystack[roi_slice] = 0

        # Whew! Let's see if there's a match after all that.
        positions.sort(key=lambda x: (x[0][1], x[0][0]))
        return positions
460178
def longest_increasing_subsequence(x):
    
    n = len(x)
    p = [None] * n
    h = [None]
    b = [float('-inf')]  # - infinity
    for i in range(n):
        if x[i] > b[-1]:
            p[i] = h[-1]
            h.append(i)
            b.append(x[i])
        else:
            #   -- binary search: b[k - 1] < x[i] <= b[k]
            k = bisect_left(b, x[i])
            h[k] = i
            b[k] = x[i]
            p[i] = h[k - 1]
    # extract solution
    q = h[-1]
    s = []
    while q is not None:
        s.append(x[q])
        q = p[q]
    return s[::-1]
86721
public static void main(String[] args) {

    String[] s = {"there once was a man", "this one is a manic", "hey there", "there once was a mane", "once in a manger.", "where is one match?", "Jo3seph Smarr!", "Joseph R Smarr"};
    for (int i = 0; i < 8; i++) {
      for (int j = 0; j < 8; j++) {
        System.out.println("s1: " + s[i]);
        System.out.println("s2: " + s[j]);
        System.out.println("edit distance: " + editDistance(s[i], s[j]));
        System.out.println("LCS:           " + longestCommonSubstring(s[i], s[j]));
        System.out.println("LCCS:          " + longestCommonContiguousSubstring(s[i], s[j]));
        System.out.println();
      }
    }
  }
616167
private function longestCommonPrefixLength($string1, $string2)
    {
        $length = min(strlen($string1), strlen($string2));
        $index  = 0;

        while ($index < $length) {
            if ($string1[$index] !== $string2[$index]) {
                break;
            }

            ++$index;
        }

        return $index;
    }
313386
def __matches(s1, s2, ngrams_fn, n=3):
    
    ngrams1, ngrams2 = set(ngrams_fn(s1, n=n)), set(ngrams_fn(s2, n=n))
    return ngrams1.intersection(ngrams2)
1000802
private void doSimpleMatching() {
        for (int i = 0; i < equalElements.size(); i++) {
            if (!alreadyMatched.get(i)) {
                List<Integer> equalTo = equalElements.get(i);
                if (equalTo.size() > 0) {
                    List<Integer> equivalentElements = getEquivalentElements(equalTo);

                    // We have the same set matching as is equivalent, we can remove them all
                    if (equalTo.size() == equivalentElements.size()) {
                        for (int j = 0; j < equivalentElements.size(); j++) {
                            recordMatch(equivalentElements.get(j), equalTo.get(j));
                        }
                    } else if (equivalentElements.size() > 1 && equalTo.size() > 1) {
                        List<Integer> equalToUsedOnlyInEquivalentElements = getEqualToUsedOnlyInEquivalentElements(equalTo, equivalentElements);
                        for (int j = 0; j < min(equivalentElements.size(), equalToUsedOnlyInEquivalentElements.size()); j++) {
                            recordMatch(equivalentElements.get(j), equalTo.get(j));
                        }
                    }
                }
            }
        }
    }
840385
static Matcher optimize(Matcher matcher) {
    Matcher m = matcher;
    Matcher opt = optimizeSinglePass(m);
    for (int i = 0; !m.equals(opt) && i < MAX_ITERATIONS; ++i) {
      m = opt;
      opt = optimizeSinglePass(m);
    }
    return opt;
  }
232625
public function longestCommonSubsequence(array $a, array $b)
    {
        $c = array();

        $m = count($a);
        $n = count($b);

        for ($i = 0; $i <= $m; $i++) {
            $c[$i][0] = 0;
        }

        for ($j = 0; $j <= $n; $j++) {
            $c[0][$j] = 0;
        }

        for ($i = 1; $i <= $m; $i++) {
            for ($j = 1; $j <= $n; $j++) {
                if ($this->matchStrategy->isMatch($a[$i - 1], $b[$j - 1])) {
                    $c[$i][$j] = 1 + (isset($c[$i - 1][$j - 1]) ? $c[$i - 1][$j - 1] : 0);
                } else {
                    $c[$i][$j] = max(
                        isset($c[$i][$j - 1]) ? $c[$i][$j - 1] : 0,
                        isset($c[$i - 1][$j]) ? $c[$i - 1][$j] : 0
                    );
                }
            }
        }

        $lcs = array_pad(array(), $m + 1, 0);
        $this->compileMatches($c, $a, $b, $m, $n, $lcs);

        return $lcs;
    }
559532
def best_match(given_word)
      words = (@word_list.is_a? Array) ? @word_list : @word_list.keys

      word_bigrams = bigramate(given_word)
      word_hash = words.map do |key|
        [key, bigram_compare(word_bigrams, bigramate(key))]
      end
      word_hash = Hash[word_hash]

      # Weight by word usage, if logical
      word_hash = apply_usage_weights(word_hash) if @word_list.is_a? Hash

      word_hash.max_by { |_key, value| value }.first
    end
354601
def similarity(self, other: Trigram) -> float:
        
        return max((self._match(x, other) for x in self.trigrams), default=0)
448850
def match(self, sentence, start=0, _v=None, _u=None):
        
        if sentence.__class__.__name__ == "Sentence":
            pass
        elif isinstance(sentence, list) or sentence.__class__.__name__ == "Text":
            return find(lambda m,s: m is not None, ((self.match(s, start, _v), s) for s in sentence))[0]
        elif isinstance(sentence, basestring):
            sentence = Sentence(sentence)
        elif isinstance(sentence, Match) and len(sentence) > 0:
            sentence = sentence[0].sentence.slice(sentence[0].index, sentence[-1].index + 1)
        # Variations (_v) further down the list may match words more to the front.
        # We need to check all of them. Unmatched variations are blacklisted (_u).
        # Pattern.search() calls Pattern.match() with a persistent blacklist (1.5x faster).
        a = []
        for sequence in (_v is not None and _v or self._variations()):
            if _u is not None and id(sequence) in _u:
                continue
            m = self._match(sequence, sentence, start)
            if m is not None:
                a.append((m.words[0].index, len(m.words), m))
            if m is not None and m.words[0].index == start:
                return m
            if m is None and _u is not None:
                _u[id(sequence)] = False
        # Return the leftmost-longest.
        if len(a) > 0:
            return sorted(a)[0][-1]
148056
def _check_for_answers(self, pk):
        
        longest_match = ()
        if len(self._answer_patterns) > 0:
            data = (pk.header,) + tuple(pk.data)
            for p in list(self._answer_patterns.keys()):
                logger.debug('Looking for pattern match on %s vs %s', p, data)
                if len(p) <= len(data):
                    if p == data[0:len(p)]:
                        match = data[0:len(p)]
                        if len(match) >= len(longest_match):
                            logger.debug('Found new longest match %s', match)
                            longest_match = match
        if len(longest_match) > 0:
            self._answer_patterns[longest_match].cancel()
            del self._answer_patterns[longest_match]
219427
def best_match(string_a, *string_b)
      similarities(string_a, *string_b).max_by { |_k, v| v }[0]
    end
636723
def char_matches(s1, s2, n=3):
    
    return __matches(s1, s2, char_ngrams, n=n)
251353
def longest_in_list2 list  #:nodoc:
      longest = list.inject(list[0]) do |memo,word|
        memo.length >= word.length ? memo : word
      end    
      longest
    end