class TwitterCldr::Utils::ScriptDetector

Constants

PROPERTY_NAME

Public Class Methods

detect_scripts(text) click to toggle source
# File lib/twitter_cldr/utils/script_detector.rb, line 37
def detect_scripts(text)
  length = text.length.to_f

  ScriptDetectionResult.new(
    scores_for(text).each_with_object({}) do |(script_name, count), ret|
      ret[script_name] = count / length
    end
  )
end

Private Class Methods

properties() click to toggle source
# File lib/twitter_cldr/utils/script_detector.rb, line 71
def properties
  TwitterCldr::Shared::CodePoint.properties
end
scores_for(text) click to toggle source
# File lib/twitter_cldr/utils/script_detector.rb, line 49
def scores_for(text)
  Hash.new(0).tap do |result|
    text.chars.each do |char|
      script = scripts_hash[char]
      result[script] += 1 if script
    end
  end
end
scripts() click to toggle source
# File lib/twitter_cldr/utils/script_detector.rb, line 67
def scripts
  @scripts ||= properties.property_values_for(PROPERTY_NAME)
end
scripts_hash() click to toggle source
# File lib/twitter_cldr/utils/script_detector.rb, line 58
def scripts_hash
  @scripts_hash ||= scripts.each_with_object({}) do |script_name, ret|
    code_points = properties.code_points_for_property(PROPERTY_NAME, script_name)
    code_points.each do |code_point|
      ret[[code_point].pack("U*")] = script_name
    end
  end
end