NumberSayer 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 09b17000708f07caca19344afcc43b414bc1d18e
4
+ data.tar.gz: fe3380cab1a6b895b334c5484f72907bc603e629
5
+ SHA512:
6
+ metadata.gz: 08367d002adb82e0dbc9e76386cf856ab2b67bdf4552e6b983743a272f365c4f58e6608e7b6d049eaad85791679e42bef7ef4040dfc8d62a34ce54a400516514
7
+ data.tar.gz: 990da246ef56ee2f1685cdcbe75c05518c5190048e772c9513113e2bac124a4a71b4a620878eb59815710461b248aac1aba9de8324a81ca73760138680dc0568
@@ -0,0 +1,43 @@
1
+ require_relative './supporting_classes/parse_belasan_indonesia'
2
+ require_relative './supporting_classes/parse_belasan_english'
3
+ require_relative './supporting_classes/digits_parser'
4
+
5
+ class NumberSayer
6
+
7
+ def initialize
8
+ @two_digits_parser = DigitsParser.new
9
+
10
+ @satuans_id = ["nol", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan"]
11
+ @adjective_sayings_id = {puluh: "puluh", belas: "belas", ratusan: "ratus", ribuan: "ribu", jutaan: "juta", milyaran: "milyar"}
12
+ @anomalies_id = {a10: "sepuluh", a11: "sebelas", a100:"seratus", a1rb: "seribu", a1jt: "sejuta", a1M:"semilyar"}
13
+ @belasan_parser_id = ParseBelasanIndonesia.new @satuans_id, @anomalies_id, @adjective_sayings_id
14
+
15
+ @satuans_en = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
16
+ @adjective_sayings_en = {ratusan: "hundred", ribuan: "thousand", jutaan: "million", milyaran: "billion"}
17
+ @anomalies_en = {a10: "ten", a11: "eleven", a12: "twelve", a13:"thirteen", a14:"fourteen", a15:"fifteen",
18
+ a16: "sixteen", a17:"seventeen", a18: "eighteen", a19:"nineteen",
19
+ a20: "twenty", a30: "thirty", a40: "fourty", a50: "fifty", a60: "sixty", a70: "seventy", a80:"eighty", a90:"ninety",
20
+ a100:"one hundred", a1rb: "one thousand", a1jt: "one million", a1M:"one billion"}
21
+ @belasan_parser_en = ParseBelasanEnglish.new @satuans_en, @anomalies_en, @adjective_sayings_en
22
+ end
23
+
24
+ def say(number, language = :id)
25
+ if language == :id
26
+ @two_digits_parser.satuans = @satuans_id
27
+ @two_digits_parser.adjective_sayings = @adjective_sayings_id
28
+ @two_digits_parser.anomalies = @anomalies_id
29
+ @two_digits_parser.belasan_parser = @belasan_parser_id
30
+ return @two_digits_parser.say number
31
+ elsif language == :en
32
+ @two_digits_parser.satuans = @satuans_en
33
+ @two_digits_parser.adjective_sayings = @adjective_sayings_en
34
+ @two_digits_parser.anomalies = @anomalies_en
35
+ @two_digits_parser.belasan_parser = @belasan_parser_en
36
+ return @two_digits_parser.say number
37
+ else
38
+ raise "Unknown language"
39
+ end
40
+ end
41
+ end
42
+
43
+
@@ -0,0 +1,137 @@
1
+ class DigitsParser
2
+ attr_accessor :satuans
3
+ attr_accessor :adjective_sayings
4
+ attr_accessor :anomalies
5
+ attr_accessor :belasan_parser
6
+
7
+ def initialize
8
+ @satuans = ["nol", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan"]
9
+ @adjective_sayings = {puluh: "puluh", belas: "belas", ratusan: "ratus", ribuan: "ribu", jutaan: "juta", milyaran: "milyar"}
10
+ @anomalies = {a10: "sepuluh", a11: "sebelas", a100:"seratus", a1rb: "seribu", a1jt: "sejuta", a1M:"semilyar"}
11
+ end
12
+
13
+ def parse_belasan(param)
14
+ @belasan_parser.parse param
15
+ end
16
+
17
+ def handle_digit(i, current_digit, digit_adjectives, results)
18
+ front_digit = digit_adjectives[i - 1][1]
19
+ if front_digit == "0"
20
+ if current_digit != "0"
21
+ satuan = @satuans[current_digit.to_i]
22
+ results.push satuan
23
+ end
24
+ else
25
+ parse_belasan front_digit: front_digit, current_digit: current_digit, destination: results
26
+ end
27
+ end
28
+
29
+ def handle_hundreds(current_digit, results)
30
+ if current_digit == "1"
31
+ results.push @anomalies[:a100]
32
+ else
33
+ if current_digit != "0"
34
+ ratusan = @satuans[current_digit.to_i]
35
+ results.push "#{ratusan} #{@adjective_sayings[:ratusan]}"
36
+ end
37
+ end
38
+ end
39
+
40
+ def handle_thousands(i, current_digit, current_adjective, digit_adjectives, results)
41
+ has_puluhan = i > 0
42
+ if !has_puluhan
43
+ if current_digit == "1"
44
+ if current_adjective == :ribuan
45
+ results.push "#{@anomalies[:a1rb]}"
46
+ else
47
+ results.push "#{@anomalies[:a1jt]}"
48
+ end
49
+ else
50
+ ribuan = @satuans[current_digit.to_i]
51
+ results.push "#{ribuan} #{@adjective_sayings[current_adjective]}"
52
+ end
53
+ else
54
+ front_digit = digit_adjectives[i - 1][1]
55
+ if front_digit == "0"
56
+ if current_digit != "0"
57
+ satuan = satuans[current_digit.to_i]
58
+ results.push "#{satuan}"
59
+ results.push @adjective_sayings[current_adjective]
60
+ else
61
+ ratusan_val = digit_adjectives[i - 2][1]
62
+ if ratusan_val != "0"
63
+ results.push @adjective_sayings[current_adjective]
64
+ end
65
+ end
66
+ else
67
+ parse_belasan front_digit: front_digit, current_digit: current_digit, destination: results
68
+ results.push @adjective_sayings[current_adjective]
69
+ end
70
+ end
71
+ end
72
+
73
+ def get_digit_adjectives(number, adjectives)
74
+ adjective_index = 0
75
+
76
+ digit_adjectives = []
77
+
78
+ number_s = number.to_s
79
+
80
+ backward_index = number_s.length - 1
81
+ while backward_index >= 0
82
+
83
+ digit_s = number_s[backward_index]
84
+ digit_adjectives.push [adjectives[adjective_index], digit_s]
85
+
86
+ adjective_index += 1
87
+
88
+ backward_index -= 1
89
+ end
90
+
91
+ digit_adjectives
92
+ end
93
+
94
+ def parse(digit_adjectives, results)
95
+ digit_adjectives.each_with_index do |digit_adjective, i|
96
+
97
+ current_adjective = digit_adjective[0]
98
+ current_digit = digit_adjective[1]
99
+
100
+ if current_adjective == :satuan
101
+ handle_digit i, current_digit, digit_adjectives, results
102
+ end
103
+
104
+ if current_adjective == :ratusan or current_adjective == :ratusan_ribu or
105
+ current_adjective == :ratusan_juta or current_adjective == :ratusan_milyar
106
+
107
+ handle_hundreds current_digit, results
108
+
109
+ end
110
+
111
+ if current_adjective == :ribuan or current_adjective == :jutaan or current_adjective == :milyaran
112
+ handle_thousands i, current_digit, current_adjective, digit_adjectives, results
113
+ end
114
+ end
115
+
116
+ end
117
+
118
+ def say(number)
119
+ adjectives = [:satuan, :puluhan, :ratusan, :ribuan, :puluhan_ribu, :ratusan_ribu, :jutaan, :puluhan_juta, :ratusan_juta, :milyaran, :puluhan_milyar, :ratusan_milyar]
120
+
121
+
122
+ if number < 10
123
+ return satuans[number]
124
+ else # 10, 11, 19, 20...99, 100
125
+ results = []
126
+
127
+ digit_adjectives = get_digit_adjectives number, adjectives
128
+
129
+ digit_adjectives = digit_adjectives.reverse
130
+
131
+ parse digit_adjectives, results
132
+
133
+ return results.join " "
134
+ end
135
+
136
+ end
137
+ end
@@ -0,0 +1,36 @@
1
+ class ParseBelasanEnglish
2
+ def initialize(satuans, anomalies, adjective_sayings)
3
+ @satuans = satuans
4
+ @anomalies = anomalies
5
+ @adjective_sayings = adjective_sayings
6
+ end
7
+ def parse(param)
8
+
9
+ front_digit = param[:front_digit]
10
+ current_digit = param[:current_digit]
11
+ results = param[:destination]
12
+ satuans = @satuans
13
+ if front_digit == "1"
14
+ if current_digit == "0" #10
15
+ results.push @anomalies[:a10]
16
+ elsif current_digit == "1" #11
17
+ results.push @anomalies[:a11]
18
+ else # 12-19
19
+ satuan = satuans[current_digit.to_i]
20
+ symbol_string = "a#{front_digit}#{current_digit}"
21
+ results.push "#{@anomalies[symbol_string.to_sym]}"
22
+ end
23
+ else #20
24
+ if front_digit != "0"
25
+ puluhan = satuans[front_digit.to_i]
26
+ symbol_string = "a#{front_digit}0"
27
+ results.push "#{@anomalies[symbol_string.to_sym]}"
28
+
29
+ if current_digit != "0"
30
+ satuan = satuans[current_digit.to_i]
31
+ results.push satuan
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,38 @@
1
+ class ParseBelasanIndonesia
2
+ def initialize(satuans, anomalies, adjective_sayings)
3
+ @satuans = satuans
4
+ @anomalies = anomalies
5
+ @adjective_sayings = adjective_sayings
6
+ end
7
+ def parse(param)
8
+
9
+ front_digit = param[:front_digit]
10
+ current_digit = param[:current_digit]
11
+ results = param[:destination]
12
+ satuans = @satuans
13
+ if front_digit == "1"
14
+ if current_digit == "0" #10
15
+ # results.push "sepuluh"
16
+ results.push @anomalies[:a10]
17
+ elsif current_digit == "1" #11
18
+ # results.push "sebelas"
19
+ results.push @anomalies[:a11]
20
+ else # 12-19
21
+ satuan = satuans[current_digit.to_i]
22
+ # results.push "#{satuan} belas"
23
+ results.push "#{satuan} #{@adjective_sayings[:belas]}"
24
+ end
25
+ else #20
26
+ if front_digit != "0"
27
+ puluhan = satuans[front_digit.to_i]
28
+ # results.push "#{puluhan} puluh"
29
+ results.push "#{puluhan} #{@adjective_sayings[:puluh]}"
30
+
31
+ if current_digit != "0"
32
+ satuan = satuans[current_digit.to_i]
33
+ results.push satuan
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: NumberSayer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Irwansyah
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Convert from number to Indonesian and English (or any other language)
14
+ text
15
+ email: irwansyah@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/number_sayer.rb
21
+ - lib/supporting_classes/digits_parser.rb
22
+ - lib/supporting_classes/parse_belasan_english.rb
23
+ - lib/supporting_classes/parse_belasan_indonesia.rb
24
+ homepage: https://github.com/irwansyahwii/NumberSayer
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.4.3
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Convert from number to Indonesian and English (or any other language) text
48
+ test_files: []