-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomEmoji.py
81 lines (69 loc) · 2.45 KB
/
randomEmoji.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
####
# Source: https://gist.github.com/shello/efa2655e8a7bce52f273
####
from itertools import accumulate
from bisect import bisect
from random import randrange
from unicodedata import name as unicode_name
# Set the unicode version.
# Your system may not support Unicode 7.0 charecters just yet! So hipster.
UNICODE_VERSION = 6
# Sauce: http://www.unicode.org/charts/PDF/U1F300.pdf
EMOJI_RANGES_UNICODE = {
6: [
("\U0001F300", "\U0001F320"),
("\U0001F330", "\U0001F335"),
("\U0001F337", "\U0001F37C"),
("\U0001F380", "\U0001F393"),
("\U0001F3A0", "\U0001F3C4"),
("\U0001F3C6", "\U0001F3CA"),
("\U0001F3E0", "\U0001F3F0"),
("\U0001F400", "\U0001F43E"),
("\U0001F440",),
("\U0001F442", "\U0001F4F7"),
("\U0001F4F9", "\U0001F4FC"),
("\U0001F500", "\U0001F53C"),
("\U0001F540", "\U0001F543"),
("\U0001F550", "\U0001F567"),
("\U0001F5FB", "\U0001F5FF"),
],
7: [
("\U0001F300", "\U0001F32C"),
("\U0001F330", "\U0001F37D"),
("\U0001F380", "\U0001F3CE"),
("\U0001F3D4", "\U0001F3F7"),
("\U0001F400", "\U0001F4FE"),
("\U0001F500", "\U0001F54A"),
("\U0001F550", "\U0001F579"),
("\U0001F57B", "\U0001F5A3"),
("\U0001F5A5", "\U0001F5FF"),
],
8: [
("\U0001F300", "\U0001F579"),
("\U0001F57B", "\U0001F5A3"),
("\U0001F5A5", "\U0001F5FF"),
],
}
NO_NAME_ERROR = "(No name found for this codepoint)"
def random_emoji(unicode_version=6):
if unicode_version in EMOJI_RANGES_UNICODE:
emoji_ranges = EMOJI_RANGES_UNICODE[unicode_version]
else:
emoji_ranges = EMOJI_RANGES_UNICODE[-1]
# Weighted distribution
count = [ord(r[-1]) - ord(r[0]) + 1 for r in emoji_ranges]
weight_distr = list(accumulate(count))
# Get one point in the multiple ranges
point = randrange(weight_distr[-1])
# Select the correct range
emoji_range_idx = bisect(weight_distr, point)
emoji_range = emoji_ranges[emoji_range_idx]
# Calculate the index in the selected range
point_in_range = point
if emoji_range_idx is not 0:
point_in_range = point - weight_distr[emoji_range_idx - 1]
# Emoji 😄
emoji = chr(ord(emoji_range[0]) + point_in_range)
emoji_name = unicode_name(emoji, NO_NAME_ERROR).capitalize()
emoji_codepoint = "U+{}".format(hex(ord(emoji))[2:].upper())
return (emoji, emoji_codepoint, emoji_name)