Class: Transliterator::Base

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/transliterator/base.rb

Direct Known Subclasses

Cyrillic, Greek, Latin

Constant Summary collapse

APPROXIMATIONS =
{
  "×" => "x",
  "÷" => "/",
  "" => "-",
  "" => "-",
  "" => "-",
  "" => "-",
  "" => "-",
  "" => "-",
  "" => "-",
  "" => "'",
  "" => "'",
  "" => '"',
  "" => '"',
  "" => '"',
  "" => '"',
  '' => "'",
  # various kinds of space characters
  "\xc2\xa0"     => " ",
  "\xe2\x80\x80" => " ",
  "\xe2\x80\x81" => " ",
  "\xe2\x80\x82" => " ",
  "\xe2\x80\x83" => " ",
  "\xe2\x80\x84" => " ",
  "\xe2\x80\x85" => " ",
  "\xe2\x80\x86" => " ",
  "\xe2\x80\x87" => " ",
  "\xe2\x80\x88" => " ",
  "\xe2\x80\x89" => " ",
  "\xe2\x80\x8a" => " ",
  "\xe2\x81\x9f" => " ",
  "\xe3\x80\x80" => " ",
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTransliterator::Base

Initializes a new Transliterator::Base instance.

Because Transliterator::Base is a singleton, you can only get an instance of it by calling the #instance class method on it:

Transliterator::Base.new      # => NoMethodError: private method `new' called for Transliterator::Base:Class
Transliterator::Base.instance # => #<Transliterator::Base:0x007f9b8c086e78>


54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/transliterator/base.rb', line 54

def initialize
  if self.class < Base
    @approximations = self.class.superclass.instance.approximations.dup
  else
    @approximations = {}
  end
  
  self.class::APPROXIMATIONS.inject(@approximations) do |memo, object|
    index       = object[0].unpack("U").shift
    value       = object[1].unpack("C*")
    memo[index] = value.length == 1 ? value[0] : value
    memo
  end
end

Instance Attribute Details

#approximationsObject (readonly)

Returns the value of attribute approximations.



43
44
45
# File 'lib/transliterator/base.rb', line 43

def approximations
  @approximations
end

Instance Method Details

#transliterate(string) ⇒ String

Transliterate a given string’s UTF-8 characters to their ASCII equivalants.

transliterator = Transliterator::Base.instance
transliterator.transliterate "5 × 10 ÷ 2 ‐ 5 = 20" # => "5 x 10 / 2 - 5 = 20"

Returns:

  • (String)

    The transliterated string



75
76
77
78
79
80
# File 'lib/transliterator/base.rb', line 75

def transliterate(string)
  string.unpack("U*")
        .map { |codepoint| approximations[codepoint] || codepoint }
        .flatten
        .pack("U*")
end