def test_enter_natex(): df = DialogueFlow('root') transitions = { 'state': 'root', 'hello': { 'state': 'one', 'score': 2.0, 'enter': '#GATE', 'error': 'root' }, 'hi': { 'state': 'two', 'score': 1.0, 'enter': '#GATE', 'error': 'root' }, 'bye': { 'state': 'one', 'score': 1.0, }, 'goodbye': { 'state': 'three', 'score': 0.0, 'error': 'end' } } df.load_transitions(transitions) assert df.system_turn() == 'hello' df.user_turn('hello') assert df.system_turn() == 'hi' df.user_turn('hi') assert df.system_turn() == 'goodbye'
def test_virtual_transitions(): df = DialogueFlow("root") transitions = { "state": "root", "hello": { "state": "test", "a": { "something typical": "nope" }, "#VT(other)": "blah", }, "not taken": { "state": "other", "score": 0, "x": { "you win": { "state": "success" } }, "y": { "you win": { "state": "success" } }, }, } df.load_transitions(transitions) df.system_turn() assert df.state() == "test" df.user_turn("x", debugging=True) assert df.system_turn() == "you win" assert df.state() == "success"
def test_virtual_transitions(): df = DialogueFlow('root') transitions = { 'state': 'root', 'hello': { 'state': 'test', 'a': { 'something typical': 'nope' }, '#VT(other)': 'blah' }, 'not taken': { 'state': 'other', 'score': 0, 'x': { 'you win': { 'state': 'success' } }, 'y': { 'you win': { 'state': 'success' } } } } df.load_transitions(transitions) df.system_turn() assert df.state() == 'test' df.user_turn('x', debugging=True) assert df.system_turn() == 'you win' assert df.state() == 'success'
def test_gate_none(): df = DialogueFlow('root') transitions = { 'state': 'root', '#GATE(var:None) hello': { 'state': 'greet', 'score': 2.0, 'hi': { '#SET($var=True) how are ya': {'error': 'root'} }, 'error': { 'how are you': {'error': 'root'} } }, 'hi': 'greet' } df.load_transitions(transitions) assert df.system_turn().strip() == 'hello' df.user_turn('hi') assert df.system_turn().strip() == 'how are ya' df.user_turn('fine') assert df.system_turn().strip() == 'hi' df.user_turn('oh') assert df.system_turn().strip() == 'how are you'
def test_information_state_state_set(): df = DialogueFlow('root', initial_speaker=Speaker.USER) transitions = { 'state': 'root', 'error': { 'state': 'one', 'okay': { 'state': 'two', 'error': { 'state': 'three', 'sure': 'root' } } }, 'something': { 'state': 'special', 'that is great': 'root' } } df.add_update_rule('[{read, watched}]', '#TRANSITION(special, 2.0)') df.load_transitions(transitions, speaker=Speaker.USER) df.user_turn('hi') assert df.system_turn() == 'okay' df.user_turn('i read a book') assert df.state() == 'special' assert df.system_turn() == 'that is great' assert df.state() == 'root'
def test_transitions_to_transitions(): df = DialogueFlow("root", initial_speaker=Speaker.SYSTEM) transitions = { "state": "root", '"Hello World!"': {"state": "state_2", "error": {'"Hi,"': "root->state_2"}}, } df.load_transitions(transitions, speaker=Speaker.SYSTEM) assert df.system_turn() == "Hello World!" df.user_turn("blah") assert df.system_turn() == "Hi, Hello World!"
def test_global_transition_priority(): df = DialogueFlow("root") transitions = { "state": "root", "hello": { "[oh]": {"score": 0.6, "okay": {}}, "[wow]": {"score": 2.0, "alright": {}}, }, } df.add_global_nlu("root", "/.*/", score=0.7) df.load_transitions(transitions) df.system_turn() df.user_turn("oh wow") assert df.system_turn() == "alright"
def test_transitions_to_transitions(): df = DialogueFlow('root', initial_speaker=Speaker.SYSTEM) transitions = { 'state': 'root', '"Hello World!"': { 'state': 'state_2', 'error': { '"Hi,"': 'root->state_2' } } } df.load_transitions(transitions, speaker=Speaker.SYSTEM) assert df.system_turn() == 'Hello World!' df.user_turn('blah') assert df.system_turn() == 'Hi, Hello World!'
def test_enter_natex(): df = DialogueFlow("root") transitions = { "state": "root", "hello": {"state": "one", "score": 2.0, "enter": "#GATE", "error": "root"}, "hi": {"state": "two", "score": 1.0, "enter": "#GATE", "error": "root"}, "bye": { "state": "one", "score": 1.0, }, "goodbye": {"state": "three", "score": 0.0, "error": "end"}, } df.load_transitions(transitions) assert df.system_turn() == "hello" df.user_turn("hello") assert df.system_turn() == "hi" df.user_turn("hi") assert df.system_turn() == "goodbye"
def test_information_state_state_set(): df = DialogueFlow("root", initial_speaker=Speaker.USER) transitions = { "state": "root", "error": { "state": "one", "okay": {"state": "two", "error": {"state": "three", "sure": "root"}}, }, "something": {"state": "special", "that is great": "root"}, } df.add_update_rule("[{read, watched}]", "#TRANSITION(special, 2.0)") df.load_transitions(transitions, speaker=Speaker.USER) df.user_turn("hi") assert df.system_turn() == "okay" df.user_turn("i read a book") assert df.state() == "special" assert df.system_turn() == "that is great" assert df.state() == "root"
def test_gate_none(): df = DialogueFlow("root") transitions = { "state": "root", "#GATE(var:None) hello": { "state": "greet", "score": 2.0, "hi": {"#SET($var=True) how are ya": {"error": "root"}}, "error": {"how are you": {"error": "root"}}, }, "hi": "greet", } df.load_transitions(transitions) assert df.system_turn().strip() == "hello" df.user_turn("hi") assert df.system_turn().strip() == "how are ya" df.user_turn("fine") assert df.system_turn().strip() == "hi" df.user_turn("oh") assert df.system_turn().strip() == "how are you"
def test_global_transition_priority(): df = DialogueFlow('root') transitions = { 'state': 'root', 'hello':{ '[oh]':{ 'score': 0.6, 'okay':{} }, '[wow]':{ 'score': 2.0, 'alright': {} } } } df.add_global_nlu('root', '/.*/', score=0.7) df.load_transitions(transitions) df.system_turn() df.user_turn('oh wow') assert df.system_turn() == 'alright'
def test_virtual_transitions_cross_module(): df = DialogueFlow("root") transitions = { "state": "root", "hello": { "state": "test", "a": {"something typical"}, "#VT(two:other)": "blah", }, } df.load_transitions(transitions) df2 = DialogueFlow("r2") transitions2 = { "not taken": { "state": "other", "score": 0, "x": { "you win": { "state": "success" } }, "y": { "you win": { "state": "success" } }, } } df2.load_transitions(transitions2) cdf = CompositeDialogueFlow("one:root", "e", "e") cdf.add_component(df, "one") cdf.add_component(df2, "two") cdf.set_state("one:root") cdf.system_turn() assert cdf.state() == ("one", "test") cdf.user_turn("x", debugging=True) assert cdf.system_turn() == "you win" assert cdf.state() == ("two", "success")
def test_virtual_transitions_cross_module(): df = DialogueFlow('root') transitions = { 'state': 'root', 'hello': { 'state': 'test', 'a': {'something typical'}, '#VT(two:other)': 'blah' } } df.load_transitions(transitions) df2 = DialogueFlow('r2') transitions2 = { 'not taken': { 'state': 'other', 'score': 0, 'x': { 'you win': { 'state': 'success' } }, 'y': { 'you win': { 'state': 'success' } } } } df2.load_transitions(transitions2) cdf = CompositeDialogueFlow('one:root', 'e', 'e') cdf.add_component(df, 'one') cdf.add_component(df2, 'two') cdf.set_state('one:root') cdf.system_turn() assert cdf.state() == ('one', 'test') cdf.user_turn('x', debugging=True) assert cdf.system_turn() == 'you win' assert cdf.state() == ('two', 'success')
def test_stack(): df = DialogueFlow("root") transitions = { "state": "root", "`What should I do?`": "gma_hospital", "#GOAL(grandma_hospital) #GATE" "`I cannot concentrate on a lot of things right now. " "My grandma is in the hospital. " "What should I do?`": { "state": "gma_hospital", "score": 2.0, "[{[{dont,shouldnt,not},worry],calm,relax,distract,[mind,off]}]": { "state": "dont_worry", "#GATE " "\"Okay, I will try my best not to worry. It's hard because she lives by " "herself and won't let anyone help her very much, so I feel like this " 'will just happen again."': { "state": "happen_again", "[{sorry,sucks,hard}]": { '"yeah, thanks."': {} }, "[will,better]": { '"I really hope you are right."': {} }, "error": { "state": "feel_better", '"I actually feel a little bit better after talking with you. Thanks for listening. "': {}, }, }, "#DEFAULT": "feel_better", }, "#IDK": { '"yeah, i dont know either. Its so tough."': {} }, }, } gma = { "#GOAL(why_grandma_hospital) " "[{[why,hospital],wrong,happened}]": { "state": "why_grandma_hospital", '"She is just so frail. I can hardly believe it. ' 'She fell off of a stool in the kitchen and broke her hip."': { "[dont worry]": {"#GRET(grandma_hospital,dont_worry)"}, "error": { "#GRET": "return" }, }, }, "#GOAL(grandma_hospital_before) " "[{has,was},{she,grandma},hospital,{before,earlier,previously}]": { "state": "grandma_hospital_before", '"No, this is the first time, thank goodness."': { "error": { "#GRET": "return" } }, }, } df.load_transitions(transitions) df.load_transitions(gma, speaker=DialogueFlow.Speaker.USER) df.add_global_nlu( "why_grandma_hospital", "#GOAL(why_grandma_hospital) [{[why,hospital],wrong,happened}]", score=0.7, ) df.add_global_nlu( "grandma_hospital_before", "#GOAL(grandma_hospital_before) [{has,was},{she,grandma},hospital,{before,earlier,previously}]", score=0.7, ) # r = df.system_turn() df.system_turn() assert df.vars()["__goal__"] == "grandma_hospital" assert len(df.vars()["__stack__"]) == 0 assert df.state() == "gma_hospital" df.user_turn("what happened") assert df.state() == "why_grandma_hospital" assert df.vars()["__goal_return_state__"] == "None" assert df.vars()["__goal__"] == "why_grandma_hospital" assert len(df.vars()["__stack__"]) == 1 assert df.vars()["__stack__"][0][0] == "grandma_hospital" assert "fell off of a stool" in df.system_turn() df.user_turn("oh no", debugging=True) assert "What should I do" in df.system_turn(debugging=True) assert df.state() == "gma_hospital" assert df.vars()["__goal__"] == "grandma_hospital" assert len(df.vars()["__stack__"]) == 0 df.user_turn("dont worry") assert df.state() == "dont_worry" assert "she lives by herself" in df.system_turn() df.user_turn("has your grandma been in the hospital before this") assert df.state() == "grandma_hospital_before" assert df.vars()["__goal__"] == "grandma_hospital_before" assert len(df.vars()["__stack__"]) == 1 assert df.vars()["__stack__"][0][0] == "grandma_hospital" assert "this is the first time" in df.system_turn() df.user_turn("ok that is good") assert "feel a little bit better" in df.system_turn() assert df.vars()["__goal__"] == "grandma_hospital" assert len(df.vars()["__stack__"]) == 0
def test_stack(): df = DialogueFlow('root') transitions = { 'state': 'root', '`What should I do?`': 'gma_hospital', '#GOAL(grandma_hospital) #GATE' '`I cannot concentrate on a lot of things right now. ' 'My grandma is in the hospital. ' 'What should I do?`': { 'state': 'gma_hospital', 'score': 2.0, '[{[{dont,shouldnt,not},worry],calm,relax,distract,[mind,off]}]': { 'state': 'dont_worry', '#GATE ' '"Okay, I will try my best not to worry. It\'s hard because she lives by ' 'herself and won\'t let anyone help her very much, so I feel like this ' 'will just happen again."': { 'state': 'happen_again', '[{sorry,sucks,hard}]': { '"yeah, thanks."': {} }, '[will,better]': { '"I really hope you are right."': {} }, 'error': { 'state': 'feel_better', '"I actually feel a little bit better after talking with you. Thanks for listening. "': {} } }, '#DEFAULT': 'feel_better' }, '#IDK': { '"yeah, i dont know either. Its so tough."': {} } } } gma = { '#GOAL(why_grandma_hospital) ' '[{[why,hospital],wrong,happened}]': { 'state': 'why_grandma_hospital', '"She is just so frail. I can hardly believe it. ' 'She fell off of a stool in the kitchen and broke her hip."': { '[dont worry]': {'#GRET(grandma_hospital,dont_worry)'}, 'error': { '#GRET': 'return' } } }, '#GOAL(grandma_hospital_before) ' '[{has,was},{she,grandma},hospital,{before,earlier,previously}]': { 'state': 'grandma_hospital_before', '"No, this is the first time, thank goodness."': { 'error': { '#GRET': 'return' } } } } df.load_transitions(transitions) df.load_transitions(gma, speaker=DialogueFlow.Speaker.USER) df.add_global_nlu( 'why_grandma_hospital', '#GOAL(why_grandma_hospital) [{[why,hospital],wrong,happened}]', score=0.7) df.add_global_nlu( 'grandma_hospital_before', '#GOAL(grandma_hospital_before) [{has,was},{she,grandma},hospital,{before,earlier,previously}]', score=0.7) r = df.system_turn() assert df.vars()['__goal__'] == 'grandma_hospital' assert len(df.vars()['__stack__']) == 0 assert df.state() == 'gma_hospital' df.user_turn("what happened") assert df.state() == "why_grandma_hospital" assert df.vars()['__goal_return_state__'] == 'None' assert df.vars()['__goal__'] == 'why_grandma_hospital' assert len(df.vars()['__stack__']) == 1 assert df.vars()['__stack__'][0][0] == 'grandma_hospital' assert "fell off of a stool" in df.system_turn() df.user_turn("oh no", debugging=True) assert "What should I do" in df.system_turn(debugging=True) assert df.state() == 'gma_hospital' assert df.vars()['__goal__'] == 'grandma_hospital' assert len(df.vars()['__stack__']) == 0 df.user_turn("dont worry") assert df.state() == 'dont_worry' assert "she lives by herself" in df.system_turn() df.user_turn("has your grandma been in the hospital before this") assert df.state() == 'grandma_hospital_before' assert df.vars()['__goal__'] == 'grandma_hospital_before' assert len(df.vars()['__stack__']) == 1 assert df.vars()['__stack__'][0][0] == 'grandma_hospital' assert "this is the first time" in df.system_turn() df.user_turn("ok that is good") assert "feel a little bit better" in df.system_turn() assert df.vars()['__goal__'] == 'grandma_hospital' assert len(df.vars()['__stack__']) == 0