forked from epogrebnyak/as-ikea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
as_ikea.py
89 lines (67 loc) · 2.45 KB
/
as_ikea.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
83
84
85
86
87
88
89
"""Make your last name sound as an Ikea product name.
As seen in https://www.facebook.com/max.abelev/posts/3347430315294332
"""
import random
from typing import List
# EP: note this is a very rich mapper, will convert every letter to
# something Swedish, this may not be the result you want as
# some names look cool with just one accented letter, not many letters
# like this.
#
# To enquire about "Umlaut" see https://ru.wikipedia.org/wiki/%D0%A3%D0%BC%D0%BB%D0%B0%D1%83%D1%82
mapper = dict(
a=["à", "á", "â", "ä", "æ", "ã", "ā"],
o=["ô", "ö", "ò", "ó", "œ", "ø", "ō"],
u=["û", "ü", "ù", "ú", "ū"],
e=["è", "é", "ê", "ë", "ē", "ė"],
)
def substitute(letter: str, mapper: dict = mapper) -> str:
"""
Change 'a', 'o', 'u', 'e' to alternated variants randomly.
"""
return random.choice(mapper[letter])
def reverse(s):
return s[::-1]
def positions(name, mapper=mapper) -> List[int]:
pos = []
targets = mapper.keys()
for i, letter in enumerate(name.lower()):
if letter in targets:
pos.append(i)
return pos
def as_ikea(name: str) -> str:
"""
Convert *name* to Ikea-like name.
"""
# Replace just once
name=name.lower()
index = random.choice(positions(name))
name = replace_at(name, index)
return reverse(name).title()
def replace_at(name, index):
new_letter = substitute(name[index])
return replace_once(name, new_letter, index)
def replace_once(xs: str, x: str, i: int) -> str:
"""
Insert *x* at position *i* in *xs*.
"""
assert len(x) == 1
return xs[:i] + x + xs[i + 1 :]
if __name__ == "__main__":
print(as_ikea("Abelev"))
# Possible more serious applications:
# - how we can get real array of Ikea products?
# - does product naming affect market success? See eg https://www.esic.edu/documentos/revistas/esicmk/1574698527_I.pdf
# - is there A/B test evidence for this that?
# Can the name be taken for Swedesh?
# - can we make a properly sounding generative model for the products?
# - can we calculate the likelyhood it is Swedish?
# - what should be the probabilities of changing the letter (maybe due to position)?
# Ideas on web implementation:
# - FastAPI for API
# - something like Heroku for quick launch as a web service?
# For initial task (done):
# - can you write a pseudocode for this procedure?
# - add more tests that fail
# - should there be just one letter change?
# - does code need more refactoring?