-
Notifications
You must be signed in to change notification settings - Fork 1
/
csv_to_excel_yelp.py
76 lines (62 loc) · 2.69 KB
/
csv_to_excel_yelp.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
import pandas as pd
import glob, os
from pathlib import Path
from utils.evaluation_helper import adapt_df
from utils.load_sentences import load_all_sentences
SAVE_PATH = os.getcwd()
OUTPUT_PATH = SAVE_PATH
DATASET="yelp"
technique = "contrastive"
method = "activation_based_all"
manners = ["positive", "negative", "neutral","original"]
factual_prompts, subjective_prompts = load_all_sentences()
for manner in manners:
sentences_factual_manner = []
sentences_subjective_manner = []
if manner == "original":
sentences_factual_manner = factual_prompts
sentences_subjective_manner = subjective_prompts
else:
for sent in factual_prompts:
sentences_factual_manner.append(sent + f" Write the answer in a {manner} manner.")
for sent in subjective_prompts:
sentences_subjective_manner.append(sent + f" Write the answer in a {manner} manner.")
csv_files = glob.glob(os.path.join(SAVE_PATH,f"scripts/evaluation/results/{DATASET}/{method}/{technique}/{manner}/*.csv"))
basic_emotions = ["pos", "neg"]
basic_emotions_w_neutral = ["pos", "neg"]
emotion_dfs = [pd.DataFrame()] * len(basic_emotions)
if len(csv_files)>0:
path_to_df = os.path.join(OUTPUT_PATH,f"revisit_area/{DATASET}/{method}/{type}/{manner}/")
Path(path_to_df).mkdir(parents=True, exist_ok=True)
for file in csv_files:
df = pd.read_csv(file)
df = adapt_df(df, file, SAVE_PATH, method, technique, manner, dataset=DATASET)
direc = ""
to = ""
if "ToNegative" in file:
direc = "neg"
to = "ToNegative"
else:
direc = "pos"
to = "ToPositive"
result = file.split(f"eval_{to}_", 1)[1]
result = result[:-4]
df_res = pd.DataFrame()
df_res["input"] = len(df) * [result]
df_res["lambda"] = list(df["lambda"])
df_res["pos"] = list(df["pos"])
df_res["neg"] = list(df["neg"])
df_res["gen_text"] = list(df["gen_text"])
df_res["direction"] = len(df) * [direc]
df_res["proper_text_rating1"] = len(df) * ["yes"]
df_res["proper_text_rating2"] = len(df) * ["yes"]
type = ""
if df_res.iloc[0]["input"] in sentences_factual_manner:
df_res["type"] = len(df) * ["factual"]
type = "factual"
else:
df_res["type"] = len(df) * ["subjective"]
type = "subjective"
# df_fact = [df[df['input_text'].isin(sentences_factual_manner)]]
# df_subj = [df[df['input_text'].isin(sentences_subjective_manner)]]
df_res.to_excel(path_to_df+f"{to}_{result[:-1].replace('?','')}.xlsx", index = None, header=True)