Files
hudozka/modules/sync/utils/text/transform.py
T
2020-02-23 21:25:17 +03:00

95 lines
1.5 KiB
Python
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.
import re
from unidecode import unidecode
from utils.fn import compose
remove_pattr = re.compile('[«»<>#,!&"\']')
reduce_dash_patterns = [
(re.compile('[-]+'), '-'),
(re.compile('^-'), '')
]
def translit(text):
"""
RUSSIAN TEXT => ENGLISH TEXT
"""
return unidecode(text)
def interpolate(text, storage, key_fn=None):
if not key_fn:
key_fn = lambda i: i
key = key_fn(text)
return storage[key] if key in storage else text
def interpolate_swift(i):
return re.search("([\d\w-]+)", i).group(1)
def remove_punctuation(i):
return remove_pattr.sub('', i)
def reduce_dashes(i):
value = i
for reg_exp, replace in reduce_dash_patterns:
value = reg_exp.sub(replace, value)
return value
def space_to_dash(i):
return i.replace(' ', '-')
def short_dash(i):
"""
minus -> dash
tire -> dash
:param i:
:return:
"""
return i.replace('–', '-').replace('—', '-')
def dot_to_dash(i):
return i.replace('.', '-')
def slash_to_dash(i):
return i.replace('/', '-')
def remove_underscores(i):
return i.replace('_', '')
def text_lower(i):
return i.lower()
url_encode_file = compose(
translit,
space_to_dash,
short_dash,
remove_underscores,
remove_punctuation,
reduce_dashes,
text_lower,
)
url_encode_text = compose(
translit,
space_to_dash,
dot_to_dash,
short_dash,
slash_to_dash,
remove_underscores,
remove_punctuation,
reduce_dashes,
text_lower,
)