-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathtest_dialog_sentiment_intensity_mapper.py
173 lines (150 loc) · 6.88 KB
/
test_dialog_sentiment_intensity_mapper.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
import unittest
import json
from loguru import logger
from data_juicer.core.data import NestedDataset as Dataset
from data_juicer.ops.mapper.dialog_sentiment_intensity_mapper import DialogSentimentIntensityMapper
from data_juicer.utils.unittest_utils import (SKIPPED_TESTS,
DataJuicerTestCaseBase)
from data_juicer.utils.constant import Fields, MetaKeys
# Skip tests for this OP.
# These tests have been tested locally.
@SKIPPED_TESTS.register_module()
class TestDialogSentimentIntensityMapper(DataJuicerTestCaseBase):
# before runing this test, set below environment variables:
# export OPENAI_API_URL=https://dashscope.aliyuncs.com/compatible-mode/v1
# export OPENAI_API_KEY=your_key
def _run_op(self, op, samples, target_len, intensities_key=None, analysis_key=None):
dataset = Dataset.from_list(samples)
dataset = op.run(dataset)
intensities_key = intensities_key or MetaKeys.dialog_sentiment_intensity
analysis_key = analysis_key or MetaKeys.dialog_sentiment_intensity_analysis
intensity_list = dataset[0][Fields.meta][intensities_key]
analysis_list = dataset[0][Fields.meta][analysis_key]
for analysis, intensity in zip(analysis_list, intensity_list):
logger.info(f'分析:{analysis}')
logger.info(f'情绪:{intensity}')
self.assertNotEqual(analysis, '')
self.assertEqual(len(analysis_list), target_len)
self.assertEqual(len(intensity_list), target_len)
def test_default(self):
samples = [{
'history': [
(
'李莲花有口皆碑',
'「微笑」过奖了,我也就是个普通大夫,没什么值得夸耀的。'
),
(
'是的,你确实是一个普通大夫,没什么值得夸耀的。',
'「委屈」你这话说的,我也是尽心尽力治病救人了。'
),
(
'你自己说的呀,我现在说了,你又不高兴了。',
'or of of of of or or and or of of of of of of of,,, '
),
(
'你在说什么我听不懂。',
'「委屈」我也没说什么呀,就是觉得你有点冤枉我了'
)
]
}]
op = DialogSentimentIntensityMapper(api_model='qwen2.5-72b-instruct')
self._run_op(op, samples, 4)
def test_max_round(self):
samples = [{
'history': [
(
'李莲花有口皆碑',
'「微笑」过奖了,我也就是个普通大夫,没什么值得夸耀的。'
),
(
'是的,你确实是一个普通大夫,没什么值得夸耀的。',
'「委屈」你这话说的,我也是尽心尽力治病救人了。'
),
(
'你自己说的呀,我现在说了,你又不高兴了。',
'or of of of of or or and or of of of of of of of,,, '
),
(
'你在说什么我听不懂。',
'「委屈」我也没说什么呀,就是觉得你有点冤枉我了'
)
]
}]
op = DialogSentimentIntensityMapper(api_model='qwen2.5-72b-instruct',
max_round=1)
self._run_op(op, samples, 4)
def test_max_round_zero(self):
samples = [{
'history': [
(
'李莲花有口皆碑',
'「微笑」过奖了,我也就是个普通大夫,没什么值得夸耀的。'
),
(
'是的,你确实是一个普通大夫,没什么值得夸耀的。',
'「委屈」你这话说的,我也是尽心尽力治病救人了。'
),
(
'你自己说的呀,我现在说了,你又不高兴了。',
'or of of of of or or and or of of of of of of of,,, '
),
(
'你在说什么我听不懂。',
'「委屈」我也没说什么呀,就是觉得你有点冤枉我了'
)
]
}]
op = DialogSentimentIntensityMapper(api_model='qwen2.5-72b-instruct',
max_round=0)
self._run_op(op, samples, 4)
def test_query(self):
samples = [{
'history': [
(
'李莲花有口皆碑',
'「微笑」过奖了,我也就是个普通大夫,没什么值得夸耀的。'
),
(
'是的,你确实是一个普通大夫,没什么值得夸耀的。',
'「委屈」你这话说的,我也是尽心尽力治病救人了。'
),
(
'你自己说的呀,我现在说了,你又不高兴了。',
'or of of of of or or and or of of of of of of of,,, '
)
],
'query': '你在说什么我听不懂。',
'response': '「委屈」我也没说什么呀,就是觉得你有点冤枉我了'
}]
op = DialogSentimentIntensityMapper(api_model='qwen2.5-72b-instruct',
max_round=1)
self._run_op(op, samples, 4)
def test_rename_keys(self):
samples = [{
'history': [
(
'李莲花有口皆碑',
'「微笑」过奖了,我也就是个普通大夫,没什么值得夸耀的。'
),
(
'是的,你确实是一个普通大夫,没什么值得夸耀的。',
'「委屈」你这话说的,我也是尽心尽力治病救人了。'
),
(
'你自己说的呀,我现在说了,你又不高兴了。',
'or of of of of or or and or of of of of of of of,,, '
),
(
'你在说什么我听不懂。',
'「委屈」我也没说什么呀,就是觉得你有点冤枉我了'
)
]
}]
intensities_key = 'my_intensity'
analysis_key = 'my_analysis'
op = DialogSentimentIntensityMapper(api_model='qwen2.5-72b-instruct',
intensities_key=intensities_key,
analysis_key=analysis_key)
self._run_op(op, samples, 4, intensities_key, analysis_key)
if __name__ == '__main__':
unittest.main()