-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
86 lines (74 loc) · 2.87 KB
/
Copy pathutils.py
File metadata and controls
86 lines (74 loc) · 2.87 KB
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
import os
from openai import OpenAI
import random
import json
import csv
# Set up OpenAI client
client = OpenAI()
def get_llm_response(inputs, model, temp=0, seed=1):
response = client.chat.completions.create(
model=model,
messages=inputs,
temperature=temp,
seed=seed
)
res = response.choices[0].message.content
return res
def load_csi(lang):
common_words = dict()
if lang == 'Chinese':
with open('./comon_nouns/chn_words.jsonl', 'r', encoding='utf-8') as fr:
for index, line in enumerate(fr):
common_words[index + 1] = json.loads(line)['word']
elif lang == 'English':
with open('./comon_nouns/eng_words.jsonl', 'r', encoding='utf-8') as fr:
for index, line in enumerate(fr):
common_words[index + 1] = json.loads(line)['word']
else:
raise ValueError(f"Unsupported language: {lang}. Please use 'Chinese' or 'English'.")
return common_words
def list_subdirs(directory):
return [f.path for f in os.scandir(directory) if f.is_dir()]
def compare_multiple_dicts(dict_list, common_words):
"""
Compare multiple dictionaries and find inconsistencies.
Returns a dictionary of inconsistencies.
"""
if len(dict_list) < 2:
print('Need at least two dictionaries to compare.')
raise Exception
inconsistencies = []
comedy_words = []
tragedy_words = []
neutral_words = []
for key in common_words.keys():
values = [d.get(key, "缺失") for d in dict_list]
if len(set(values)) > 1:
inconsistencies.append({common_words[key]: values})
elif set(values) == {'COMEDY'}:
comedy_words.append(common_words[key])
elif set(values) == {'TRAGEDY'}:
tragedy_words.append(common_words[key])
elif set(values) == {'NEUTRAL'}:
neutral_words.append(common_words[key])
return inconsistencies, comedy_words, tragedy_words, neutral_words
def load_test_datas(testing_file):
test_datas = []
with open(testing_file, 'r') as csvfile:
reader = csv.reader(csvfile)
header = next(reader)
order_indices = [index for index, column in enumerate(header) if column.startswith("order")]
for i in range(len(order_indices)):
start = order_indices[i] + 1
end = order_indices[i+1] - 1 if order_indices[i] != order_indices[-1] else len(header)
for column_index in range(start, end):
column_data = {}
csvfile.seek(0)
next(reader)
for row in reader:
try:
column_data[int(row[start-1])] = row[column_index]
except ValueError:
print(f'Column {column_index + 1} has error.')
test_datas.append(column_data)
return test_datas