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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
"""
Several preprocessor classes.
Author: md
"""

from preprocessor.base import BasePreprocessorConfig, BasePreprocessor
from const import (
    DIALOGUE_SUMMARY,
    DIALOGUE_CONTEXT_TO_RESPONSE_GENERATION,
    DIALOG,
    KNOWLEDGE,
    UTTERANCE,
    ROLES,
    EMOTION_RECOGNITION,
    VALUE,
    ABSA,
    CHARACTER_IDENTIFICATION,
    DIALOGUE_STATE_TRACKING,
    DOCUMENT_GROUNDED_CONVERSATION,
    TEXT2SQL,
    SLOT_FILLING,
    ROLE_RELATION_RECOGNITION,
    QUESTION_IN_CONTEXT_REWRITING,
    NATURAL_LANGUAGE_INFERENCE,
    MACHINE_READING_COMPREHENSION,
    MULTIPLE_CHOICE_QUESTION_ANSWERING,
    INTENT_DETECTION,
    DATA_TO_TEXT,
    CHIT_CHAT,
    TRAIN_SPLIT,
)
from typing import Dict, List, Callable
from copy import deepcopy


class SerialConfig(BasePreprocessorConfig):
    def __init__(
        self,
        input_dir: str,
        output_dir: str,
        task: str,
        task_bos_token: str = "<s>",
        knowledge_bos_token: str = "[EK]",
        prompt_bos_token: str = "[C]",
        use_role: bool = True,
        turn_sep: str = None,
        roles_to_build_example: List = None,
        dev_and_test_roles_to_build_example: List = None,
        prompt_func: Callable = None,
        knowledge_func: Callable = None,
        label_func: Callable = None,
        turn_knowledge_func: Callable = None,
        roles_in_history: List[List] = None,
        cur_turn_process_func: Callable = None,
        all_turns_process_func: Callable = None,
        multi_ref_sep: str = None,
        *args,
        **kwargs,
    ) -> None:
        super().__init__(input_dir, output_dir, task, *args, **kwargs)

        self.use_role = use_role
        self.turn_sep = turn_sep
        self.roles_to_build_example = roles_to_build_example
        self.prompt_func = prompt_func
        self.task_bos_token = task_bos_token
        self.knowledge_bos_token = knowledge_bos_token
        self.prompt_bos_token = prompt_bos_token
        self.knowledge_func = knowledge_func
        self.label_func = label_func
        self.turn_knowledge_func = turn_knowledge_func
        self.roles_in_history = roles_in_history
        self.multi_ref_sep = multi_ref_sep
        self.dev_and_test_roles_to_build_example = dev_and_test_roles_to_build_example
        self.cur_turn_process_func = cur_turn_process_func
        self.all_turns_process_func = all_turns_process_func


def concat_roles(roles):
    return ", ".join(roles)


def concat_dial_history(config: SerialConfig, history: List[Dict]):
    # utterance_list = [
    #     f"{concat_roles(turn[ROLES])}: {turn[UTTERANCE].strip()}"
    #     if config.use_role
    #     else turn[UTTERANCE].strip()
    #     for turn in history
    # ]

    utterance_list = []
    for turn in history:
        if (
            config.roles_in_history is not None
            and turn[ROLES] not in config.roles_in_history
        ):
            continue

        if config.use_role:
            utterance_list.append(
                f"{concat_roles(turn[ROLES])}: {turn[UTTERANCE].strip()}"
            )
        else:
            utterance_list.append(turn[UTTERANCE].strip())

    if not utterance_list:
        return "None"

    turn_sep = " "
    if config.turn_sep is not None:
        turn_sep = f" {config.turn_sep} "

    return turn_sep.join(utterance_list)


def concat_history_knowledge_prompt(
    config: SerialConfig, history: str, knowledge: str = "", prompt: str = ""
):
    """Concat `history`, `knowledge` and `prompt`.

    NOTE: the order is fixed now.
    """
    text = ""

    if config.task_bos_token is not None:
        text = f"{config.task_bos_token} "

    text += history

    if knowledge is not None:
        text += f" {config.knowledge_bos_token} {knowledge}"

    if prompt is not None:
        text += f" {config.prompt_bos_token} {prompt}"

    return text


def clean(text):
    return text.replace("\r\n", " ").replace("\n", " ").replace("\r", " ")


def add_prefix_to_label(prefix, split, label):
    tgt = f"{prefix} {label}" if split == "train" else label
    return tgt


class SerialPreprocessor(BasePreprocessor):
    def __init__(self, config: SerialConfig) -> None:
        super().__init__(config)

    def extract_knowledge(self, example: Dict):
        if self.config.knowledge_func is None:
            knowledge = None

        elif (
            KNOWLEDGE not in example
            or not self.config.knowledge_func.__code__.co_argcount
        ):
            knowledge = self.config.knowledge_func()
        else:
            knowledge = self.config.knowledge_func(example[KNOWLEDGE][VALUE])

        return knowledge

    def preprocess_for_dialogue_level(self, split: str, example: Dict, knowledge: str):
        label = self.config.label_func(example)
        tgt = add_prefix_to_label(self.config.task_bos_token, split, label)

        history = concat_dial_history(self.config, example[DIALOG])

        if self.config.prompt_func is None:
            prompt = ""
        elif not self.config.prompt_func.__code__.co_argcount:
            prompt = self.config.prompt_func()

        src = concat_history_knowledge_prompt(self.config, history, knowledge, prompt)

        return [{"src": clean(src), "tgt": clean(tgt)}]

    def preprocess_for_label_level(self, split: str, example: Dict, knowledge: str):
        label_generator = self.config.label_func(example)

        examples = []
        for turn_id, label, extra_args in label_generator:
            tgt = add_prefix_to_label(self.config.task_bos_token, split, label)

            hist = deepcopy(example[DIALOG])
            if self.config.all_turns_process_func is not None:
                hist[turn_id] = self.config.all_turns_process_func(
                    hist[turn_id], *extra_args
                )

            history = concat_dial_history(self.config, hist)

            if self.config.prompt_func is None:
                prompt = ""
            elif not self.config.prompt_func.__code__.co_argcount:
                prompt = self.config.prompt_func()

            src = concat_history_knowledge_prompt(
                self.config, history, knowledge, prompt
            )

            examples.append({"src": clean(src), "tgt": clean(tgt)})

        return examples

    def get_label(
        self, turn, include_current_turn, turn_idx, split, origin_knowledge=None
    ):
        # skip the roles not requiring to build examples
        if (
            split != TRAIN_SPLIT
            and self.config.dev_and_test_roles_to_build_example is not None
        ):
            roles_to_build_example = self.config.dev_and_test_roles_to_build_example
        else:
            roles_to_build_example = self.config.roles_to_build_example
        if (
            roles_to_build_example is not None
            and turn[ROLES] not in roles_to_build_example
        ):
            return None

        # skip the first turn if not including current turn
        if not include_current_turn and turn_idx == 0:
            return None

        if self.config.task != DIALOGUE_STATE_TRACKING:
            try:
                label = self.config.label_func(turn, split=split)
            except:
                label = self.config.label_func(turn, origin_knowledge, split=split)
        else:
            label = self.config.label_func(
                turn, self.ontologies[split], do_train=(split == TRAIN_SPLIT)
            )

        return label

    def preprocess_for_turn_level(
        self,
        split: str,
        example: Dict,
        knowledge: str,
        include_current_turn=False,
        origin_knowledge=None,
    ):
        examples = []
        multiref = []
        for turn_idx, turn in enumerate(example[DIALOG]):
            label = self.get_label(
                turn, include_current_turn, turn_idx, split, origin_knowledge
            )

            if label is None:
                continue

            multiref.append(label)
            # requre to merge and arrive at the final consecutive label
            if (
                self.config.multi_ref_sep is not None
                and split != "train"
                and turn_idx < len(example[DIALOG]) - 1
                and self.get_label(
                    example[DIALOG][turn_idx + 1],
                    include_current_turn,
                    turn_idx + 1,
                    split,
                )
                is not None
            ):
                continue

            if self.config.multi_ref_sep is not None and split != "train":
                label = self.config.multi_ref_sep.join(multiref)

            tgt = add_prefix_to_label(self.config.task_bos_token, split, label)

            end = (turn_idx + 1) if include_current_turn else turn_idx

            hist = deepcopy(example[DIALOG][:end])
            if self.config.cur_turn_process_func is not None:
                hist[-1] = self.config.cur_turn_process_func(hist[-1])

            history = concat_dial_history(self.config, hist)

            if self.config.prompt_func is None:
                prompt = ""
            elif not self.config.prompt_func.__code__.co_argcount:
                prompt = self.config.prompt_func()

            if self.config.turn_knowledge_func is not None:
                knowledge_to_use = self.config.turn_knowledge_func(knowledge, turn)
            else:
                knowledge_to_use = knowledge

            src = concat_history_knowledge_prompt(
                self.config, history, knowledge_to_use, prompt
            )

            examples.append({"src": clean(src), "tgt": clean(tgt)})

            multiref = []

        return examples

    def preprocess_line(self, split: str, example: Dict) -> List[Dict]:
        knowledge = self.extract_knowledge(example)

        # 1. Dialogue Summary
        if self.config.task == DIALOGUE_SUMMARY:
            return self.preprocess_for_dialogue_level(split, example, knowledge)

        # 2. Emotion Recognition
        if self.config.task == EMOTION_RECOGNITION:
            return self.preprocess_for_turn_level(
                split, example, knowledge, include_current_turn=True
            )

        # 3. Dialogue Context-to-Text Generation
        if self.config.task == DIALOGUE_CONTEXT_TO_RESPONSE_GENERATION:
            return self.preprocess_for_turn_level(
                split, example, knowledge, include_current_turn=False
            )

        # 4. ABSA
        if self.config.task.startswith(ABSA):
            return self.preprocess_for_turn_level(
                split, example, knowledge, include_current_turn=True
            )

        # 5. Character Identification
        if self.config.task == CHARACTER_IDENTIFICATION:
            # return self.preprocess_for_turn_level(
            #     split, example, knowledge, include_current_turn=True
            # )
            # return self.preprocess_for_dialogue_level(split, example, knowledge)
            return self.preprocess_for_label_level(split, example, knowledge)

        # 6. Dialogue State Tracking
        if self.config.task == DIALOGUE_STATE_TRACKING:
            return self.preprocess_for_turn_level(
                split, example, knowledge, include_current_turn=True
            )

        # 7. Document Grounded Conversation
        if self.config.task == DOCUMENT_GROUNDED_CONVERSATION:
            return self.preprocess_for_turn_level(
                split, example, knowledge, include_current_turn=False
            )

        # 8. Text2SQL
        if self.config.task == TEXT2SQL:
            seq_examples = self.preprocess_for_turn_level(
                split, example, knowledge, include_current_turn=True
            )

            for idx in range(len(seq_examples)):
                seq_examples[idx]["db_id"] = knowledge["db_id"]

            return seq_examples

        # 9. Slot Filling
        if self.config.task == SLOT_FILLING:
            return self.preprocess_for_turn_level(
                split, example, knowledge, include_current_turn=True
            )

        # 10. Relation Recognition
        if self.config.task == ROLE_RELATION_RECOGNITION:
            return self.preprocess_for_dialogue_level(split, example, knowledge)

        # 11. Question in Context Rewriting
        if self.config.task == QUESTION_IN_CONTEXT_REWRITING:
            return self.preprocess_for_turn_level(
                split, example, knowledge, include_current_turn=True
            )

        # 12. Natural Language Inference
        if self.config.task == NATURAL_LANGUAGE_INFERENCE:
            return self.preprocess_for_turn_level(
                split,
                example,
                knowledge,
                include_current_turn=True,
                origin_knowledge=example[KNOWLEDGE][VALUE],
            )

        # 13. Machine Reading Comprehension
        if self.config.task == MACHINE_READING_COMPREHENSION:
            return self.preprocess_for_turn_level(split, example, knowledge)

        # 14. Multiple Choice Question Answering
        if self.config.task == MULTIPLE_CHOICE_QUESTION_ANSWERING:
            return self.preprocess_for_turn_level(
                split,
                example,
                knowledge,
                include_current_turn=True,
                origin_knowledge=example[KNOWLEDGE][VALUE],
            )

        # 15. Intent Detection
        if self.config.task == INTENT_DETECTION:
            return self.preprocess_for_turn_level(
                split, example, knowledge, include_current_turn=True
            )

        # 16. Data-to-Text
        if self.config.task == DATA_TO_TEXT:
            return self.preprocess_for_turn_level(
                split, example, knowledge, include_current_turn=True
            )

        # 17. Chit-Chat
        if self.config.task == CHIT_CHAT:
            return self.preprocess_for_turn_level(
                split, example, knowledge, include_current_turn=False
            )

        if self.config.task == "Semantic Parsing":
            seq_examples = self.preprocess_for_turn_level(
                split, example, knowledge, include_current_turn=True
            )

            return seq_examples