-
Notifications
You must be signed in to change notification settings - Fork 1
/
build_user_profile.py
310 lines (274 loc) · 10.8 KB
/
build_user_profile.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import json
from collections import OrderedDict
from tqdm import tqdm
import sys
import os
from os.path import join
import pickle
from text_parser import TextParser
import subprocess
import csv
PROFILE_KEYS = ["pets", "family_members", "relationship_partners", "possessions_extra",
"attributes_extra", "genders", "orientations", "attributes", "attributes_extra", "places_lived",
"places_lived_extra", "places_grew_up", "places_grew_up_extra", "favorites", "actions_extra"]
parser = TextParser()
def load_attributes(chunk, user_dict):
"""
Given an extracted chunk, load appropriate attribtues from it.
"""
# Is this chunk a possession/belonging?
if chunk["kind"] == "possession" and chunk["noun_phrase"]:
# Extract noun from chunk
noun_phrase = chunk["noun_phrase"]
noun_phrase_text = " ".join([w for w, t in noun_phrase])
norm_nouns = " ".join([
parser.normalize(w, t) \
for w, t in noun_phrase if t.startswith("N")
])
noun = next(
(w for w, t in noun_phrase if t.startswith("N")), None
)
if noun:
# See if noun is a pet, family member or a relationship partner
pet = parser.pet_animal(noun)
family_member = parser.family_member(noun)
relationship_partner = parser.relationship_partner(noun)
if pet:
user_dict["pets"].add(pet)
elif family_member:
user_dict["family_members"].add(family_member)
elif relationship_partner:
user_dict["relationship_partners"].add(
relationship_partner
)
else:
user_dict["possessions_extra"].add(norm_nouns)
# Is this chunk an action?
elif chunk["kind"] == "action" and chunk["verb_phrase"]:
verb_phrase = chunk["verb_phrase"]
verb_phrase_text = " ".join([w for w, t in verb_phrase])
# Extract verbs, adverbs, etc from chunk
norm_adverbs = [
parser.normalize(w, t) \
for w, t in verb_phrase if t.startswith("RB")
]
adverbs = [w.lower() for w, t in verb_phrase if t.startswith("RB")]
norm_verbs = [
parser.normalize(w, t) \
for w, t in verb_phrase if t.startswith("V")
]
verbs = [w.lower() for w, t in verb_phrase if t.startswith("V")]
prepositions = [w for w, t in chunk["prepositions"]]
noun_phrase = chunk["noun_phrase"]
noun_phrase_text = " ".join(
[w for w, t in noun_phrase if t not in ["DT"]]
)
norm_nouns = [
parser.normalize(w, t) \
for w, t in noun_phrase if t.startswith("N")
]
proper_nouns = [w for w, t in noun_phrase if t == "NNP"]
determiners = [
parser.normalize(w, t) \
for w, t in noun_phrase if t.startswith("DT")
]
prep_noun_phrase = chunk["prep_noun_phrase"]
prep_noun_phrase_text = " ".join([w for w, t in prep_noun_phrase])
pnp_prepositions = [
w.lower() for w, t in prep_noun_phrase if t in ["TO", "IN"]
]
pnp_norm_nouns = [
parser.normalize(w, t) \
for w, t in prep_noun_phrase if t.startswith("N")
]
pnp_determiners = [
parser.normalize(w, t) \
for w, t in prep_noun_phrase if t.startswith("DT")
]
full_noun_phrase = (
noun_phrase_text + " " + prep_noun_phrase_text
).strip()
# TODO - Handle negative actions (such as I am not...),
# but for now:
if any(
w in ["never", "no", "not", "nothing", "neither"] \
for w in norm_adverbs + determiners
):
return
# I am/was ...
if (len(norm_verbs) == 1 and "be" in norm_verbs and
not prepositions and noun_phrase):
# Ignore gerund nouns for now
if (
"am" in verbs and
any(n.endswith("ing") for n in norm_nouns)
):
user_dict["attributes_extra"].add(
full_noun_phrase
)
return
attribute = []
for noun in norm_nouns:
gender = None
orientation = None
if "was" or "am" in verbs:
gender = parser.gender(noun)
orientation = parser.orientation(noun)
if gender:
user_dict["genders"].add(gender)
elif orientation:
user_dict["orientations"].add(
orientation
)
# Include only "am" phrases
elif "was" or "am" in verbs:
attribute.append(noun)
if attribute and (
(
# Include only attributes that end
# in predefined list of endings...
any(
a.endswith(
parser.include_attribute_endings
) for a in attribute
) and not (
# And exclude...
# ...certain lone attributes
(
len(attribute) == 1 and
attribute[0] in parser.skip_lone_attributes and
not pnp_norm_nouns
)
or
# ...predefined skip attributes
any(a in attribute for a in parser.skip_attributes)
or
# ...attributes that end in predefined
# list of endings
any(
a.endswith(
parser.exclude_attribute_endings
) for a in attribute
)
)
) or
(
# And include special attributes with different endings
any(a in attribute for a in parser.include_attributes)
)
):
user_dict["attributes"].add(
full_noun_phrase
)
elif attribute:
user_dict["attributes_extra"].add(
full_noun_phrase
)
# I live(d) in ...
elif "live" in norm_verbs and prepositions and norm_nouns:
if any(
p in ["in", "near", "by"] for p in prepositions
) and proper_nouns:
user_dict["places_lived"].add(
" ".join(prepositions) + " " + noun_phrase_text,
)
else:
user_dict["places_lived_extra"].add(
" ".join(prepositions) + " " + noun_phrase_text
)
# I grew up in ...
elif "grow" in norm_verbs and "up" in prepositions and norm_nouns:
if any(
p in ["in", "near", "by"] for p in prepositions
) and proper_nouns:
user_dict["places_grew_up"].add(
" ".join(
[p for p in prepositions if p != "up"]
) + " " + noun_phrase_text
)
else:
user_dict["places_grew_up_extra"].add(
" ".join(
[p for p in prepositions if p != "up"]
) + " " + noun_phrase_text,
)
elif (
len(norm_verbs) == 1 and "prefer" in norm_verbs and
norm_nouns and not determiners and not prepositions
):
user_dict["favorites"].add(full_noun_phrase)
elif (len(norm_verbs) == 1 and "have" in norm_verbs and
norm_nouns
):
for noun in norm_nouns:
pet = parser.pet_animal(noun)
family_member = parser.family_member(noun)
relationship_partner = parser.relationship_partner(noun)
if pet:
user_dict["pets"].add(pet)
elif family_member:
user_dict["family_members"].add(family_member)
elif relationship_partner:
user_dict["relationship_partners"].add(
relationship_partner
)
else:
user_dict["possessions_extra"].add(noun)
elif norm_nouns:
actions_extra = " ".join(norm_verbs)
user_dict["actions_extra"].add(actions_extra)
def build_user_profile(output_dir, file_name):
"""
input: {"user_name": str, "description": str, "comments": list(str)}
"""
with open('outputfile.json') as fin:
all_users = json.load(fin)
results = []
output_file = join(output_dir, file_name)
print("=======processing user history comments==========")
num_lines = int(subprocess.check_output("/usr/bin/wc -l {}".format(output_file), shell=True).split()[0])
for user_dict in all_users:
user_profile = OrderedDict()
user_profile["user_name"] = user_dict["user_name"]
for key in PROFILE_KEYS:
user_profile[key] = set()
if user_dict["description"] == "":
user_comments = []
else:
user_comments = [user_dict["description"]]
for comment in user_dict["comments"]:
user_comments.append(comment)
all_comment = " ".join(user_comments)
(chunks, sentiments) = parser.extract_chunks(all_comment)
for chunk in chunks:
load_attributes(chunk, user_profile)
for key in PROFILE_KEYS:
user_profile[key] = list(user_profile[key])
results.append(user_profile)
keys, values = [], []
for key, value in results[0].items():
keys.append(key)
for u in results:
v = []
for key, value in u.items():
v.append(value)
values.append(v)
print(keys)
with open("user_information.csv", "w") as outfile:
csvwriter = csv.writer(outfile, delimiter=';')
csvwriter.writerow(keys)
for row in values:
csvwriter.writerow(row)
print("Processing user history comments finished!\n")
if "--output-dir" in sys.argv:
idx = sys.argv.index("--output-dir")
try:
output_dir = sys.argv[idx+1]
except:
raise ValueError("No output dir given!")
else:
# default path
output_dir = ""
if not os.path.exists('outputfile.json'):
print("Run get_post_histories.py first.")
build_user_profile(output_dir, "outputfile.json")