From bcb464c5b62c0b0010ca61a704edb0d6ef3d3502 Mon Sep 17 00:00:00 2001 From: Nolwenn <28621493+NoB0@users.noreply.github.com> Date: Mon, 19 Aug 2024 11:52:11 +0200 Subject: [PATCH 1/2] Participant is not initialised correctly when loading from JSON Fixes #260 --- dialoguekit/utils/dialogue_evaluation.py | 8 ++++---- dialoguekit/utils/dialogue_reader.py | 3 ++- tests/utils/test_dialogue_reader.py | 5 +++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/dialoguekit/utils/dialogue_evaluation.py b/dialoguekit/utils/dialogue_evaluation.py index a179405a..228946ff 100644 --- a/dialoguekit/utils/dialogue_evaluation.py +++ b/dialoguekit/utils/dialogue_evaluation.py @@ -89,7 +89,7 @@ def user_act_ratio(self) -> Dict[str, float]: for dialogue in self._dialogues: for utterance in dialogue.utterances: - sender = str(utterance.participant) + sender = utterance.participant.name statistics[sender] += 1 if len(statistics.keys()) > 2: @@ -152,7 +152,7 @@ def reward(self) -> Dict[str, List[Dict[str, float]]]: # Start dialogue with Agent first. for j, utterance in enumerate(dialogue.utterances): - if utterance.participant == DialogueParticipant.AGENT.name: + if utterance.participant == DialogueParticipant.AGENT: dialogue_utterances_start_agent: List[ AnnotatedUtterance ] = dialogue.utterances[j:] @@ -209,7 +209,7 @@ def _check_included_intents(self) -> Dict[str, Any]: for utterance in dialogue.utterances: if ( isinstance(utterance, AnnotatedUtterance) - and utterance.participant == DialogueParticipant.USER.name + and utterance.participant == DialogueParticipant.USER ): intents = [ Intent(intent.label.split(".")[0]) @@ -244,7 +244,7 @@ def _user_agent_ratio(self, results: Dict[str, Any]) -> Dict[str, Any]: num_user_acts = sum( 1 for utterance in dialogue.utterances - if utterance.participant == DialogueParticipant.USER.name + if utterance.participant == DialogueParticipant.USER ) results["dialogues"][i]["user_turns"] = num_user_acts results["dialogues"][i][ diff --git a/dialoguekit/utils/dialogue_reader.py b/dialoguekit/utils/dialogue_reader.py index d1d7f23e..f38ccc5e 100644 --- a/dialoguekit/utils/dialogue_reader.py +++ b/dialoguekit/utils/dialogue_reader.py @@ -10,6 +10,7 @@ from dialoguekit.core.feedback import BinaryFeedback, UtteranceFeedback from dialoguekit.core.intent import Intent from dialoguekit.core.slot_value_annotation import SlotValueAnnotation +from dialoguekit.participant import DialogueParticipant _FIELD_UTTERANCE = "utterance" _FIELD_DIALOGUE_ACTS = "dialogue_acts" @@ -58,7 +59,7 @@ def json_to_annotated_utterance( Returns: An AnnotatedUtterance object representation of the json utterance. """ - participant = json_utterance.get(_FIELD_PARTICIPANT) + participant = DialogueParticipant[json_utterance.get(_FIELD_PARTICIPANT)] utterance_text = json_utterance.get(_FIELD_UTTERANCE) utterance_id = json_utterance.get(_FIELD_UTTERANCE_ID) diff --git a/tests/utils/test_dialogue_reader.py b/tests/utils/test_dialogue_reader.py index b021b693..9f7ef569 100644 --- a/tests/utils/test_dialogue_reader.py +++ b/tests/utils/test_dialogue_reader.py @@ -6,6 +6,7 @@ from dialoguekit.core.feedback import BinaryFeedback from dialoguekit.core.intent import Intent +from dialoguekit.participant import DialogueParticipant from dialoguekit.utils.dialogue_reader import json_to_dialogues @@ -20,8 +21,8 @@ def test_json_to_dialogues() -> None: assert dialogues[0].user_id == "TEST03" assert dialogues[-1].agent_id == "Agent" assert dialogues[-1].user_id == "User" - assert dialogues[0].utterances[0].participant == "USER" - assert dialogues[0].utterances[1].participant == "AGENT" + assert dialogues[0].utterances[0].participant == DialogueParticipant.USER + assert dialogues[0].utterances[1].participant == DialogueParticipant.AGENT assert dialogues[0].utterances[0].get_intents() == [ Intent("DISCLOSE.NON-DISCLOSE") ] From f645db09b1f7955216f029c8657fd575078aeccd Mon Sep 17 00:00:00 2001 From: Nolwenn <28621493+NoB0@users.noreply.github.com> Date: Mon, 19 Aug 2024 13:06:47 +0200 Subject: [PATCH 2/2] Ignore mypy issue The typing issue raised is covered earlier in the method by checking if all utterances are AnnotatedUtterance --- dialoguekit/utils/dialogue_evaluation.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dialoguekit/utils/dialogue_evaluation.py b/dialoguekit/utils/dialogue_evaluation.py index 228946ff..a171bc89 100644 --- a/dialoguekit/utils/dialogue_evaluation.py +++ b/dialoguekit/utils/dialogue_evaluation.py @@ -155,7 +155,9 @@ def reward(self) -> Dict[str, List[Dict[str, float]]]: if utterance.participant == DialogueParticipant.AGENT: dialogue_utterances_start_agent: List[ AnnotatedUtterance - ] = dialogue.utterances[j:] + ] = dialogue.utterances[ + j: + ] # type: ignore[assignment] break previous_sender = dialogue_utterances_start_agent[0].participant previous_intents = dialogue_utterances_start_agent[0].get_intents()