-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.py
114 lines (103 loc) · 3.84 KB
/
Config.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 random
import json
from ZM_IP import *
def select_Simple(question,choices):
if 'affected_id' not in question:
options = list(range(1, len(question['probabilities']) + 1))
answer = random.choices(options, weights=question['probabilities'], k=1)[0]
else:
affected_id = question['affected_id'] # 受影响选项ID
affected_value = choices[f'{affected_id}'][0] # 受影响选项答案
options = list(range(1, len(question['probabilities'][f'{affected_value}']) + 1))
answer = random.choices(options, weights=question['probabilities'][f'{affected_value}'], k=1)[0]
return [answer]
def select_Multiple(question,choices):
options = list(range(1, len(question['probabilities']) + 1))
# 使用random.choice选出3个,可能会重复,再用random.sample确保选择的答案不重复
sampled_elements = random.choices(options, weights=question['probabilities'], k=3)
seen = set()
answers = [x for x in sampled_elements if x not in seen and not seen.add(x)]
return answers
def select_Sort(question,choices):
options = list(range(1, len(question['probabilities']) + 1))
answers = random.sample(options, len(question['probabilities']))
return answers
def select_Scale(question,choices):
options = list(range(1, len(question['probabilities']) + 1))
answer = random.choices(options, weights=question['probabilities'], k=1)[0]
return [answer]
def input_Gap(question,choices):
id = random.randint(1,3)
answer = question['input'][f'{id}']
return answer
class Config(object):
def __init__(self):
'''
:param id: 题号
:param type: 题目类型,Simple单选,Multiple多选,Sort排序,Scale量表,Gap填空
:param affected_id: 受影响选项的题号
:param probabilities: 题目选项的概率分布
'''
self.batch = 10
self.proxy = True
self.questions = [
{
"id": 1,
"type": "Simple",
"probabilities": [0.6, 0.4]
},
{
"id": 2,
"type": "Simple",
"affected_id": 1,
"probabilities": {
"1":[0.9, 0.1],
"2":[0.1, 0.9]
}
},
{
"id": 3,
"type": "Multiple",
"probabilities": [0.4,0.2,0.1,0.3]
},
{
"id": 4,
"type": "Sort",
"probabilities": [0.1,0.5,0.4]
},
{
"id": 5,
"type": "Scale",
"probabilities": [0.1,0.1,0.2,0.2,0.4]
},
{
"id": 6,
"type": "Gap",
"input": {
"1":['支持'],
"2":['反对'],
"3":['无明确意见']
}
}
]
# TODO 自定义测试
if __name__ == "__main__":
config = Config()
# 答案汇总字典
choices = {}
# 遍历每个题目并选择答案
for question in config.questions:
id = question['id']
if question['type'] == 'Simple':
answers = select_Simple(question,choices)
elif question['type'] == 'Multiple':
answers = select_Multiple(question,choices)
elif question['type'] == 'Sort':
answers = select_Sort(question,choices)
elif question['type'] == 'Scale':
answers = select_Scale(question,choices)
elif question['type'] == 'Gap':
answers = input_Gap(question,choices)
choices[f'{id}'] = answers
print(f"For question {id}, the selected answer is: {answers}")
print('Question selection presentation:',choices)