-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
__init__.py
82 lines (63 loc) · 2.37 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import re
from faker.utils.decorators import slugify_domain
from .. import Provider as InternetProvider
class Provider(InternetProvider):
free_email_domains = (
"hol.gr",
"gmail.com",
"hotmail.gr",
"yahoo.gr",
"googlemail.gr",
"otenet.gr",
"forthnet.gr",
)
tlds = ("com", "com", "com", "net", "org", "gr", "gr", "gr")
@slugify_domain
def user_name(self) -> str:
pattern: str = self.random_element(self.user_name_formats)
return latinize(self.bothify(self.generator.parse(pattern)))
@slugify_domain
def domain_word(self) -> str:
company = self.generator.format("company")
company_elements = company.split(" ")
company = latinize(company_elements.pop(0))
return company
# ``slugify`` doesn't replace greek glyphs.
def remove_accents(value: str) -> str:
"""
Remove accents from characters in the given string.
"""
search = "ΆΈΉΊΌΎΏάέήίόύώΪϊΐϋΰ"
replace = "ΑΕΗΙΟΥΩαεηιουωΙιιυυ"
def replace_accented_character(match):
matched = match.group(0)
if matched in search:
return replace[search.find(matched)]
return matched
return re.sub(r"[{}]+".format(search), replace_accented_character, value)
def latinize(value: str) -> str:
"""
Converts (transliterates) greek letters to latin equivalents.
"""
def replace_double_character(match):
search = ("Θ Χ Ψ " "θ χ ψ " "ΟΥ ΑΥ ΕΥ " "Ου Αυ Ευ " "ου αυ ευ").split()
replace = ("TH CH PS " "th ch ps " "OU AU EU " "Ou Au Eu " "ou au eu").split()
matched = match.group(0)
if matched in search:
return replace[search.index(matched)]
return matched
search = "ΑΒΓΔΕΖΗΙΚΛΜΝΞΟΠΡΣΣΤΥΦΩαβγδεζηικλμνξοπρσςτυφω"
replace = "AVGDEZIIKLMNXOPRSSTUFOavgdeziiklmnxoprsstyfo"
def replace_greek_character(match):
matched = list(match.group(0))
value = (replace[search.find(char)] for char in matched)
return "".join(value)
return re.sub(
r"[{}]+".format(search),
replace_greek_character,
re.sub(
r"([ΘΧΨθχψ]+|ΟΥ|ΑΥ|ΕΥ|Ου|Αυ|Ευ|ου|αυ|ευ)",
replace_double_character,
remove_accents(value),
),
)