-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathutils.py
1286 lines (1085 loc) · 49.4 KB
/
utils.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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import re
import logging
from os import getenv
from copy import deepcopy
from random import choice
from common.custom_requests import request_triples_wikidata
from common.factoid import FACTOID_THRESHOLD
from common.combined_classes import combined_classes, TOPIC_GROUPS
from common.join_pattern import *
from common import food, books, music, news, travel
from common import art, science, movies, animals, gaming, sport, gossip
import sentry_sdk
logger = logging.getLogger(__name__)
sentry_sdk.init(getenv("SENTRY_DSN"))
other_skills = {
"dff_intent_responder_skill",
"dff_program_y_dangerous_skill",
"misheard_asr",
"christmas_new_year_skill",
"superbowl_skill",
"oscar_skill",
"valentines_day_skill",
}
scenario_skills = {
"dff_movie_skill",
"personal_info_skill", # 'short_story_skill',
"dff_book_skill",
"dff_weather_skill",
"emotion_skill",
"dummy_skill_dialog",
"meta_script_skill",
"dff_coronavirus_skill",
"small_talk_skill",
"news_api_skill",
"game_cooperative_skill",
}
retrieve_skills = {
"dff_program_y_skill",
"alice",
"eliza",
"book_tfidf_retrieval",
"entertainment_tfidf_retrieval",
"fashion_tfidf_retrieval",
"movie_tfidf_retrieval",
"music_tfidf_retrieval",
"politics_tfidf_retrieval",
"science_technology_tfidf_retrieval",
"sport_tfidf_retrieval",
"animals_tfidf_retrieval",
"convert_reddit",
"topicalchat_convert_retrieval",
"dff_program_y_wide_skill",
"knowledge_grounding_skill",
}
okay_statements = {
"Okay.",
"That's cool!",
"Interesting.",
"Sounds interesting.",
"Sounds interesting!",
"OK.",
"Cool!",
"Thanks!",
"Okay, thanks.",
"I'm glad you think so!",
"Sorry, I don't have an answer for that!",
"Let's talk about something else.",
"As you wish.",
"All right.",
"Right.",
"Anyway.",
"Oh, okay.",
"Oh, come on.",
"Really?",
"Okay. I got it.",
"Well, okay.",
"Well, as you wish.",
}
service_intents = {
"lets_chat_about",
"tell_me_more",
"topic_switching",
"yes",
"opinion_request",
"dont_understand",
"no",
"stupid",
"weather_forecast_intent",
"doing_well",
"tell_me_a_story",
"choose_topic",
}
high_priority_intents = {
"dff_intent_responder_skill": {
"cant_do",
"exit",
"repeat",
"what_can_you_do",
"what_is_your_job",
"what_is_your_name",
"where_are_you_from",
"who_made_you",
},
"dff_grounding_skill": {"what_are_you_talking_about"},
}
low_priority_intents = {"dont_understand", "what_time", "choose_topic"}
MULTILABEL_TASKS = [
"emotion_classification",
"toxic_classification",
]
DP_THRESHOLDS = {
"Food": 0,
"Movies_TV": 0,
"Leisure": 0,
"Beauty": 0,
"Clothes": 0,
"Depression": 0,
"Celebrities&Events": 0,
"Family&Relationships": 0,
"Health&Medicine": 0,
"Education": 0,
"Sports": 0,
"Books&Literature": 0.3,
"Videogames": 0.3,
"Politics": 0.3,
"ArtificialIntelligence": 0.3,
"MassTransit": 0.3,
}
THRESHOLDS = {
"deeppavlov_topics": {class_: DP_THRESHOLDS.get(class_, 0.9) for class_ in combined_classes["deeppavlov_topics"]},
"toxic_classification": {
"identity_hate": 0.5,
"insult": 0.5,
"not_toxic": 0.5,
"obscene": 0.5,
"severe_toxic": 0.5,
"sexual_explicit": 0.6,
"threat": 0.5,
"toxic": 0.5,
},
}
midas_classes = {
"semantic_request": {
"question": [
"open_question_factual",
"open_question_opinion",
"open_question_personal",
"yes_no_question",
"clarifying_question",
],
"command": ["command", "dev_command"],
"opinion": ["appreciation", "opinion", "complaint", "comment"],
"statement": ["statement"],
"answer": ["other_answers", "pos_answer", "neg_answer"],
},
"functional_request": {
"incomplete": ["abandon", "nonsense"],
"social_convention": ["opening", "closing", "hold", "back-channeling"],
"apology": [],
"other": ["uncertain", "non_compliant", "correction"],
},
}
MIDAS_SEMANTIC_LABELS = sum([intent_list for intent_list in midas_classes["semantic_request"].values()], [])
MIDAS_FUNCTIONAL_LABELS = sum([intent_list for intent_list in midas_classes["functional_request"].values()], [])
def get_skill_outputs_from_dialog(utterances, skill_name, activated=False):
"""
Extract list of dictionaries with already formatted outputs of `skill_name` from full dialog.
If `activated=True`, skill also should be chosen as `active_skill`;
otherwise, empty list.
Args:
utterances: utterances, the first one is user's reply
skill_name: name of target skill
activated: if target skill should be chosen by response selector on previous step or not
Returns:
list of dictionaries with formatted outputs of skill
"""
result = []
skills_outputs = []
for uttr in utterances:
if "active_skill" in uttr:
final_response = uttr.get("orig_text", None) if uttr.get("orig_text", None) is not None else uttr["text"]
for skop in skills_outputs:
# need to check text-response for skills with several hypotheses
if skop["skill_name"] == skill_name:
if activated and skop["text"] in final_response and uttr["active_skill"] == skill_name:
# removed one condition as if scop contains skill_name and text, its len is > 0
result.append(skop)
else:
if not activated and skop:
result.append(skop)
elif "hypotheses" in uttr:
skills_outputs = uttr["hypotheses"]
return result
def transform_vbg(s):
"""
Transform infinitive form of verb to Ving form.
Args:
s: verb infinitive
Returns:
string with required verb form
"""
import re
# by Anastasia Kravtsova
s += "+VBG"
# irregular cases
s1 = re.compile(r"(?<![a-z])be\+VBG")
s2 = re.compile(r"(?<![aouiey])([^aouiey][aouiey]([^aouieywr]))\+VBG")
s3 = re.compile(r"ie\+VBG")
s4 = re.compile(r"(ee)\+VBG")
s5 = re.compile(r"e\+VBG")
# regular case
s6 = re.compile(r"\+VBG")
# irregular cases
s = re.sub(s1, "being", s)
s = re.sub(s2, r"\1\2ing", s)
s = re.sub(s3, r"ying", s)
s = re.sub(s4, r"\1ing", s)
s = re.sub(s5, r"ing", s)
# regular case
s = re.sub(s6, "ing", s)
return s
def get_list_of_active_skills(utterances):
"""
Extract list of active skills names
Args:
utterances: utterances, the first one is user's reply
Returns:
list of string skill names
"""
result = []
for uttr in utterances:
if "active_skill" in uttr:
result.append(uttr["active_skill"])
return result
def get_user_replies_to_particular_skill(utterances, skill_name):
"""
Return user's responses to particular skill if it was active
Args:
utterances:
skill_name:
Returns:
list of string response
"""
result = []
for i, uttr in enumerate(utterances):
if uttr.get("active_skill", "") == skill_name:
result.append(utterances[i - 1]["text"])
return result
yes_templates = re.compile(
r"(\byes\b|\byup\b|\byep\b|\bsure\b|go ahead|\byeah\b|\bok\b|okay|^(kind of|kinda)\.?$|"
r"^why not\.?$|^tell me\.?$|^i (agree|do|did|like|have|had|think so)\.?$)"
)
def is_yes(annotated_phrase):
yes_detected = "yes" in get_intents(annotated_phrase, which="intent_catcher", probs=False)
midas_yes_detected = "pos_answer" in get_intents(annotated_phrase, which="midas", probs=False)
# TODO: intent catcher not catches 'yes thanks!'
if yes_detected or midas_yes_detected or re.search(yes_templates, annotated_phrase.get("text", "").lower()):
return True
return False
no_templates = re.compile(r"(\bno\b|\bnot\b|no way|don't|no please|i disagree|^neither.?$)")
DONOTKNOW_LIKE = [r"(i )?(do not|don't) know", "you (choose|decide|pick up)", "no idea"]
DONOTKNOW_LIKE_PATTERN = re.compile(join_sentences_in_or_pattern(DONOTKNOW_LIKE), re.IGNORECASE)
def is_donot_know(annotated_phrase):
if DONOTKNOW_LIKE_PATTERN.search(annotated_phrase.get("text", "")):
return True
return False
def is_no_intent(annotated_phrase):
no_detected = "no" in get_intents(annotated_phrase, which="intent_catcher", probs=False)
midas_no_detected = False # "neg_answer" in get_intents(annotated_phrase, which='midas', probs=False)
is_not_idontknow = not is_donot_know(annotated_phrase)
if (no_detected or midas_no_detected) and is_not_idontknow:
return True
return False
def is_no(annotated_phrase):
no_detected = "no" in get_intents(annotated_phrase, which="intent_catcher", probs=False)
midas_no_detected = "neg_answer" in get_intents(annotated_phrase, which="midas", probs=False)
# TODO: intent catcher thinks that horrible is no intent'
user_phrase = annotated_phrase.get("text", "").lower().strip().replace(".", "")
is_not_horrible = "horrible" != user_phrase
no_regexp_detected = re.search(no_templates, annotated_phrase.get("text", "").lower())
is_not_idontknow = not is_donot_know(annotated_phrase)
_yes = is_yes(annotated_phrase)
if is_not_horrible and (no_detected or midas_no_detected or no_regexp_detected) and is_not_idontknow and not _yes:
return True
return False
def is_question(text):
return "?" in text
def substitute_nonwords(text):
return re.sub(r"\W+", " ", text).strip()
def get_intent_name(text):
splitter = "#+#"
if splitter not in text:
return None
intent_name = text.split(splitter)[-1]
intent_name = re.sub(r"\W", " ", intent_name.lower()).strip()
return intent_name
OPINION_REQUEST_PATTERN = re.compile(
r"(don't|do not|not|are not|are|do)?\s?you\s"
r"(like|dislike|adore|hate|love|believe|consider|get|know|taste|think|"
r"recognize|sure|understand|feel|fond of|care for|fansy|appeal|suppose|"
r"imagine|guess)",
re.IGNORECASE,
)
OPINION_EXPRESSION_PATTERN = re.compile(
r"\bi (don't|do not|not|am not|'m not|am|do)?\s?"
r"(like|dislike|adore|hate|love|believe|consider|get|know|taste|think|"
r"recognize|sure|understand|feel|fond of|care for|fansy|appeal|suppose|"
r"imagine|guess)",
re.IGNORECASE,
)
def is_opinion_request(annotated_utterance):
intents = get_intents(annotated_utterance, which="all", probs=False)
intent_detected = any([intent in intents for intent in ["Opinion_RequestIntent", "open_question_opinion"]])
uttr_text = annotated_utterance.get("text", "")
if intent_detected or (OPINION_REQUEST_PATTERN.search(uttr_text) and "?" in uttr_text):
return True
else:
return False
def is_opinion_expression(annotated_utterance):
all_intents = get_intents(annotated_utterance, which="all")
intent_detected = any([intent in all_intents for intent in ["opinion", "Opinion_ExpressionIntent"]])
uttr_text = annotated_utterance.get("text", "")
if intent_detected or OPINION_EXPRESSION_PATTERN.search(uttr_text):
return True
else:
return False
def get_outputs_with_response_from_dialog(utterances, response, activated=False):
"""
Extract list of dictionaries with already formatted outputs of different skills from full dialog
which replies containe `response`.
If `activated=True`, skill also should be chosen as `active_skill`;
otherwise, empty list.
Args:
utterances: utterances, the first one is user's reply
response: target text to search among bot utterances
activated: if target skill should be chosen by response selector on previous step or not
Returns:
list of dictionaries with formatted outputs of skill
"""
result = []
skills_outputs = []
for uttr in utterances:
if "active_skill" in uttr:
final_response = uttr["text"]
for skop in skills_outputs:
# need to check text-response for skills with several hypotheses
if response in skop["text"]:
if activated and skop["text"] in final_response and skop:
result.append(skop)
else:
if not activated and skop:
result.append(skop)
elif "hypotheses" in uttr:
skills_outputs = uttr["hypotheses"]
return result
def get_not_used_template(used_templates, all_templates, any_if_no_available=True):
"""
Choose not used template among all templates
Args:
used_templates: list of templates already used in the dialog
all_templates: list of all available templates
Returns:
string template
"""
available = list(set(all_templates).difference(set(used_templates)))
if available:
return choice(available)
elif any_if_no_available:
return choice(all_templates)
else:
return ""
def get_all_not_used_templates(used_templates, all_templates):
"""
Return all not used template among all templates
Args:
used_templates: list of templates already used in the dialog
all_templates: list of all available templates
Returns:
string template
"""
available = list(set(all_templates).difference(set(used_templates)))
return available
def _probs_to_labels(answer_probs, max_proba=True, threshold=0.5):
if not answer_probs:
return []
if isinstance(threshold, dict):
answer_labels = [key for key in answer_probs if answer_probs[key] > threshold.get(key, 0)]
if max_proba:
answer_labels = [key for key in answer_labels if answer_probs[key] == max(answer_probs.values())]
else:
answer_labels = [label for label in answer_probs if answer_probs[label] > threshold]
if not answer_labels and max_proba:
answer_labels = [key for key in answer_probs if answer_probs[key] == max(answer_probs.values())]
return answer_labels
def _labels_to_probs(answer_labels, all_labels):
answer_probs = dict()
for label in all_labels:
if label in answer_labels:
answer_probs[label] = 1
else:
answer_probs[label] = 0
return answer_probs
def _get_combined_annotations(annotated_utterance, model_name, threshold=0.5):
answer_probs, answer_labels = {}, []
try:
annotations = annotated_utterance["annotations"]
combined_annotations = annotations.get("combined_classification", {})
if combined_annotations and isinstance(combined_annotations, list):
combined_annotations = combined_annotations[0]
if model_name in combined_annotations:
answer_probs = combined_annotations[model_name]
else:
logger.warning(f"Not found Model name {model_name} in combined annotations {combined_annotations}")
old_style_toxic = all(
[model_name == "toxic_classification", "factoid_classification" not in combined_annotations]
)
if model_name in MULTILABEL_TASKS or old_style_toxic:
answer_labels = _probs_to_labels(answer_probs, max_proba=False, threshold=threshold)
elif model_name == "factoid_classification" and answer_probs.get("is_factoid", 0) < threshold:
answer_labels = ["is_conversational"]
elif model_name == "deeppavlov_topics":
answer_labels = _probs_to_labels(answer_probs, max_proba=True, threshold=THRESHOLDS["deeppavlov_topics"])
elif model_name == "toxic_classification":
answer_labels = _probs_to_labels(answer_probs, max_proba=True, threshold=THRESHOLDS["toxic_classification"])
else:
answer_labels = _probs_to_labels(answer_probs, max_proba=True, threshold=threshold)
except Exception as e:
sentry_sdk.capture_exception(e)
logger.exception(e)
return answer_probs, answer_labels
def _process_text(answer):
if isinstance(answer, dict) and "text" in answer:
return answer["text"]
else:
return answer
def _process_old_sentiment(answer):
# Input: all sentiment annotations. Output: probs
if isinstance(answer[0], str) and isinstance(answer[1], float):
# support old sentiment output
curr_answer = {}
for key in combined_classes["sentiment_classification"]:
if key == answer[0]:
curr_answer[key] = answer[1]
else:
curr_answer[key] = 0.5 * (1 - answer[1])
answer_probs = curr_answer
return answer_probs
else:
logger.warning("_process_old_sentiment got file with an output that is not old-style")
return answer
def _get_plain_annotations(annotated_utterance, model_name, threshold=0.5):
answer_probs, answer_labels = {}, []
try:
annotations = annotated_utterance["annotations"]
answer = annotations[model_name]
answer = _process_text(answer)
if isinstance(answer, list):
if model_name == "sentiment_classification":
answer_probs = _process_old_sentiment(answer)
answer_labels = _probs_to_labels(answer_probs, max_proba=True, threshold=threshold)
else:
answer_labels = answer
answer_probs = _labels_to_probs(answer_labels, combined_classes[model_name])
else:
answer_probs = answer
if model_name == "toxic_classification":
# this function is only for plain annotations (when toxic_classification is a separate annotator)
answer_labels = _probs_to_labels(answer_probs, max_proba=False, threshold=threshold)
elif model_name == "factoid_classification" and answer_probs.get("is_factoid", 0) < threshold:
answer_labels = ["is_conversational"]
else:
answer_labels = _probs_to_labels(answer_probs, max_proba=True, threshold=threshold)
except Exception as e:
logger.warning(e)
return answer_probs, answer_labels
def print_combined(combined_output):
combined_output = deepcopy(combined_output)
for i in range(len(combined_output)):
for key in combined_output[i]:
for class_ in combined_output[i][key]:
combined_output[i][key][class_] = round(combined_output[i][key][class_], 2)
logger.info(f"Combined classifier output is {combined_output}")
def _get_etc_model(annotated_utterance, model_name, probs, default_probs, default_labels, threshold=0.5):
"""Function to get emotion classifier annotations from annotated utterance.
Args:
annotated_utterance: dictionary with annotated utterance, or annotations
probs: return probabilities or not
default_probs: default probs to return.
default_labels: default labels to return.
Threshold: threshold for classification
Returns:
dictionary with emotion probablilties, if probs == True, or emotion labels if probs != True
"""
try:
if model_name in annotated_utterance.get("annotations", {}):
answer_probs, answer_labels = _get_plain_annotations(
annotated_utterance, model_name=model_name, threshold=threshold
)
elif "combined_classification" in annotated_utterance.get("annotations", {}):
answer_probs, answer_labels = _get_combined_annotations(
annotated_utterance, model_name=model_name, threshold=threshold
)
else:
answer_probs, answer_labels = default_probs, default_labels
except Exception as e:
logger.exception(e, stack_info=True)
answer_probs, answer_labels = default_probs, default_labels
if probs: # return probs
return answer_probs
else:
return answer_labels
def get_toxic(annotated_utterance, probs=True, default_probs=None, default_labels=None):
"""Function to get toxic classifier annotations from annotated utterance.
Args:
annotated_utterance: dictionary with annotated utterance, or annotations
probs: return probabilities or not
default: default value to return. If it is None, returns empty dict/list depending on probs argument
Returns:
dictionary with toxic probablilties, if probs == True, or toxic labels if probs != True
"""
default_probs = {} if default_probs is None else default_probs
default_labels = [] if default_labels is None else default_labels
return _get_etc_model(
annotated_utterance,
"toxic_classification",
probs=probs,
default_probs=default_probs,
default_labels=default_labels,
)
def get_factoid(annotated_utterance, probs=True, default_probs=None, default_labels=None):
"""Function to get factoid classifier annotations from annotated utterance.
Args:
annotated_utterance: dictionary with annotated utterance, or annotations
probs: return probabilities or not
default: default value to return. If it is None, returns empty dict/list depending on probs argument
Returns:
dictionary with factoid probablilties, if probs == True, or factoid labels if probs != True
"""
default_probs = {"is_conversational": 1} if default_probs is None else default_probs
default_labels = ["is_conversational"] if default_labels is None else default_labels
return _get_etc_model(
annotated_utterance,
"factoid_classification",
probs=probs,
default_probs=default_probs,
default_labels=default_labels,
threshold=FACTOID_THRESHOLD,
)
def get_sentiment(annotated_utterance, probs=True, default_probs=None, default_labels=None):
"""Function to get sentiment classifier annotations from annotated utterance.
Args:
annotated_utterance: dictionary with annotated utterance, or annotations
probs: return probabilities or not
default: default value to return. If it is None, returns empty dict/list depending on probs argument
Returns:
dictionary with sentiment probablilties, if probs == True, or sentiment labels if probs != True
"""
default_probs = {"positive": 0, "negative": 0, "neutral": 1} if default_probs is None else default_probs
default_labels = ["neutral"] if default_labels is None else default_labels
return _get_etc_model(
annotated_utterance,
"sentiment_classification",
probs=probs,
default_probs=default_probs,
default_labels=default_labels,
)
def get_emotions(annotated_utterance, probs=True, default_probs=None, default_labels=None):
"""Function to get emotion classifier annotations from annotated utterance.
Args:
annotated_utterance: dictionary with annotated utterance, or annotations
probs: return probabilities or not
default: default value to return. If it is None, returns empty dict/list depending on probs argument
Returns:
dictionary with emotion probablilties, if probs == True, or emotion labels if probs != True
"""
default_probs = (
{"anger": 0, "fear": 0, "joy": 0, "love": 0, "sadness": 0, "surprise": 0, "neutral": 1}
if default_probs is None
else default_probs
)
default_labels = ["neutral"] if default_labels is None else default_labels
return _get_etc_model(
annotated_utterance,
"emotion_classification",
probs=probs,
default_probs=default_probs,
default_labels=default_labels,
)
def get_topics(annotated_utterance, probs=False, default_probs=None, default_labels=None, which="all"):
"""Function to get topics from particular annotator or all detected.
Args:
annotated_utterance: dictionary with annotated utterance
probs: if False we return labels, otherwise we return probs
default_probs: default probabilities to return
default_labels: default labels to return
which: which topics to return.
'all' means topics by `cobot_topics` and `cobot_dialogact_topics`,
'cobot_topics' means topics by `cobot_topics`,
'cobot_dialogact_topics' means topics by `cobot_dialogact_topics`.
'deeppavlov_topics' means topics by `deeppavlov_topics`.
Returns:
list of topic labels, if probs == False,
dictionary where all keys are topic labels and values are probabilities, if probs == True
"""
default_probs = {} if default_probs is None else default_probs
default_labels = [] if default_labels is None else default_labels
annotations = annotated_utterance.get("annotations", {})
cobot_topics_probs, cobot_topics_labels = {}, []
if "cobot_topics" in annotations:
cobot_topics_labels = _process_text(annotations.get("cobot_topics", {}))
if "combined_classification" in annotations and not cobot_topics_labels:
cobot_topics_probs, cobot_topics_labels = _get_combined_annotations(
annotated_utterance, model_name="cobot_topics"
)
cobot_topics_labels = _process_text(cobot_topics_labels)
if not cobot_topics_probs:
cobot_topics_probs = _labels_to_probs(cobot_topics_labels, combined_classes.get("cobot_topics", {}))
cobot_da_topics_probs, cobot_da_topics_labels = {}, []
if "cobot_dialogact" in annotations and "topics" in annotations["cobot_dialogact"]:
cobot_da_topics_labels = annotations["cobot_dialogact"]["topics"]
elif "cobot_dialogact_topics" in annotations:
cobot_da_topics_labels = annotations["cobot_dialogact_topics"]
if "combined_classification" in annotations and not cobot_da_topics_labels:
cobot_da_topics_probs, cobot_da_topics_labels = _get_combined_annotations(
annotated_utterance, model_name="cobot_dialogact_topics"
)
cobot_da_topics_labels = _process_text(cobot_da_topics_labels)
if not cobot_da_topics_probs:
cobot_da_topics_probs = _labels_to_probs(cobot_da_topics_labels, combined_classes["cobot_dialogact_topics"])
dp_topics_probs, dp_topics_labels = {}, []
if "combined_classification" in annotations and not dp_topics_labels:
dp_topics_probs, dp_topics_labels = _get_combined_annotations(
annotated_utterance, model_name="deeppavlov_topics"
)
topics_ru_probs, topics_ru_labels = {}, []
if "topics_ru" in annotations:
topics_ru_probs, topics_ru_labels = _get_combined_annotations(annotated_utterance, model_name="topics_ru")
if which == "all":
answer_labels = cobot_topics_labels + cobot_da_topics_labels + dp_topics_labels + topics_ru_labels
answer_probs = {**cobot_topics_probs, **cobot_da_topics_probs, **dp_topics_probs, **topics_ru_probs}
elif which == "cobot_topics":
answer_probs, answer_labels = cobot_topics_probs, cobot_topics_labels
elif which == "cobot_dialogact_topics":
answer_probs, answer_labels = cobot_da_topics_probs, cobot_da_topics_labels
elif which == "deeppavlov_topics":
answer_probs, answer_labels = dp_topics_probs, dp_topics_labels
elif which == "topics_ru":
answer_probs, answer_labels = topics_ru_probs, topics_ru_labels
else:
logger.exception(f"Unknown input type in get_topics: {which}")
answer_probs, answer_labels = default_probs, default_labels
if probs:
return answer_probs
else:
return answer_labels
def get_intents(annotated_utterance, probs=False, default_probs=None, default_labels=None, which="all"):
"""Function to get intents from particular annotator or all detected.
Args:
annotated_utterance: dictionary with annotated utterance
probs: if False we return labels, otherwise we return probs
default_probs: default probabilities to return
default_labels: default labels to return
which: which intents to return:
'all' means intents detected by `intent_catcher`,
`cobot_dialogact_intents` and `midas_classification`.
'intent_catcher' means intents detected by `intent_catcher`.
'cobot_dialogact_intents' means intents detected by `cobot_dialogact_intents`.
'midas' means intents detected by `midas_classification`.
Returns:
list of intent labels, if probs == False,
dictionary where all keys are intent labels and values are probabilities, if probs == True
"""
default_probs = {} if default_probs is None else default_probs
default_labels = [] if default_labels is None else default_labels
annotations = annotated_utterance.get("annotations", {})
intents = annotations.get("intent_catcher", {})
detected_intents = [k for k, v in intents.items() if v.get("detected", 0) == 1]
detected_intent_probs = {key: 1 for key in detected_intents}
midas_intent_probs = annotations.get("midas_classification", {})
if "combined_classification" in annotations and not midas_intent_probs:
midas_intent_probs, midas_intent_labels = _get_combined_annotations(
annotated_utterance, model_name="midas_classification"
)
if isinstance(midas_intent_probs, dict) and midas_intent_probs:
semantic_midas_probs = {k: v for k, v in midas_intent_probs.items() if k in MIDAS_SEMANTIC_LABELS}
functional_midas_probs = {k: v for k, v in midas_intent_probs.items() if k in MIDAS_FUNCTIONAL_LABELS}
if semantic_midas_probs:
max_midas_semantic_prob = max(semantic_midas_probs.values())
else:
max_midas_semantic_prob = 0.0
if functional_midas_probs:
max_midas_functional_prob = max(functional_midas_probs.values())
else:
max_midas_functional_prob = 0.0
midas_semantic_intent_labels = [k for k, v in semantic_midas_probs.items() if v == max_midas_semantic_prob]
midas_functional_intent_labels = [
k for k, v in functional_midas_probs.items() if v == max_midas_functional_prob
]
midas_intent_labels = midas_semantic_intent_labels + midas_functional_intent_labels
elif isinstance(midas_intent_probs, list):
if midas_intent_probs:
# now it's a list of dictionaries. length of list is n sentences
midas_intent_labels = []
for midas_sent_probs in midas_intent_probs:
max_midas_sent_prob = max(midas_sent_probs.values())
midas_intent_labels += [k for k, v in midas_sent_probs.items() if v == max_midas_sent_prob]
_midas_intent_probs = deepcopy(midas_intent_probs)
midas_intent_probs = {}
class_names = list(set(sum([list(resp.keys()) for resp in _midas_intent_probs], [])))
for class_name in class_names:
max_proba = max([resp.get(class_name, 0.0) for resp in _midas_intent_probs])
midas_intent_probs[class_name] = max_proba
else:
midas_intent_probs = {}
midas_intent_labels = []
else:
midas_intent_labels = []
cobot_da_intent_probs, cobot_da_intent_labels = {}, []
if "cobot_dialogact" in annotations and "intents" in annotations["cobot_dialogact"]:
cobot_da_intent_labels = annotated_utterance["annotations"]["cobot_dialogact"]["intents"]
elif "cobot_dialogact_intents" in annotations:
cobot_da_intent_labels = annotated_utterance["annotations"]["cobot_dialogact_intents"]
if "combined_classification" in annotations and not cobot_da_intent_labels:
cobot_da_intent_probs, cobot_da_intent_labels = _get_combined_annotations(
annotated_utterance, model_name="cobot_dialogact_intents"
)
cobot_da_intent_labels = _process_text(cobot_da_intent_labels)
if not cobot_da_intent_probs:
cobot_da_intent_probs = _labels_to_probs(cobot_da_intent_labels, combined_classes["cobot_dialogact_intents"])
if which == "all":
answer_probs = {**detected_intent_probs, **cobot_da_intent_probs, **midas_intent_probs}
answer_labels = detected_intents + cobot_da_intent_labels + midas_intent_labels
elif which == "intent_catcher":
answer_probs, answer_labels = detected_intent_probs, detected_intents
elif which == "cobot_dialogact_intents":
answer_probs, answer_labels = cobot_da_intent_probs, cobot_da_intent_labels
elif which == "midas":
answer_probs, answer_labels = midas_intent_probs, midas_intent_labels
else:
logger.warning(f"Unknown type in get_intents {which}")
answer_probs, answer_labels = default_probs, default_labels
if probs:
return answer_probs
else:
return answer_labels
COBOT_ENTITIES_SKIP_LABELS = ["anaphor"]
def get_entities(annotated_utterance, only_named=False, with_labels=False, return_lemmas=False):
entities = []
if not only_named:
if "entity_detection" in annotated_utterance.get("annotations", {}):
# for english and russian languages
labelled_entities = annotated_utterance["annotations"]["entity_detection"].get("labelled_entities", [])
# skip some labels
entities = [ent for ent in labelled_entities if ent["label"] not in COBOT_ENTITIES_SKIP_LABELS]
if not with_labels:
entities = [ent["text"] for ent in entities]
elif "spacy_nounphrases" in annotated_utterance.get("annotations", {}):
# for english language
entities = annotated_utterance.get("annotations", {}).get("spacy_nounphrases", [])
if with_labels:
# actually there are no labels for cobot nounphrases
# so, let's make it as for cobot_entities format
entities = [{"text": ent, "label": "misc"} for ent in entities]
elif "spacy_annotator" in annotated_utterance.get("annotations", {}):
# for russian language
words = annotated_utterance["annotations"]["spacy_annotator"]
for word in words:
if word.get("pos_", "") == "NOUN":
entities += [{"text": word["lemma_"] if return_lemmas else word["text"], "label": "misc"}]
if not with_labels:
entities = [ent["text"] for ent in entities]
else:
# `ner` contains list of lists of dicts. the length of the list is n-sentences
# each entity is {"confidence": 1, "end_pos": 1, "start_pos": 0, "text": "unicorns", "type": "ORG"}
entities = annotated_utterance.get("annotations", {}).get("ner", [])
entities = sum(entities, []) # flatten list, now it's a list of dicts-entities
if not with_labels:
entities = [ent["text"] for ent in entities]
return entities if entities is not None else []
def get_named_persons(annotated_utterance):
named_entities = get_entities(annotated_utterance, only_named=True, with_labels=True)
all_entities = get_entities(annotated_utterance, only_named=False, with_labels=True)
named_persons = []
if "cobot_entities" in annotated_utterance["annotations"]:
for ent in all_entities:
if ent["label"] == "person":
named_persons.append(ent["text"])
if "ner" in annotated_utterance["annotations"]:
for ent in named_entities:
if ent["type"] == "PER":
named_persons.append(ent["text"])
named_persons = list(set(named_persons))
return named_persons
def get_named_locations(annotated_utterance):
named_entities = get_entities(annotated_utterance, only_named=True, with_labels=True)
all_entities = get_entities(annotated_utterance, only_named=False, with_labels=True)
named_locations = []
if "cobot_entities" in annotated_utterance["annotations"]:
for ent in all_entities:
if ent["label"] == "location":
named_locations.append(ent["text"])
if len(named_locations) == 0 and "ner" in annotated_utterance["annotations"]:
for ent in named_entities:
if ent["type"] == "LOC" and ent["text"] != "alexa":
_is_part_of_other_entity = False
for cobot_ent in all_entities:
if ent["text"] in cobot_ent["text"] and cobot_ent["label"] != "location":
_is_part_of_other_entity = True
if not _is_part_of_other_entity:
named_locations.append(ent["text"])
named_locations = list(set(named_locations))
if re.search(r"\bjapan\b", annotated_utterance["text"], re.IGNORECASE) and "japan" not in named_locations:
# NER does not catch this country at all!
named_locations.append("japan")
return named_locations
def get_raw_entity_names_from_annotations(annotations):
"""
Args:
annotated_utterance: annotated utterance
Returns:
Wikidata entities we received from annotations
"""
raw_el_output = annotations.get("entity_linking", [{}])
entities = []
try:
if raw_el_output:
if isinstance(raw_el_output[0], dict):
entities = raw_el_output[0].get("entity_ids", [])
if isinstance(raw_el_output[0], list):
entities = raw_el_output[0][0]
except Exception as e:
error_message = f"Wrong entity linking output format {raw_el_output} : {e}"
sentry_sdk.capture_exception(e)
logger.exception(error_message)
return entities
def get_entity_names_from_annotations(annotated_utterance, stopwords=None, default_entities=None):
"""
Args:
annotated_utterance: annotated utterance
stopwords_file: name of file with stopwords
Returns:
Names of named entities we received from annotations
"""
default_entities = [] if default_entities is None else default_entities
stopwords = stopwords if stopwords else []
full_text = annotated_utterance.get("text", "").lower()
named_entities = [full_text] if full_text in default_entities else []
annotations = annotated_utterance.get("annotations", {})
for tmp in annotations.get("ner", []):
if tmp and "text" in tmp[0]:
named_entities.append(tmp[0]["text"])