Initial commit

This commit is contained in:
Yaser
2026-08-13 19:56:46 +03:30
commit de1d57a67b
474 changed files with 43185 additions and 0 deletions

49
lib/barong/string.rb Normal file
View 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