
Homoglyphs: obtenha letras semelhantes, converta para ASCII, detecte possíveis idiomas e grupo UTF-8.
Forks: https://github.com/orsinium/forks
Homoglyphs -- biblioteca Python para obter homóglifos e converter para ASCII.
É uma versão mais inteligente do confusable_homoglyphs:
sudo pip install homoglyphs
A melhor forma de explicar algo é mostrar como funciona. Então, vamos dar uma olhada no uso real.
Importando:
import homoglyphs as hg
#detectar
hg.Languages.detect('w')
# {'pl', 'da', 'nl', 'fi', 'cz', 'sr', 'pt', 'it', 'en', 'es', 'sk', 'de', 'fr', 'ro'}
hg.Languages.detect('т')
# {'mk', 'ru', 'be', 'bg', 'sr'}
hg.Languages.detect('.')
# set()
# obter alfabeto para idiomas
hg.Languages.get_alphabet(['ru'])
# {'в', 'Ё', 'К', 'Т', ..., 'Р', 'З', 'Э'}
# obter todos os idiomas
hg.Languages.get_all()
# {'nl', 'lt', ..., 'de', 'mk'}
Categorias -- (alias do ISO 15924).
#detectar
hg.Categories.detect('w')
# 'LATIN'
hg.Categories.detect('т')
# 'CYRILLIC'
hg.Categories.detect('.')
# 'COMMON'
# obter alfabeto para categorias
hg.Categories.get_alphabet(['CYRILLIC'])
# {'ӗ', 'Ԍ', 'Ґ', 'Я', ..., 'Э', 'ԕ', 'ӻ'}
# obter todas as categorias
hg.Categories.get_all()
# {'RUNIC', 'DESERET', ..., 'SOGDIAN', 'TAI_LE'}
Obter homóglifos:
# get homoglyphs (latin alphabet initialized by default)
hg.Homoglyphs().get_combinations('q')
# ['q', '𝐪', '𝑞', '𝒒', '𝓆', '𝓺', '𝔮', '𝕢', '𝖖', '𝗊', '𝗾', '𝘲', '𝙦', '𝚚']
Carregamento de alfabeto:
# load alphabet on init by categories
homoglyphs = hg.Homoglyphs(categories=('LATIN', 'COMMON', 'CYRILLIC')) # alphabet loaded here
homoglyphs.get_combinations('гы')
# ['rы', 'гы', 'ꭇы', 'ꭈы', '𝐫ы', '𝑟ы', '𝒓ы', '𝓇ы', '𝓻ы', '𝔯ы', '𝕣ы', '𝖗ы', '𝗋ы', '𝗿ы', '𝘳ы', '𝙧ы', '𝚛ы']
# load alphabet on init by languages
homoglyphs = hg.Homoglyphs(languages={'ru', 'en'}) # alphabet will be loaded here
homoglyphs.get_combinations('гы')
# ['rы', 'гы']
# manual set alphabet on init # eng rus
homoglyphs = hg.Homoglyphs(alphabet='abc абс')
homoglyphs.get_combinations('с')
# ['c', 'с']
# load alphabet on demand
homoglyphs = hg.Homoglyphs(languages={'en'}, strategy=hg.STRATEGY_LOAD)
# ^ alphabet will be loaded here for "en" language
homoglyphs.get_combinations('гы')
# ^ alphabet will be loaded here for "ru" language
# ['rы', 'гы']
Você pode combinar categories, languages, alphabet e quaisquer estratégias como desejar. As estratégias especificam como lidar com caracteres ainda não carregados:
STRATEGY_LOAD: carregar categoria para este caractereSTRATEGY_IGNORE: adicionar caractere ao resultadoSTRATEGY_REMOVE: remover caractere do resultadohomoglyphs = hg.Homoglyphs(languages={'en'}, strategy=hg.STRATEGY_LOAD)
# convert
homoglyphs.to_ascii('ТЕСТ')
# ['TECT']
homoglyphs.to_ascii('ХР123.') # isto é cirílico "х" e "р"
# ['XP123.', 'XPI23.', 'XPl23.']
# string com caracteres que não podem ser convertidos por padrão será ignorada
homoglyphs.to_ascii('лол')
# []
# você pode definir estratégia para remover caracteres não ASCII não convertidos do resultado
homoglyphs = hg.Homoglyphs(
languages={'en'},
strategy=hg.STRATEGY_LOAD,
ascii_strategy=hg.STRATEGY_REMOVE,
)
homoglyphs.to_ascii('лол')
# ['o']
# também pode definir um intervalo de códigos de caracteres permitidos para ascii (0-128 por padrão):
homoglyphs = hg.Homoglyphs(
languages={'en'},
strategy=hg.STRATEGY_LOAD,
ascii_strategy=hg.STRATEGY_REMOVE,
ascii_range=range(ord('a'), ord('z')),
)
homoglyphs.to_ascii('ХР123.')
# ['l']
homoglyphs.to_ascii('хр123.')
# ['xpl']