Skip to content
This repository was archived by the owner on May 1, 2025. It is now read-only.

Commit 6b119c6

Browse files
author
Guangsen Wang
committed
unify NLU and NLG model names
1 parent d4aa1b0 commit 6b119c6

File tree

7 files changed

+33
-39
lines changed

7 files changed

+33
-39
lines changed

botsim/models/nlg/nlg_model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ModelNLG(NLG):
88
def __init__(self, model, *args):
99
self.model = model
1010

11-
def generate(self, *args):
12-
""" generate a natural language message
11+
def generate(self, dialog_state, *args):
12+
""" generate a natural language message given a semantic-level dialog state
1313
"""
14-
pass
14+
raise NotImplementedError

botsim/models/nlu/nlu_api.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88

99
class APIIntentPredictor(IntentDetector):
10-
def __init__(self, api_end_point, *args):
10+
def __init__(self, dialog_act_map, api_end_point, *args):
11+
super().__init__(dialog_act_map)
1112
self.api_end_point = api_end_point
12-
self.intents = None
1313

14-
def predict(self, user_message, *args):
15-
pass
14+
def predict(self, bot_message, dialog_name, *args):
15+
raise NotImplementedError

botsim/models/nlu/nlu_base.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
# SPDX-License-Identifier: BSD-3-Clause
44
# For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
55

6+
from botsim.botsim_utils.utils import load_intent_examples
67
class IntentDetector:
7-
def __init__(self, *args):
8-
self.intents = None
9-
10-
def predict(self, bot_message, *args):
8+
def __init__(self, dialog_act_map_path):
9+
self.intent_templates = None
10+
if dialog_act_map_path:
11+
dialog_act_maps = load_intent_examples(dialog_act_map_path)
12+
self.intent_templates = []
13+
for intent in dialog_act_maps:
14+
self.intent_templates.append({"intent": intent, "dialog_act_and_slot": dialog_act_maps[intent]})
15+
def predict(self, bot_message, dialog_name, *args):
1116
"""
12-
Predict the intent labels given the user message
17+
Predict the intent labels given the bot message via template-matching
1318
:param bot_message: bot message/prompt for the intent query
1419
"""
1520
raise NotImplementedError

botsim/models/nlu/nlu_fuzzy_match.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,15 @@
66
import re
77
from rapidfuzz import process, fuzz
88

9-
from botsim.botsim_utils.utils import load_intent_examples
109
from botsim.models.nlu.nlu_base import IntentDetector
1110

1211

1312
class FuzzyMatchIntentPredictor(IntentDetector):
14-
def __init__(self, dialog_act_map_path=None, *args):
15-
self.intents = None
16-
if dialog_act_map_path:
17-
dialog_act_maps = load_intent_examples(dialog_act_map_path)
18-
self.intents = []
19-
for intent in dialog_act_maps:
20-
self.intents.append({"intent": intent, "dialog_act_and_slot": dialog_act_maps[intent]})
13+
def __init__(self, dialog_act_map_path=None):
14+
super().__init__(dialog_act_map_path)
2115

22-
def predict(self, bot_message, intent_name):
23-
intents = self.intents
16+
def predict(self, bot_message, dialog_name):
17+
intents = self.intent_templates
2418
# remove variable names between $$
2519
regex = r"\$.*? "
2620
bot_message = re.sub(regex, "$", bot_message)
@@ -33,7 +27,7 @@ def predict(self, bot_message, intent_name):
3327
score_to_candidates = {}
3428

3529
for intent_index, intent_info in enumerate(intents):
36-
if intent_name.find(intent_info["intent"]) == -1:
30+
if dialog_name.find(intent_info["intent"]) == -1:
3731
continue
3832
dialog_act_and_slots = intent_info["dialog_act_and_slot"]
3933
res = []

botsim/models/nlu/nlu_model.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@
77

88

99
class ModelIntentPredictor(IntentDetector):
10-
def __init__(self, model_path, *args):
11-
self.model_path = model_path
12-
self.intents = None
13-
self.model = None
10+
def __init__(self, dialog_act_map_path, model_path, *args):
11+
super().__init__(dialog_act_map_path)
1412

15-
def predict(self, user_message, *args):
16-
if self.model:
17-
return self.model(user_message)
13+
def predict(self, bot_message, dialog_name):
14+
raise NotImplementedError

botsim/modules/simulator/abus.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,10 @@ def __init__(self, simulation_goals, simulation_configs):
3131
self.max_round = simulation_configs["simulator"]["run_time"]["max_round_num"]
3232
self.default_key = botsim_default_key
3333

34-
# self.intent = \
35-
self.intent_model = \
34+
self.nlu_model = \
3635
FuzzyMatchIntentPredictor( simulation_configs["generator"]["file_paths"]["revised_dialog_act_map"])
3736

38-
#FuzzyMatchIntentPredictor(simulation_configs["generator"]["file_paths"]["revised_dialog_act_map"])
39-
self.template_nlg = TemplateNLG(simulation_configs["generator"]["file_paths"]["response_template"])
37+
self.nlg_model = TemplateNLG(simulation_configs["generator"]["file_paths"]["response_template"])
4038

4139
# a stack for keeping track of BotSIM dialog turns. Each element includes
4240
# 1. user dialog acts

botsim/modules/simulator/user_simulator.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def generate_user_response(self, botsim_action):
4747
"""
4848
nl_message, semantic_message = "", ""
4949
if botsim_action["action"] != "fail":
50-
template_responses, slots_responses = self.template_nlg.generate(botsim_action, "user")
50+
template_responses, slots_responses = self.nlg_model.generate(botsim_action, "user")
5151
if isinstance(template_responses[0], list):
5252
for i, responses in enumerate(template_responses):
5353
j = random.choice(range(0, len(responses)))
@@ -341,7 +341,7 @@ def terminate_simulation_session_from_message(self,
341341
and not self.state["intent_succeed"]:
342342
self.state["request_slots"]["fall_back"] = "UNK"
343343
bot_message = " ".join(bot_response)
344-
best_matching_dialog_act, _, _, _ = self.intent_model.predict(bot_message, self.goal["name"])
344+
best_matching_dialog_act, _, _, _ = self.nlu_model.predict(bot_message, self.goal["name"])
345345
if best_matching_dialog_act == "":
346346
return {"to_discard": True}
347347
self.state["intent_error"] = (best_matching_dialog_act, self.state["user_response"], bot_message)
@@ -504,12 +504,12 @@ def enqueue_bot_actions_from_bot_messages(self,
504504
for bot_message in bot_api_response:
505505
# process one message in the bot api response list
506506
best_matching_dialog_act, best_matching_message, best_matching_score, matched_dialog_acts = \
507-
self.intent_model.predict(bot_message, self.goal["name"])
507+
self.nlu_model.predict(bot_message, self.goal["name"])
508508
if bot_action["round"] == self.intent_check_turn_index:
509509
# check for intent errors on the intent_check_turn
510-
for intent_index, task in enumerate(self.intent_model.intents):
510+
for intent_index, task in enumerate(self.nlu_model.intent_templates):
511511
if task["intent"] == self.goal["name"]: continue
512-
_, _, match_score, _ = self.intent_model.predict(bot_message, task["intent"])
512+
_, _, match_score, _ = self.nlu_model.predict(bot_message, task["intent"])
513513
if match_score > best_matching_score:
514514
self.state["request_slots"]["fall_back"] = "UNK"
515515
self.state["intent_error"] = \

0 commit comments

Comments
 (0)