Files
Dalan/lib/barong/string.rb
2026-08-13 19:56:46 +03:30

50 lines
1.0 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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