zhuqi commited on
Commit
fe2ceb8
1 Parent(s): 8ea6225

Upload preprocess.py

Browse files
Files changed (1) hide show
  1. preprocess.py +230 -2
preprocess.py CHANGED
@@ -8,7 +8,6 @@ from tqdm import tqdm
8
  from collections import Counter
9
  from pprint import pprint
10
  from nltk.tokenize import TreebankWordTokenizer, PunktSentenceTokenizer
11
- from data.unified_datasets.multiwoz21.booking_remapper import BookingActRemapper
12
 
13
  ontology = {
14
  "domains": { # descriptions are adapted from multiwoz22, but is_categorical may be different
@@ -567,7 +566,8 @@ slot_name_map = {
567
  'train': {
568
  'day': 'day', 'time': "duration"
569
  },
570
- 'police': {}
 
571
  }
572
 
573
  reverse_da_slot_name_map = {
@@ -601,6 +601,234 @@ digit2word = {
601
  cnt_domain_slot = Counter()
602
 
603
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
604
  def reverse_da(dialogue_acts):
605
  global reverse_da_slot_name_map
606
  das = {}
 
8
  from collections import Counter
9
  from pprint import pprint
10
  from nltk.tokenize import TreebankWordTokenizer, PunktSentenceTokenizer
 
11
 
12
  ontology = {
13
  "domains": { # descriptions are adapted from multiwoz22, but is_categorical may be different
 
566
  'train': {
567
  'day': 'day', 'time': "duration"
568
  },
569
+ 'police': {},
570
+ 'booking': {}
571
  }
572
 
573
  reverse_da_slot_name_map = {
 
601
  cnt_domain_slot = Counter()
602
 
603
 
604
+ class BookingActRemapper:
605
+
606
+ def __init__(self, ontology):
607
+ self.ontology = ontology
608
+ self.reset()
609
+
610
+ def reset(self):
611
+ self.current_domains_user = []
612
+ self.current_domains_system = []
613
+ self.booked_domains = []
614
+
615
+ def retrieve_current_domain_from_user(self, turn_id, ori_dialog):
616
+ prev_user_turn = ori_dialog[turn_id - 1]
617
+
618
+ dialog_acts = prev_user_turn.get('dialog_act', [])
619
+ keyword_domains_user = get_keyword_domains(prev_user_turn)
620
+ current_domains_temp = get_current_domains_from_act(dialog_acts)
621
+ self.current_domains_user = current_domains_temp if current_domains_temp else self.current_domains_user
622
+ next_user_domains = get_next_user_act_domains(ori_dialog, turn_id)
623
+
624
+ return keyword_domains_user, next_user_domains
625
+
626
+ def retrieve_current_domain_from_system(self, turn_id, ori_dialog):
627
+
628
+ system_turn = ori_dialog[turn_id]
629
+ dialog_acts = system_turn.get('dialog_act', [])
630
+ keyword_domains_system = get_keyword_domains(system_turn)
631
+ current_domains_temp = get_current_domains_from_act(dialog_acts)
632
+ self.current_domains_system = current_domains_temp if current_domains_temp else self.current_domains_system
633
+ booked_domain_current = self.check_domain_booked(system_turn)
634
+
635
+ return keyword_domains_system, booked_domain_current
636
+
637
+ def remap(self, turn_id, ori_dialog):
638
+
639
+ keyword_domains_user, next_user_domains = self.retrieve_current_domain_from_user(turn_id, ori_dialog)
640
+ keyword_domains_system, booked_domain_current = self.retrieve_current_domain_from_system(turn_id, ori_dialog)
641
+
642
+ # only need to remap if there is a dialog action labelled
643
+ dialog_acts = ori_dialog[turn_id].get('dialog_act', [])
644
+ spans = ori_dialog[turn_id].get('span_info', [])
645
+ if dialog_acts:
646
+
647
+ flattened_acts = flatten_acts(dialog_acts)
648
+ flattened_spans = flatten_span_acts(spans)
649
+ remapped_acts, error_local = remap_acts(flattened_acts, self.current_domains_user,
650
+ booked_domain_current, keyword_domains_user,
651
+ keyword_domains_system, self.current_domains_system,
652
+ next_user_domains, self.ontology)
653
+
654
+ remapped_spans, _ = remap_acts(flattened_spans, self.current_domains_user,
655
+ booked_domain_current, keyword_domains_user,
656
+ keyword_domains_system, self.current_domains_system,
657
+ next_user_domains, self.ontology)
658
+
659
+ deflattened_remapped_acts = deflat_acts(remapped_acts)
660
+ deflattened_remapped_spans = deflat_span_acts(remapped_spans)
661
+
662
+ return deflattened_remapped_acts, deflattened_remapped_spans
663
+ else:
664
+ return dialog_acts, spans
665
+
666
+ def check_domain_booked(self, turn):
667
+
668
+ booked_domain_current = None
669
+ for domain in turn['metadata']:
670
+ if turn['metadata'][domain]["book"]["booked"] and domain not in self.booked_domains:
671
+ booked_domain_current = domain.capitalize()
672
+ self.booked_domains.append(domain)
673
+ return booked_domain_current
674
+
675
+
676
+ def get_keyword_domains(turn):
677
+ keyword_domains = []
678
+ text = turn['text']
679
+ for d in ["Hotel", "Restaurant", "Train"]:
680
+ if d.lower() in text.lower():
681
+ keyword_domains.append(d)
682
+ return keyword_domains
683
+
684
+
685
+ def get_current_domains_from_act(dialog_acts):
686
+
687
+ current_domains_temp = []
688
+ for dom_int in dialog_acts:
689
+ domain, intent = dom_int.split('-')
690
+ if domain in ["general", "Booking"]:
691
+ continue
692
+ if domain not in current_domains_temp:
693
+ current_domains_temp.append(domain)
694
+
695
+ return current_domains_temp
696
+
697
+
698
+ def get_next_user_act_domains(ori_dialog, turn_id):
699
+ domains = []
700
+ try:
701
+ next_user_act = ori_dialog[turn_id + 1]['dialog_act']
702
+ domains = get_current_domains_from_act(next_user_act)
703
+ except:
704
+ # will fail if system act is the last act of the dialogue
705
+ pass
706
+ return domains
707
+
708
+
709
+ def flatten_acts(dialog_acts):
710
+ flattened_acts = []
711
+ for dom_int in dialog_acts:
712
+ domain, intent = dom_int.split('-')
713
+ for slot_value in dialog_acts[dom_int]:
714
+ slot = slot_value[0]
715
+ value = slot_value[1]
716
+ flattened_acts.append((domain, intent, slot, value))
717
+
718
+ return flattened_acts
719
+
720
+
721
+ def flatten_span_acts(span_acts):
722
+
723
+ flattened_acts = []
724
+ for span_act in span_acts:
725
+ domain, intent = span_act[0].split("-")
726
+ flattened_acts.append((domain, intent, span_act[1], span_act[2:]))
727
+ return flattened_acts
728
+
729
+
730
+ def deflat_acts(flattened_acts):
731
+
732
+ dialog_acts = dict()
733
+
734
+ for act in flattened_acts:
735
+ domain, intent, slot, value = act
736
+ if f"{domain}-{intent}" not in dialog_acts.keys():
737
+ dialog_acts[f"{domain}-{intent}"] = [[slot, value]]
738
+ else:
739
+ dialog_acts[f"{domain}-{intent}"].append([slot, value])
740
+
741
+ return dialog_acts
742
+
743
+
744
+ def deflat_span_acts(flattened_acts):
745
+
746
+ dialog_span_acts = []
747
+ for act in flattened_acts:
748
+ domain, intent, slot, value = act
749
+ if value == 'none':
750
+ continue
751
+ new_act = [f"{domain}-{intent}", slot]
752
+ new_act.extend(value)
753
+ dialog_span_acts.append(new_act)
754
+
755
+ return dialog_span_acts
756
+
757
+
758
+ def remap_acts(flattened_acts, current_domains, booked_domain=None, keyword_domains_user=None,
759
+ keyword_domains_system=None, current_domain_system=None, next_user_domain=None, ontology=None):
760
+
761
+ # We now look for all cases that can happen: Booking domain, Booking within a domain or taxi-inform-car for booking
762
+ error = 0
763
+ remapped_acts = []
764
+
765
+ # if there is more than one current domain or none at all, we try to get booked domain differently
766
+ if len(current_domains) != 1 and booked_domain:
767
+ current_domains = [booked_domain]
768
+ elif len(current_domains) != 1 and len(keyword_domains_user) == 1:
769
+ current_domains = keyword_domains_user
770
+ elif len(current_domains) != 1 and len(keyword_domains_system) == 1:
771
+ current_domains = keyword_domains_system
772
+ elif len(current_domains) != 1 and len(current_domain_system) == 1:
773
+ current_domains = current_domain_system
774
+ elif len(current_domains) != 1 and len(next_user_domain) == 1:
775
+ current_domains = next_user_domain
776
+
777
+ for act in flattened_acts:
778
+ try:
779
+ domain, intent, slot, value = act
780
+ if f"{domain}-{intent}-{slot}" == "Booking-Book-Ref":
781
+ # We need to remap that booking act now
782
+ potential_domain = current_domains[0]
783
+ remapped_acts.append((potential_domain, "Book", "none", "none"))
784
+ if ontology_check(potential_domain, slot, ontology):
785
+ remapped_acts.append((potential_domain, "Inform", "Ref", value))
786
+ elif domain == "Booking" and intent == "Book" and slot != "Ref":
787
+ # the book intent is here actually an inform intent according to the data
788
+ potential_domain = current_domains[0]
789
+ if ontology_check(potential_domain, slot, ontology):
790
+ remapped_acts.append((potential_domain, "Inform", slot, value))
791
+ elif domain == "Booking" and intent == "Inform":
792
+ # the inform intent is here actually a request intent according to the data
793
+ potential_domain = current_domains[0]
794
+ if ontology_check(potential_domain, slot, ontology):
795
+ remapped_acts.append((potential_domain, "OfferBook", slot, value))
796
+ elif domain == "Booking" and intent in ["NoBook", "Request"]:
797
+ potential_domain = current_domains[0]
798
+ if ontology_check(potential_domain, slot, ontology):
799
+ remapped_acts.append((potential_domain, intent, slot, value))
800
+ elif f"{domain}-{intent}-{slot}" == "Taxi-Inform-Car":
801
+ # taxi-inform-car actually triggers the booking and informs on a car
802
+ remapped_acts.append((domain, "Book", "none", "none"))
803
+ remapped_acts.append((domain, intent, slot, value))
804
+ elif f"{domain}-{intent}-{slot}" in ["Train-Inform-Ref", "Train-OfferBooked-Ref"]:
805
+ # train-inform/offerbooked-ref actually triggers the booking and informs on the reference number
806
+ remapped_acts.append((domain, "Book", "none", "none"))
807
+ remapped_acts.append((domain, "Inform", slot, value))
808
+ elif domain == "Train" and intent == "OfferBooked" and slot != "Ref":
809
+ # this is actually an inform act
810
+ remapped_acts.append((domain, "Inform", slot, value))
811
+ else:
812
+ remapped_acts.append(act)
813
+ except Exception as e:
814
+ print("Error detected:", e)
815
+ error += 1
816
+
817
+ return remapped_acts, error
818
+
819
+
820
+ def ontology_check(domain_, slot_, init_ontology):
821
+
822
+ domain = domain_.lower()
823
+ slot = slot_.lower()
824
+ if slot not in init_ontology['domains'][domain]['slots']:
825
+ if slot in slot_name_map:
826
+ slot = slot_name_map[slot]
827
+ elif slot in slot_name_map[domain]:
828
+ slot = slot_name_map[domain][slot]
829
+ return slot in init_ontology['domains'][domain]['slots']
830
+
831
+
832
  def reverse_da(dialogue_acts):
833
  global reverse_da_slot_name_map
834
  das = {}