-
Notifications
You must be signed in to change notification settings - Fork 3
/
seer.py
114 lines (105 loc) · 3.39 KB
/
seer.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
import argparse
from collections import Counter
from explanation_generation import (get_candidates, get_contextualizer,
get_corpus, get_generator, get_preference)
from local_search_contextualized_opinion import \
local_search_contextualize_opinion
from sentence_pair_model import TfIdfSentencePair
from util import *
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument(
"-i",
"--input",
default="A10L9NQO44OLOU,B0044T2KBU,toy,toy",
help="Input user, item, demanded aspects (comma seperated). Ex: <userID>,<itemID>,<aspect1>,<aspect2>",
)
parser.add_argument(
"-c",
"--corpus_path",
type=str,
default="data/toy/train.csv",
help="Input corpus path",
)
parser.add_argument(
"-s",
"--strategy",
choices=[
"greedy-efm",
"greedy-mter",
"ilp-efm",
"ilp-mter",
],
default="greedy-efm",
)
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(
"-a",
"--alpha",
type=float,
default=0.5,
help="Trace off factor between open review cost and representative sentence cost",
)
parser.add_argument("--top_k", type=int, default=10)
parser.add_argument("--verbose", action="store_true")
args = parser.parse_args()
return args
def parse_input(input_string):
input_tokens = input_string.split(",")
user, item, demanded_aspects = input_tokens[0], input_tokens[1], input_tokens[2:]
return user, item, Counter(demanded_aspects)
def main(args):
corpus = get_corpus(args.corpus_path)
user, item, demanded_aspects = parse_input(args.input)
candidates, simplified_demand = get_candidates(
corpus, user, item, demanded_aspects, simplify=True
)
print("User:", user)
print("Item:", item)
print("Demanded aspects:", demanded_aspects)
print("Simplify demanded aspects:", simplified_demand)
print("# Candidate sentences:", len(candidates))
print("-" * 20)
preference = get_preference(args.preference_dir, args.strategy, args.verbose)
sentence_pair_model = TfIdfSentencePair(args.verbose)
generator = get_generator(
args.strategy,
preference,
sentence_pair_model,
alpha=args.alpha,
verbose=args.verbose,
)
result = generator.generate(user, item, simplified_demand, candidates)
selected_sentences = result.get("selected_sentences", [])
selected_aspects = result.get("selected_aspects", [])
print("Selected sentences :", selected_sentences)
contextualizer = get_contextualizer(
args.contextualizer_path,
preference,
strategy=args.strategy,
verbose=args.verbose,
)
explanations = local_search_contextualize_opinion(
user,
item,
selected_sentences,
selected_aspects,
corpus,
contextualizer,
sentence_pair_model,
top_k=args.top_k,
strategy=args.strategy,
verbose=args.verbose,
)
print("Generated explanation:", explanations)
if __name__ == "__main__":
main(parse_arguments())