Initial commit
This commit is contained in:
49
lib/barong/string.rb
Normal file
49
lib/barong/string.rb
Normal file
@@ -0,0 +1,49 @@
|
||||
class String
|
||||
CHARACTER_MAPPING = {
|
||||
'ك' => 'ک',
|
||||
'دِ' => 'د',
|
||||
'بِ' => 'ب',
|
||||
'زِ' => 'ز',
|
||||
'ذِ' => 'ذ',
|
||||
'شِ' => 'ش',
|
||||
'سِ' => 'س',
|
||||
'ى' => 'ی',
|
||||
'ي' => 'ی',
|
||||
'١' => '۱',
|
||||
'٢' => '۲',
|
||||
'٣' => '۳',
|
||||
'٤' => '۴',
|
||||
'٥' => '۵',
|
||||
'٦' => '۶',
|
||||
'٧' => '۷',
|
||||
'٨' => '۸',
|
||||
'٩' => '۹',
|
||||
'٠' => '۰'
|
||||
}.freeze
|
||||
|
||||
def to_persian
|
||||
gsub(Regexp.union(CHARACTER_MAPPING.keys), CHARACTER_MAPPING)
|
||||
end
|
||||
|
||||
def percent_match(mold)
|
||||
mold = mold.to_s.to_persian
|
||||
str = self.to_persian
|
||||
char_cost = (100.to_f / length).round(2)
|
||||
matching = 0
|
||||
str_index = 0
|
||||
mold_index = 0
|
||||
str.each_char.with_index do |_char, index|
|
||||
break if mold[mold_index].blank?
|
||||
|
||||
if mold[mold_index] != str[str_index]
|
||||
mold_index = index - 1 if mold.length < str.length
|
||||
str_index = index - 1 if mold.length > str.length
|
||||
else
|
||||
matching += char_cost
|
||||
end
|
||||
str_index += 1
|
||||
mold_index += 1
|
||||
end
|
||||
matching.round
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user