-
Notifications
You must be signed in to change notification settings - Fork 1
/
obfuscator.py
140 lines (110 loc) · 3.52 KB
/
obfuscator.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
132
133
134
135
136
137
138
139
140
import random
import re
import util
NOUN_PREAMBLE = 'Given the following question, list the nouns. Return the answer as a list' \
' of bullet points, or say None if there are no items.'
PRONOUN_PREAMBLE = 'Given the following question, list the pronouns. Return the answer as a ' \
'list of bullet points, or say None if there are no items.'
PROPER_NOUN_PREAMBLE = 'Given the following question, list the proper nouns. Return the answer ' \
'as a list of bullet points, or say None if there are no items.'
MALE_NAME_PREAMBLE = 'Given the following question, list the males mentioned in the question. ' \
'Return the answer as a list of bullet points, or say None if there are no items.'
FEMALE_NAME_PREAMBLE = 'Given the following question, list the females mentioned in the question. ' \
'Return the answer as a list of bullet points, or say None if there are no items'
WEIRD_THINGS = [
'fluffernackle',
'spiggotwhap',
'blibberfudge',
'quizzletoe',
'plumbuzzle',
'trinkleshuff',
'flibberjig',
'whackledorf',
'snooptoggle',
'zibberflap',
'gobblequark',
'dinglefrap',
'snickerblast',
'floopernoodle',
'wobblegruff',
'crinklethorp',
'blubberclank',
'mumblestitch',
'quarkleflop',
'slippleshank'
]
FEMALE_NAMES = [
'Isolde',
'Seraphine',
'Elowen',
'Thalassa',
'Vespera',
'Calista',
'Ondine',
'Belphoebe',
'Isabeau',
'Eulalia',
]
MALE_NAMES = [
'Thorne',
'Jorvik',
'Elsdon',
'Calix',
'Vero',
'Orlan',
'Brevin',
'Tyrus',
'Halvard',
'Dainan'
]
def extract_list_items(list_string):
items = list_string.split('\n')
items = [item.replace('- ', '') for item in items]
items = list(filter(lambda x: x != 'None', items))
return items
def get_entities(preamble, query):
"""
Given a preamble, fetch a list of entities.
"""
prompt = f'{preamble}\n{query}'
response = util.call_gpt(prompt)
list_string = response.choices[0]['message']['content']
if list_string == 'None':
return []
return extract_list_items(list_string)
def get_proper_nouns(query):
return get_entities(PROPER_NOUN_PREAMBLE, query)
def get_nouns(query, no_numbers=True):
nouns = get_entities(NOUN_PREAMBLE, query)
if no_numbers:
nouns = list(filter(lambda n: not re.match(r'.*\d.*', n), nouns))
return nouns
def get_males(query):
return get_entities(MALE_NAME_PREAMBLE, query)
def get_females(query):
return get_entities(FEMALE_NAME_PREAMBLE, query)
def get_non_name_nouns(names, nouns):
noun_set = set(nouns)
name_set = set(names)
non_name_noun_set = set(noun_set).difference(name_set)
return list(non_name_noun_set)
def obfuscate(query):
males = get_males(query)
females = get_females(query)
people = males + females
nouns = get_nouns(query)
non_name_nouns = get_non_name_nouns(people, nouns)
random.shuffle(WEIRD_THINGS)
random.shuffle(MALE_NAMES)
random.shuffle(FEMALE_NAMES)
male_mapping = {real: fake for real, fake in zip(males, MALE_NAMES)}
female_mapping = {real: fake for real, fake in zip(females, FEMALE_NAMES)}
thing_mapping = {real: fake for real, fake in zip(non_name_nouns, WEIRD_THINGS)}
s = query
for real, fake in male_mapping.items():
s = s.replace(real, fake)
for real, fake in female_mapping.items():
s = s.replace(real, fake)
for real, fake in thing_mapping.items():
s = s.replace(real, fake)
return s