FarsiFu 0.1.0
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.
- data/CHANGELOG +9 -0
- data/MIT-LICENSE +21 -0
- data/README +8 -0
- data/TODO +5 -0
- data/lib/farsi_fu.rb +124 -0
- data/test/test.rb +8 -0
- metadata +61 -0
data/CHANGELOG
ADDED
data/MIT-LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
== License
|
|
2
|
+
Copyright (c) 2008 Aziz Ashofte Bargi
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
5
|
+
a copy of this software and associated documentation files (the
|
|
6
|
+
"Software"), to deal in the Software without restriction, including
|
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
10
|
+
the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be
|
|
13
|
+
included in all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
19
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
20
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
21
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
data/TODO
ADDED
data/lib/farsi_fu.rb
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
#:title: FarsiFu
|
|
2
|
+
#:include:../README
|
|
3
|
+
#:include:../TODO
|
|
4
|
+
#:include:../CHANGELOG
|
|
5
|
+
#:include:../MIT-LICENSE
|
|
6
|
+
module FarsiFu
|
|
7
|
+
|
|
8
|
+
module NumbersExtensions
|
|
9
|
+
module InstanceMethods
|
|
10
|
+
# :stopdoc:
|
|
11
|
+
$KCODE = 'u'
|
|
12
|
+
require 'jcode'
|
|
13
|
+
|
|
14
|
+
PERSIAN_CHARS = "۱۲۳۴۵۶۷۸۹۰،×؛"
|
|
15
|
+
ENGLISH_CHARS = "1234567890,*;"
|
|
16
|
+
PERSIAN_DIGIT_JOINT = " و "
|
|
17
|
+
PERSIAN_DIGIT_SIGN = ["منفی ", "مثبت ", " ممیز "]
|
|
18
|
+
PERSIAN_DIGIT_SPELL = {
|
|
19
|
+
0 => [ nil ,"یک","دو","سه","چهار","پنج","شش","هفت","هشت","نه", "صفر"] ,
|
|
20
|
+
1 => [ nil ,"ده","بیست","سی","چهل","پنجاه","شصت","هفتاد","هشتاد","نود"],
|
|
21
|
+
2 => [ nil ,"صد","دویست","سیصد","چهارصد","پانصد","ششصد","هفتصد","هشتصد","نهصد"],
|
|
22
|
+
"10..19" => [ "ده" ,"یازده","دوازده","سیزده","چهارده","پانزده","شانزده","هفده","هشده","نوزده"],
|
|
23
|
+
"zillion" => [ nil ,"هزار","میلیون","میلیارد","بیلیون","تریلیون","کوادریلیون","کوینتیلیون","سیکستیلیون","سپتیلیون","اکتیلیون","نونیلیون","دسیلیون"],
|
|
24
|
+
"decimals" => [ nil, "دهم", "صدم", "هزارم", "دههزارم", "صدهزارم", "میلیونیم", "دهمیلیونیم","صدمیلیونیم","میلیاردیم"]
|
|
25
|
+
}
|
|
26
|
+
# :startdoc:
|
|
27
|
+
|
|
28
|
+
# Returns a string which is the equivalent English number of a Persian number (in String)
|
|
29
|
+
#
|
|
30
|
+
# Example:
|
|
31
|
+
#
|
|
32
|
+
# <tt>"۱۲۳".to_english # => "123" </tt>
|
|
33
|
+
def to_english
|
|
34
|
+
self.to_s.tr(PERSIAN_CHARS,ENGLISH_CHARS)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Returns a string which is the equivalent Persian number of an English number (in String)
|
|
38
|
+
# accepts instances of String, Integer and Numeric classes (Fixnum,Bignum and floats are accepted)
|
|
39
|
+
#
|
|
40
|
+
# alias: to_persian
|
|
41
|
+
#
|
|
42
|
+
# Example:
|
|
43
|
+
#
|
|
44
|
+
# <tt>"123".to_farsi # => "۱۲۳" </tt>
|
|
45
|
+
# <tt>"456".to_persian # => "۴۵۶" </tt>
|
|
46
|
+
# <tt> 789.to_farsi # => "۷۸۹" </tt>
|
|
47
|
+
def to_farsi
|
|
48
|
+
self.to_s.tr(ENGLISH_CHARS,PERSIAN_CHARS)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
alias_method :to_persian, :to_farsi
|
|
52
|
+
|
|
53
|
+
# Spells a number in Persian
|
|
54
|
+
# accpets english numbers (in float,fixnum or string)
|
|
55
|
+
#
|
|
56
|
+
# Example:
|
|
57
|
+
#
|
|
58
|
+
# <tt> 5678.spell_farsi # => "پنج هزار و ششصد و هفتاد و هشت" </tt>
|
|
59
|
+
def spell_farsi
|
|
60
|
+
# Distigushing the number (float and )
|
|
61
|
+
if self.class == Float
|
|
62
|
+
num_array = self.to_f.to_s.split(".").first.split(//).reverse
|
|
63
|
+
dec_array = self.to_f.to_s.split(".").last.split(//).slice(0..9).compact.reverse
|
|
64
|
+
dec_copy_b = dec_array.clone ; dec_copy_a = dec_array.clone
|
|
65
|
+
result = spell(num_array)
|
|
66
|
+
( result += PERSIAN_DIGIT_SIGN[2] + spell(dec_array) + PERSIAN_DIGIT_SPELL["decimals"][dec_copy_a.size].to_s ) unless [PERSIAN_DIGIT_SPELL[0][10],""].include? spell(dec_copy_b)
|
|
67
|
+
return result
|
|
68
|
+
else
|
|
69
|
+
num_array = self.to_i.to_s.split(//).reverse
|
|
70
|
+
return spell(num_array)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
private #---------------------------------------------------------
|
|
75
|
+
def spell(num_array)
|
|
76
|
+
# Dealing with signs
|
|
77
|
+
sign_m = num_array.include?("-") ? PERSIAN_DIGIT_SIGN[0] : ""
|
|
78
|
+
sign_p = num_array.include?("+") ? PERSIAN_DIGIT_SIGN[1] : ""
|
|
79
|
+
num_array.delete_at(num_array.index("-")) if sign_m.size > 0
|
|
80
|
+
num_array.delete_at(num_array.index("+")) if sign_p.size > 0
|
|
81
|
+
sign = sign_m + sign_p
|
|
82
|
+
|
|
83
|
+
zillion = 0
|
|
84
|
+
farsi_number = []
|
|
85
|
+
|
|
86
|
+
# Dealing with Zero
|
|
87
|
+
if (num_array.length == 1 && num_array[0] == "0" )
|
|
88
|
+
farsi_number = [PERSIAN_DIGIT_SPELL[0][10]]
|
|
89
|
+
num_array = []
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
while num_array.length > 0 do
|
|
93
|
+
spelling = []
|
|
94
|
+
num_array[0..2].each_with_index do |digit,index|
|
|
95
|
+
spelling[index] = PERSIAN_DIGIT_SPELL[index][digit.to_i]
|
|
96
|
+
if index == 1 && digit == "1" # Dealing with 10..19
|
|
97
|
+
spelling[1] = PERSIAN_DIGIT_SPELL["10..19"][num_array[0].to_i]
|
|
98
|
+
spelling[0] = nil
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
3.times { num_array.shift if num_array.length > 0 } # Eliminating the first 3 number of the array
|
|
103
|
+
dig = spelling.reverse.compact.join(PERSIAN_DIGIT_JOINT)
|
|
104
|
+
if dig.size > 0
|
|
105
|
+
dig << (" " + PERSIAN_DIGIT_SPELL["zillion"][zillion].to_s)
|
|
106
|
+
farsi_number.unshift(dig)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
zillion += 1
|
|
110
|
+
end # End of While
|
|
111
|
+
|
|
112
|
+
sign + farsi_number.compact.join(PERSIAN_DIGIT_JOINT)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
String.send(:include, FarsiFu::NumbersExtensions::InstanceMethods)
|
|
121
|
+
Integer.send(:include, FarsiFu::NumbersExtensions::InstanceMethods)
|
|
122
|
+
Numeric.send(:include, FarsiFu::NumbersExtensions::InstanceMethods)
|
|
123
|
+
#Fixnum.send(:include, FarsiFu::NumbersExtensions::InstanceMethods)
|
|
124
|
+
#Float.send(:include, FarsiFu::NumbersExtensions::InstanceMethods)
|
data/test/test.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: FarsiFu
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Aziz Ashofte Bargi
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2008-08-25 00:00:00 +02:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies: []
|
|
15
|
+
|
|
16
|
+
description:
|
|
17
|
+
email: aziz.bargi@gmail.com
|
|
18
|
+
executables: []
|
|
19
|
+
|
|
20
|
+
extensions: []
|
|
21
|
+
|
|
22
|
+
extra_rdoc_files:
|
|
23
|
+
- README
|
|
24
|
+
- CHANGELOG
|
|
25
|
+
- TODO
|
|
26
|
+
- MIT-LICENSE
|
|
27
|
+
files:
|
|
28
|
+
- lib/farsi_fu.rb
|
|
29
|
+
- test/test.rb
|
|
30
|
+
- README
|
|
31
|
+
- CHANGELOG
|
|
32
|
+
- TODO
|
|
33
|
+
- MIT-LICENSE
|
|
34
|
+
has_rdoc: true
|
|
35
|
+
homepage: http://farsifu.rubyforge.org
|
|
36
|
+
post_install_message:
|
|
37
|
+
rdoc_options: []
|
|
38
|
+
|
|
39
|
+
require_paths:
|
|
40
|
+
- lib
|
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: "0"
|
|
46
|
+
version:
|
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
48
|
+
requirements:
|
|
49
|
+
- - ">="
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: "0"
|
|
52
|
+
version:
|
|
53
|
+
requirements: []
|
|
54
|
+
|
|
55
|
+
rubyforge_project: FarsiFu
|
|
56
|
+
rubygems_version: 1.0.1
|
|
57
|
+
signing_key:
|
|
58
|
+
specification_version: 2
|
|
59
|
+
summary: A library for converting numbers to Persian digits and spelling numbers in Persian
|
|
60
|
+
test_files:
|
|
61
|
+
- test/test.rb
|