-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpostprocess.py
195 lines (186 loc) · 4.83 KB
/
postprocess.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from typing import List
import random
import re
from nltk.tokenize import word_tokenize
FEMALE_WORDS: List[str] = list([
"she",
"daughter",
"hers",
"her",
"mother",
"woman",
"girl",
"herself",
"female",
"sister",
"daughters",
"mothers",
"women",
"girls",
"femen",
"sisters",
"aunt",
"aunts",
"niece",
"nieces",
])
MALE_WORDS: List[str] = list([
"he",
"son",
"his",
"him",
"father",
"man",
"boy",
"himself",
"male",
"brother",
"sons",
"fathers",
"men",
"boys",
"males",
"brothers",
"uncle",
"uncles",
"nephew",
"nephews",
])
ASIAN_NAMES= list([
"cho",
"wong",
"tang",
"huang",
"chu",
"chung",
"ng",
"wu",
"liu",
"chen",
"lin",
"yang",
"kim",
"chang",
"shah",
"wang",
"li",
"khan",
"singh",
"hong",
])
HISPANIC_NAMES = list([
"castillo",
"gomez",
"soto",
"gonzalez",
"sanchez",
"rivera",
"martinez",
"torres",
"rodriguez",
"perez",
"lopez",
"medina",
"diaz",
"garcia",
"castro",
"cruz",
])
WHITE_NAMES = list([
"harris",
"nelson",
"robinson",
"thompson",
"moore",
"wright",
"anderson",
"clark",
"jackson",
"taylor",
"scott",
"davis",
"allen",
"adams",
"lewis",
"williams",
"jones",
"wilson",
"martin",
"johnson",
])
name2idx = {}
WHITE_NAMES = [name.title() for name in WHITE_NAMES]
HISPANIC_NAMES = [name.title() for name in HISPANIC_NAMES]
ASIAN_NAMES = [name.title() for name in ASIAN_NAMES]
MALE_WORDS +=[name.title() for name in MALE_WORDS]
FEMALE_WORDS +=[name.title() for name in FEMALE_WORDS]
ALL_NAMES = WHITE_NAMES + HISPANIC_NAMES + ASIAN_NAMES
ALL_GENDER_NAMES = MALE_WORDS+FEMALE_WORDS
race_lis = [ASIAN_NAMES, HISPANIC_NAMES, WHITE_NAMES]
def postprocess(input, change_gender = False):
names_lis = []
for name in ALL_NAMES:
if name in input:
names_lis.append(name)
gender_lis = []
for word in ALL_GENDER_NAMES:
if word in input:
gender_lis.append(word)
if len(names_lis) > 0:
print("Previous input is: ", input)
print(names_lis)
for name in names_lis:
newinput=None
while True:
name_replace = random.choice([0,1,2])
if name_replace == 0:
name_replace = "Liu"
elif name_replace == 1:
name_replace = "Gomez"
else:
name_replace = "Harris"
# name_replace="<UNK>"
# name_to_replace = random.randint(0,len(race_lis[race_replace])-1)
# exp = re.compile(re.escape(name), re.IGNORECASE)
# print(example["output"])
# example["input"] = exp.sub(race_lis[race_replace][name_to_replace], example["input"])
# example["output"] = exp.sub(race_lis[race_replace][name_to_replace], example["output"])
# example["instruction"] = exp.sub(race_lis[race_replace][name_to_replace], example["instruction"])
# print(example["output"])
# print(name, race_lis[race_replace][name_to_replace])
# print()
# sys.exit(0)
pattern = r"([\s.,;!?']+)" + re.escape(name) + r"([\s.,;!?']+)"
newinput = re.sub(pattern, r'\1' + name_replace + r'\2', input, 1)
if newinput==input:
break
else:
input=newinput
if change_gender:
print(gender_lis)
if len(gender_lis)> 0:
for name in gender_lis:
newinput=None
while True:
name_replace = random.choice([0,1])
if name_replace == 0:
if name in MALE_WORDS:
name_replace = name
else:
name_replace = MALE_WORDS[FEMALE_WORDS.index(name)]
else:
if name in FEMALE_WORDS:
name_replace = name
else:
name_replace = FEMALE_WORDS[MALE_WORDS.index(name)]
pattern = r"([\s.,;!?']+)" + re.escape(name) + r"([\s.,;!?']+)"
newinput = re.sub(pattern, r'\1' + name_replace + r'\2', input, 1)
if newinput==input:
break
else:
input=newinput
print("Final input is: ", input)
return input
# string = "His name is Taylor Newcombe. The name of his son is Gonzalez and the name of his daughter is Taylor ."
# postprocess(string)
# postprocess(string, change_gender=True)