/
number_tokenizer.rb
52 lines (44 loc) · 1.66 KB
/
number_tokenizer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# encoding: UTF-8
module TwitterCldr
module Tokenizers
class NumberTokenizer < Base
def initialize(options = {})
super(options)
@type = options[:type] || :decimal
@token_splitter_regex = /([^0*#,\.]*)([0#,\.]+)([^0*#,\.]*)$/
@token_type_regexes = [{ :type => :pattern, :regex => /[0?#,\.]*/ },
{ :type => :plaintext, :regex => // }]
@base_path = "numbers.formats"
@symbol_path = "numbers.symbols"
@paths = { :default => "patterns.default",
:positive => "patterns.positive",
:negative => "patterns.negative" }
end
def tokens(options = {})
unless self.traverse(self.full_path_for(:positive))
key_path = self.full_path_for(:default)
positive, negative = self.traverse(key_path).split(/;/)
insert_point = self.traverse(KeyPath.dirname(key_path))
insert_point[:positive] = positive
insert_point[:negative] = negative ? negative : "#{self.symbols[:minus] || '-'}#{positive}"
end
sign = options[:sign] || :positive
self.tokens_for(self.full_path_for(sign), nil)
end
def symbols
self.traverse(@symbol_path)
end
protected
def full_path_for(path)
KeyPath.join(@base_path, @type.to_s, self.paths[path])
end
def init_resources
@resource = TwitterCldr.get_resource(@locale, "numbers")[TwitterCldr.convert_locale(@locale)]
end
def pattern_for(resource)
# can't go any deeper, so return original pattern (which should NOT be a hash, by the way)
resource
end
end
end
end