-
Notifications
You must be signed in to change notification settings - Fork 2
/
EmojiFinder.py
131 lines (109 loc) · 4.46 KB
/
EmojiFinder.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import pandas as pd
import sqlite3
SKIN_TONE_SUFFIXES = [
'medium-light_skin_tone',
'light_skin_tone',
'medium_skin_tone',
'medium-dark_skin_tone',
'dark_skin_tone',
]
def flatten_list(list_of_lists):
return [y for x in list_of_lists for y in x]
def filter_list(list1, all_labels):
return sorted(list(set(list1).intersection(all_labels)))
class EmojiFinderCached:
def __init__(self, model_name='all-mpnet-base-v2'):
self.emoji_df = pd.read_parquet('emoji_df_improved.parquet')
self.emoji_dict = self.emoji_df.set_index('label')['emoji'].to_dict()
self.emoji_text_dict = self.emoji_df.set_index('label')['text'].to_dict()
vocab_df = pd.read_parquet(f'vocab_df_{model_name}.parquet')
self.vocab_dict = vocab_df.set_index('word')['idx'].to_dict()
self.distances = pd.read_parquet(f'semantic_distances_{model_name}.parquet').values
@staticmethod
def add_variants(base_label, all_labels):
# print(base_label)
base_search = base_label[1:-1]
if base_search in SKIN_TONE_SUFFIXES:
return []
for prefix in ['person_', 'man_', 'woman_']:
if base_search.startswith(prefix):
base_search = base_search.replace(prefix, '')
# print(f'new base {base_search}')
break
variants = [f':{base_search}_{x}:' for x in SKIN_TONE_SUFFIXES]
# print(variants)
man_variants = [':man_' + base[1:] for base in variants] + [f':man_{base_search}:']
woman_variants = [':woman_' + base[1:] for base in variants] + [
f':woman_{base_search}:'
]
person_variants = [':person_' + base[1:] for base in variants] + [
f':person_{base_search}:'
]
extra_suffixes = flatten_list(
[
[f':{gender}_{x}_{base_search}:' for x in SKIN_TONE_SUFFIXES]
for gender in ['man', 'woman', 'person']
]
)
# print(len(variants))
return (
filter_list(variants, all_labels)
+ filter_list(woman_variants, all_labels)
+ filter_list(man_variants, all_labels)
+ filter_list(person_variants, all_labels)
+ filter_list(extra_suffixes, all_labels)
)
def top_emojis(self, search):
search = search.strip().lower()
if idx := self.vocab_dict.get(search):
return self.emoji_df.iloc[(self.distances[idx])].query('version <= 15.0')
else:
return pd.DataFrame(columns=['text', 'emoji'])
class EmojiFinderSql(EmojiFinderCached):
def __init__(self, db_name='all-mpnet-base-v2_main.db'):
self.db_name = db_name
print('Begin init of class')
# self.con = sqlite3.connect(
# 'main.db') #change later, name should have model type in it
# self.base_emoji_map = self.make_variant_map()
print('end init of class')
def prep_for_variant_map(self):
self.all_labels = pd.read_sql('select distinct label from emoji_df;', con=self.con)[
'label'
].tolist()
self.make_variant_map()
def sql_frame(self, query, params=None):
with sqlite3.connect(self.db_name) as con:
return pd.read_sql(query, con=con, params=params)
def new_emoji_dict(self, label):
# print(df.shape)
# return df
return dict(
zip(
['idx', 'emoji', 'label', 'version', 'text', 'base_emoji'],
self.con.execute(
'Select * from emoji_df where label = ?;', (label,)
).fetchone(),
)
)
def sql_add_variants(self, label):
return self.sql_frame(
'select label from emoji_df where base_emoji = ? and base_emoji <> label',
params=(label,),
)['label'].to_list()
@property
def con(self):
return sqlite3.connect(self.db_name)
def filter_list(self, list1):
return sorted(list(set(list1).intersection(self.all_labels)))
def top_emojis(self, search):
search = search.strip().lower()
results = pd.read_sql(
'select * from combined_emoji where word = ? and label = base_emoji order by rank_of_search;',
con=self.con,
params=(search,),
)
if not results.empty:
return results.query('version <= 15.1') ## move this into sql and add index?
else:
return pd.DataFrame(columns=['text', 'emoji'])