|
from const import ( |
|
SUMMARY, |
|
EMOTIONS, |
|
EMOTION, |
|
UTTERANCE, |
|
ASPECTS, |
|
TARGET, |
|
VALUE, |
|
OPINION, |
|
SENTIMENT, |
|
CATEGORY, |
|
CHARACTERS, |
|
DIALOG, |
|
START, |
|
END, |
|
BELIEF_STATE, |
|
DOMAIN, |
|
INFORMED_SLOT_VALUE_TABLE, |
|
SLOT, |
|
VALUES, |
|
RELATION, |
|
SQL, |
|
SLOT_VALUE_TABLE, |
|
SLOTS_TO_FILL, |
|
ROLE_RELATIONS, |
|
REWRITTEN, |
|
ROLES_TO_SELECT, |
|
ACTIVE_INTENTS, |
|
TRAIN_SPLIT, |
|
OPTION_LABEL, |
|
CANDIDATES, |
|
) |
|
from typing import Dict |
|
import re |
|
import random |
|
import copy |
|
import json |
|
|
|
|
|
def extract_summary(dial: Dict, **kwargs): |
|
""" |
|
`dial` is the full dialog. |
|
""" |
|
return dial[SUMMARY] |
|
|
|
|
|
def extract_turn_emotion(turn: Dict, sep: str, **kwargs): |
|
if EMOTIONS not in turn: |
|
return None |
|
return sep.join(map(lambda x: x[EMOTION], turn[EMOTIONS])) |
|
|
|
|
|
def extract_turn_emotion_wrapper(sep: str): |
|
def extract_turn_emotion_func(turn: Dict, **kwargs): |
|
return extract_turn_emotion(turn, sep) |
|
|
|
return extract_turn_emotion_func |
|
|
|
|
|
def extract_turn_utterance(turn: Dict, **kwargs): |
|
return turn[UTTERANCE] |
|
|
|
|
|
def extract_aspects(turn: Dict, ext_aspect_sep: str, int_aspect_sep: str): |
|
if not turn[ASPECTS]: |
|
return "None" |
|
|
|
aspects = turn[ASPECTS] |
|
|
|
tgt_seq = [] |
|
for aspect in aspects: |
|
aspect_seq = [] |
|
if TARGET in aspect: |
|
aspect_seq.append(aspect[TARGET][VALUE]) |
|
if CATEGORY in aspect: |
|
aspect_seq.append(aspect[CATEGORY]) |
|
|
|
if OPINION in aspect: |
|
aspect_seq.append(aspect[OPINION][VALUE]) |
|
|
|
if SENTIMENT in aspect: |
|
aspect_seq.append(aspect[SENTIMENT]) |
|
|
|
tgt_seq.append(int_aspect_sep.join(aspect_seq)) |
|
|
|
return ext_aspect_sep.join(tgt_seq) |
|
|
|
|
|
def extract_aspects_wrapper(ext_aspect_sep: str, int_aspect_sep: str): |
|
def extract_aspects_func(turn: Dict, **kwargs): |
|
return extract_aspects(turn, ext_aspect_sep, int_aspect_sep) |
|
|
|
return extract_aspects_func |
|
|
|
|
|
def rebuild_utterance_with_characters(turn: Dict, split): |
|
if split == "train": |
|
utterance = turn[UTTERANCE] |
|
parts = [] |
|
pre = 0 |
|
|
|
for character in turn[CHARACTERS]: |
|
parts.append(utterance[pre : character[START]]) |
|
parts.append( |
|
f"[{utterance[character[START]: character[END]]} | {character[VALUE]}]" |
|
) |
|
pre = character[END] |
|
|
|
parts.append(utterance[pre:]) |
|
return "".join(parts) |
|
|
|
else: |
|
tuples = [] |
|
for character in turn[CHARACTERS]: |
|
tuples.append(f"{character[VALUE]}, {character[START]}, {character[END]}") |
|
|
|
if not tuples: |
|
return "None" |
|
return " | ".join(tuples) |
|
|
|
|
|
def extract_characters(example): |
|
for turn_id, turn in enumerate(example[DIALOG]): |
|
if CHARACTERS not in turn: |
|
continue |
|
|
|
for character in turn[CHARACTERS]: |
|
yield turn_id, character[VALUE], (character[END],) |
|
|
|
|
|
def extract_belief_state( |
|
turn, |
|
value_sep, |
|
domain_sep, |
|
slot_sep, |
|
domain_prompt_op, |
|
ontology=None, |
|
do_train=True, |
|
): |
|
domain_bs = dict() |
|
bs = turn[BELIEF_STATE] |
|
|
|
|
|
|
|
for state in bs: |
|
domain = state[DOMAIN] |
|
if domain not in domain_bs: |
|
domain_bs[domain] = dict() |
|
|
|
if INFORMED_SLOT_VALUE_TABLE not in state: |
|
continue |
|
|
|
for svp in state[INFORMED_SLOT_VALUE_TABLE]: |
|
slot = svp[SLOT] |
|
values = svp[VALUES] |
|
relation = svp[RELATION] |
|
|
|
if slot not in domain_bs[domain]: |
|
domain_bs[domain][slot] = {"relation": relation, "values": []} |
|
domain_bs[domain][slot]["values"] += list(map(lambda x: x[VALUE], values)) |
|
|
|
|
|
|
|
domain_bs_list = [] |
|
for domain in domain_bs: |
|
svp_list = [] |
|
for slot in domain_bs[domain]: |
|
val_str = value_sep.join(domain_bs[domain][slot]["values"]) |
|
svp_list.append(f"{slot} {domain_bs[domain][slot]['relation']} {val_str}") |
|
|
|
|
|
|
|
|
|
if not svp_list: |
|
continue |
|
if do_train: |
|
|
|
random.shuffle(svp_list) |
|
|
|
|
|
svt_str = slot_sep.join(svp_list) + slot_sep |
|
|
|
domain_bs_list.append(f"{domain}{domain_prompt_op}{svt_str.strip()}") |
|
|
|
if not domain_bs_list: |
|
return "None" |
|
|
|
return domain_sep.join(domain_bs_list) |
|
|
|
|
|
def extract_belief_state_wrapper(value_sep, domain_sep, slot_sep, domain_prompt_op): |
|
def extract_belief_state_func(turn, ontology, do_train=True, **kwargs): |
|
return extract_belief_state( |
|
turn, |
|
value_sep, |
|
domain_sep, |
|
slot_sep, |
|
domain_prompt_op, |
|
ontology, |
|
do_train=do_train, |
|
) |
|
|
|
return extract_belief_state_func |
|
|
|
|
|
def normalize(query: str) -> str: |
|
def comma_fix(s): |
|
|
|
return s.replace(" , ", ", ") |
|
|
|
def white_space_fix(s): |
|
|
|
return " ".join(s.split()) |
|
|
|
def lower(s): |
|
|
|
return re.sub( |
|
r"\b(?<!['\"])(\w+)(?!['\"])\b", lambda match: match.group(1).lower(), s |
|
) |
|
|
|
def space_fix(sql: str): |
|
def agg_fix(sql: str): |
|
return re.sub( |
|
r"(count|max|min|sum|avg)\s\(", |
|
lambda match: match.group(0).replace(" ", ""), |
|
sql, |
|
) |
|
|
|
def brackets_fix(sql: str): |
|
sql = re.sub(r"\(\s", lambda match: match.group(0)[:-1], sql) |
|
sql = re.sub(r"\s\)", lambda match: match.group(0)[1:], sql) |
|
|
|
return sql |
|
|
|
def double_chars_op_fix(sql: str): |
|
return re.sub( |
|
r"((>|<|!)\s=)", |
|
lambda match: match.group(0).replace(" ", ""), |
|
sql, |
|
) |
|
|
|
return double_chars_op_fix(brackets_fix(agg_fix(sql))) |
|
|
|
return space_fix(comma_fix(white_space_fix(lower(query)))) |
|
|
|
|
|
def extract_sql(turn, split): |
|
if SQL not in turn: |
|
return None |
|
_normalize = normalize if split == "train" else (lambda x: x) |
|
return _normalize(turn[SQL]) |
|
|
|
|
|
def extract_slots_without_intents(turn, value_sep, slot_sep): |
|
if SLOTS_TO_FILL not in turn or not turn[SLOTS_TO_FILL][SLOT_VALUE_TABLE]: |
|
return "None" |
|
slots = [] |
|
for svp in turn[SLOTS_TO_FILL][SLOT_VALUE_TABLE]: |
|
slots.append( |
|
svp[SLOT] |
|
+ " " |
|
+ svp[RELATION] |
|
+ " " |
|
+ value_sep.join(map(lambda x: x[VALUE], svp[VALUES])) |
|
) |
|
|
|
return (slot_sep.join(slots) + slot_sep).strip() |
|
|
|
|
|
def extract_slots_without_intents_wrapper(value_sep, slot_sep): |
|
def extract_slots_without_intents_func(turn, **kwargs): |
|
return extract_slots_without_intents(turn, value_sep, slot_sep) |
|
|
|
return extract_slots_without_intents_func |
|
|
|
|
|
def extract_role_relation_without_turn(dialog, relation_sep): |
|
return relation_sep.join(map(lambda x: x[RELATION], dialog[ROLE_RELATIONS])) |
|
|
|
|
|
def extract_role_relation_without_turn_wrapper(relation_sep): |
|
def extract_role_relation_without_turn_func(dialog, **kwargs): |
|
return extract_role_relation_without_turn(dialog, relation_sep) |
|
|
|
return extract_role_relation_without_turn_func |
|
|
|
|
|
def extrac_rewritten(turn, **kwargs): |
|
if REWRITTEN not in turn: |
|
return None |
|
return turn[REWRITTEN] |
|
|
|
|
|
def extract_options(turn, knowledge, split=None): |
|
if ROLES_TO_SELECT not in turn: |
|
return None |
|
if split == TRAIN_SPLIT: |
|
return knowledge[turn[ROLES_TO_SELECT][0]] |
|
else: |
|
return json.dumps( |
|
{OPTION_LABEL: turn[ROLES_TO_SELECT][0], CANDIDATES: knowledge} |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def extract_intents(turn, intent_sep): |
|
if not turn[ACTIVE_INTENTS]: |
|
return "None" |
|
return intent_sep.join( |
|
map(lambda intent: intent.replace("_", " "), turn[ACTIVE_INTENTS]) |
|
) |
|
|
|
|
|
def extract_intents_wrapper(intent_sep): |
|
def extract_intents_func(turn, **kwargs): |
|
return extract_intents(turn, intent_sep) |
|
|
|
return extract_intents_func |
|
|