Skip to content

Instantly share code, notes, and snippets.

@c93614
Created December 26, 2014 21:12
Show Gist options
  • Save c93614/4bd5bec393605294a570 to your computer and use it in GitHub Desktop.
Save c93614/4bd5bec393605294a570 to your computer and use it in GitHub Desktop.
# https://ruby-china.org/topics/7555
# gem 'fast_trie', :require => "trie"
# encoding: utf-8
class SegTrie
attr_accessor :trie
def initialize
self.trie = ::Trie.new
end
def all_words(str, offset = 0, words= [])
words_a ||= []
str ||= ''
str.length.times do |i|
self.word(str, i, words_a)
end
words_a
end
def word(str, offset = 0, words_a = [])
len = 0
while offset + len < str.length
status = self.trie.get(str[offset, (len + 1)])
if status == 2
len = len + 1
words_a.push(str[offset, len])
elsif status == 1
len = len + 1
elsif status == 3
words_a.push(str[offset, len + 1])
break
else
break
end
end
words_a
end
def add_words(words)
words.each_index do |i|
self.add_word(words[i])
end
end
def add_word(word)
return false if word.blank?
word.length.times do |i|
next if i == 0
self.trie.add(word[0, i], 1) if self.trie.get(word[0, i]).to_i < 1
end
status =self.trie.get(word).to_i
if status == 1
self.trie.add(word, 2)
elsif !([2, 3].include?(status))
self.trie.add(word, 3)
end
end
def clear_words
self.trie = ::Trie.new
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment