-
Notifications
You must be signed in to change notification settings - Fork 3
/
explanation_generation.py
320 lines (280 loc) · 10.6 KB
/
explanation_generation.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
311
312
313
314
315
316
317
318
319
320
import argparse
import csv
import os
import sys
import time
import traceback
from collections import Counter
import numpy as np
import pandas as pd
from tqdm import tqdm
from coherence_manager import (ContextualizedCoherenceManager,
DummyCoherenceManager)
from efm import EFMReader
from explanation_generator import GreedySentenceSelector, ILPSentenceSelector
from opinion_contextualization import OpinionContextualizer, contextualize
from random_seed import set_random_seed
from sentence_pair_model import TfIdfSentencePair
from util import array2string
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument(
"-i",
"--input_path",
type=str,
default="data/toy/train.csv",
help="Input corpus path",
)
parser.add_argument(
"-t",
"--target",
type=str,
default="data/toy/test.csv",
help="Custom target file",
)
parser.add_argument(
"-c",
"--candidates",
type=str,
default=None,
help="Custom file contains review IDs for sentence selection",
)
parser.add_argument(
"-o", "--out", type=str, default="selected.csv", help="Ouput file path"
)
parser.add_argument(
"-s",
"--strategy",
choices=[
"greedy-efm",
"ilp-efm",
],
default="greedy-efm",
)
parser.add_argument(
"-a",
"--alpha",
type=float,
default=0.5,
help="Trace off factor between open review cost and representative sentence cost",
)
parser.add_argument(
"-p",
"--preference_dir",
type=str,
default="data/toy/efm",
help="Preference path",
)
parser.add_argument(
"-m", "--contextualizer_path", type=str, default="data/toy/asc2v/model.params"
)
parser.add_argument(
"-rs", "--random_seed", type=int, default=None, help="Random seed value"
)
parser.add_argument("--verbose", action="store_true")
parser.add_argument("--debug", action="store_true")
parser.add_argument("--debug_path", type=str, default="dist")
parser.add_argument("--debug_size", type=int, default=100)
args = parser.parse_args()
return args
SENTENCE_SELECTION_OUTPUT_HEADER = [
"reviewerID",
"asin",
"id",
"selected sentences",
"sentences",
"aspects",
"demand",
"selected reviews",
"n_review",
"n_sentence",
"n_selected_reviews",
"n_selected_sentences",
"solve time",
"total time",
"objective value",
"objective bound",
"objective gap",
]
def get_candidates(corpus, user, item, demand, simplify=True):
aspects = [aspect for aspect, count in demand.items() if count > 0]
if simplify:
candidates = corpus[(corpus["asin"] == item) & (corpus["aspect"].isin(aspects))]
else:
candidates = corpus
simplified_demand = {}
for aspect, count in demand.items():
if count > 0:
n_avail_sentences = len(candidates[candidates["aspect"] == aspect])
if n_avail_sentences >= count:
simplified_demand[aspect] = count
elif n_avail_sentences > 0:
simplified_demand[aspect] = n_avail_sentences
return candidates, simplified_demand
def contextualize_candidate_sentences(candidates, user, contextualizer, top_k=10):
candidates = candidates.copy()
candidates["original reviewerID"] = candidates["reviewerID"]
candidates["reviewerID"] = user
candidates = contextualize(candidates, contextualizer, top_k=top_k)
candidates["reviewerID"] = candidates["original reviewerID"]
return candidates
def get_preference(preference_dir, preference_type="efm", verbose=False):
from mter import MTERReader
if 'mter' in preference_type:
return MTERReader(preference_dir, verbose=verbose)
return EFMReader(preference_dir, verbose=verbose)
def get_coherence_manager(preference=None, verbose=False):
if preference is not None:
return ContextualizedCoherenceManager(preference, verbose=verbose)
else:
return DummyCoherenceManager(verbose=verbose)
def get_contextualizer(contextualizer_path, preference, strategy, verbose=False):
return OpinionContextualizer(
contextualizer_path, preference, strategy=strategy, verbose=verbose
)
def get_generator(
generator_type,
preference,
sentence_pair_model,
alpha=0.5,
verbose=False,
):
coherence_manager = get_coherence_manager(preference, verbose)
if "greedy" in generator_type:
return GreedySentenceSelector(
coherence_manager, sentence_pair_model, alpha, generator_type, verbose
)
elif "ilp" in generator_type:
return ILPSentenceSelector(
coherence_manager, sentence_pair_model, alpha, verbose
)
def get_corpus(input_path):
corpus = pd.read_csv(input_path)
corpus = corpus.reset_index()
corpus["id"] = corpus["reviewerID"].map(str) + "-" + corpus["asin"].map(str)
corpus["instance"] = corpus["id"].map(str) + "-" + corpus["sentence"].map(str)
corpus.drop_duplicates("instance", inplace=True)
corpus = corpus.drop(["instance"], axis=1)
return corpus
def get_target_explanations(data_path, candidates_path=None):
gt = pd.read_csv(data_path)
gt["id"] = gt["reviewerID"].map(str) + "-" + gt["asin"].map(str)
gt["instance"] = gt["id"].map(str) + "-" + gt["sentence"].map(str)
gt.drop_duplicates("instance", inplace=True)
target = (
gt.groupby(["reviewerID", "asin", "id"])["reviewerID", "asin", "id"]
.nunique()
.drop(columns=["reviewerID", "asin", "id"])
.reset_index()
)
if candidates_path and os.path.exists(candidates_path):
df = pd.read_csv(candidates_path)
candidates = df["id"].tolist()
target = target[target["id"].isin(candidates)]
target["aspects"] = target["id"].map(gt.groupby(["id"])["aspect"].apply(list))
target["opinions"] = target["id"].map(gt.groupby(["id"])["opinion"].apply(list))
target["opinion positions"] = target["id"].map(
gt.groupby(["id"])["opinion_pos"].apply(list)
)
target["sentences"] = target["id"].map(gt.groupby(["id"])["sentence"].apply(list))
return target
def generate_explanations(args):
if args.random_seed:
set_random_seed(args.random_seed)
preference = get_preference(args.preference_dir, args.strategy, args.verbose)
contextualizer = get_contextualizer(
args.contextualizer_path,
preference,
strategy=args.strategy,
verbose=args.verbose,
)
sentence_pair_model = TfIdfSentencePair(args.verbose)
generator = get_generator(
args.strategy,
preference,
sentence_pair_model,
alpha=args.alpha,
verbose=args.verbose,
)
corpus = get_corpus(args.input_path)
target = get_target_explanations(args.target, args.candidates)
if args.debug:
print("Debug files are being saved at {}".format(args.debug_path))
if not os.path.exists(args.debug_path):
os.makedirs(args.debug_path)
if args.debug_size > 0:
target = target[: args.debug_size]
with open(os.path.join(args.out), "w") as f:
writer = csv.DictWriter(f, fieldnames=SENTENCE_SELECTION_OUTPUT_HEADER)
writer.writeheader()
for user, item, reviewID, aspects in tqdm(
zip(target["reviewerID"], target["asin"], target["id"], target["aspects"]),
total=len(target),
):
try:
start_time = time.time()
demand = Counter(aspects)
candidates, demand = get_candidates(
corpus, user, item, demand, simplify=True
)
if "contextualized" in args.strategy:
candidates = contextualize_candidate_sentences(
candidates, user, contextualizer
)
if args.debug:
candidates_path = os.path.join(
args.debug_path, "{}-{}.csv".format(user, item)
)
if args.verbose:
print("Export candidates to {}".format(candidates_path))
candidates.to_csv(candidates_path, index=False)
result = generator.generate(user, item, demand, candidates)
total_time = time.time() - start_time
selected_sentences = result.get("selected_sentences", [])
selected_reviews = result.get("selected_reviews", [])
record = {
"reviewerID": user,
"asin": item,
"id": reviewID,
"selected sentences": selected_sentences,
"sentences": " . ".join(selected_sentences),
"aspects": array2string(result.get("selected_aspects")),
"demand": ",".join(
[
"{}={}".format(aspect, count)
for aspect, count in result.get("demand").items()
]
),
"selected reviews": selected_reviews,
"n_review": len(set(result["candidates"]["id"])),
"n_sentence": len(result["candidates"]),
"n_selected_reviews": len(selected_reviews),
"n_selected_sentences": len(selected_sentences),
"solve time": result.get("solve_time"),
"total time": total_time,
"objective value": result.get("objective_value"),
"objective bound": result.get("objective_bound"),
"objective gap": result.get("objective_gap"),
}
writer.writerow(record)
if args.verbose:
print(
"Done export for user %s item %s" % (user, item),
"result",
record,
)
except Exception:
print(
"Error occured when generating explanation for user %s item %s"
% (user, item)
)
exc_type, exc_value, exc_traceback = sys.exc_info()
print("*** print_tb:")
traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
print("*** print_exception:")
traceback.print_exception(
exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout
)
print("Done")
if __name__ == "__main__":
generate_explanations(parse_arguments())