module TwitterCldr::Collation::TrieBuilder

Builds a fractional collation elements Trie from the file containing a fractional collation elements table.

Constants

COLLATION_ELEMENT_REGEXP
FRACTIONAL_UCA_SHORT_PATH

Public Class Methods

load_default_trie() click to toggle source
# File lib/twitter_cldr/collation/trie_builder.rb, line 19
def load_default_trie
  File.open(FRACTIONAL_UCA_SHORT_PATH, 'r') { |table| parse_collation_elements_table(table) }
end
load_tailored_trie(locale, fallback) click to toggle source
# File lib/twitter_cldr/collation/trie_builder.rb, line 23
def load_tailored_trie(locale, fallback)
  build_tailored_trie(tailoring_data(locale), fallback)
end
tailoring_data(locale) click to toggle source
# File lib/twitter_cldr/collation/trie_builder.rb, line 27
def tailoring_data(locale)
  TwitterCldr.get_resource(:collation, :tailoring, locale)
end

Private Class Methods

build_tailored_trie(tailoring_data, fallback) click to toggle source
# File lib/twitter_cldr/collation/trie_builder.rb, line 51
def build_tailored_trie(tailoring_data, fallback)
  trie = TwitterCldr::Collation::TrieWithFallback.new(fallback)

  parse_collation_elements_table(tailoring_data[:tailored_table], trie)
  copy_expansions(trie, fallback, parse_suppressed_starters(tailoring_data[:suppressed_contractions]))

  trie
end
copy_expansions(trie, source_trie, suppressed_starters) click to toggle source
# File lib/twitter_cldr/collation/trie_builder.rb, line 60
def copy_expansions(trie, source_trie, suppressed_starters)
  suppressed_starters.each do |starter|
    trie.add([starter], source_trie.get([starter]))
  end

  (trie.starters - suppressed_starters).each do |starter|
    source_trie.each_starting_with(starter) do |key, value|
      trie.add(key, value)
    end
  end
end
parse_code_points(string) click to toggle source
# File lib/twitter_cldr/collation/trie_builder.rb, line 41
def parse_code_points(string)
  string.split.map(&:hex)
end
parse_collation_element(string) click to toggle source
# File lib/twitter_cldr/collation/trie_builder.rb, line 45
def parse_collation_element(string)
  string.scan(/\[.*?\]/).map do |match|
    match[1..-2].gsub(/\s/, '').split(',', -1).map { |bytes| bytes.hex }
  end
end
parse_collation_elements_table(table, trie = TwitterCldr::Utils::Trie.new) click to toggle source
# File lib/twitter_cldr/collation/trie_builder.rb, line 33
def parse_collation_elements_table(table, trie = TwitterCldr::Utils::Trie.new)
  table.each_line do |line|
    trie.set(parse_code_points($1), parse_collation_element($2)) if COLLATION_ELEMENT_REGEXP =~ line
  end

  trie
end
parse_suppressed_starters(suppressed_contractions) click to toggle source
# File lib/twitter_cldr/collation/trie_builder.rb, line 72
def parse_suppressed_starters(suppressed_contractions)
  suppressed_contractions.chars.map do |starter|
    TwitterCldr::Utils::CodePoints.from_string(starter).first
  end
end