예제 #1
0
def test_reset():
    df = DialogueFlow(States.A, initial_speaker=Speaker.SYSTEM)
    df.add_state(States.B, error_successor=States.E)
    df.add_state(States.C, error_successor=States.E)
    df.add_system_transition(States.A, States.B, 'B')
    df.add_system_transition(States.A, States.C, 'C')
    df.add_system_transition(States.E, States.E, 'E')

    response = df.system_turn()
    df.user_turn("")
    assert df.state() == States.E

    df.reset()
    assert df.state() == 'States.A'
    assert df.speaker() == Speaker.SYSTEM
    response = df.system_turn()
    df.user_turn("")
    assert df.state() == States.E
    df.system_turn()
    assert df.state() == States.E

    df.reset()
    assert df.state() == States.A
    assert df.speaker() == Speaker.SYSTEM
    response = df.system_turn()
    df.user_turn("")
    assert df.state() == States.E
    df.system_turn()
    assert df.state() == States.E
예제 #2
0
def test_reset():
    df = DialogueFlow(States.A, initial_speaker=Speaker.SYSTEM)
    df.add_state(States.B, error_successor=States.E)
    df.add_state(States.C, error_successor=States.E)
    df.add_system_transition(States.A, States.B, "B")
    df.add_system_transition(States.A, States.C, "C")
    df.add_system_transition(States.E, States.E, "E")

    # response = df.system_turn()
    df.system_turn()
    df.user_turn("")
    assert df.state() == States.E

    df.reset()
    assert df.state() == "States.A"
    assert df.speaker() == Speaker.SYSTEM
    # response = df.system_turn()
    df.system_turn()
    df.user_turn("")
    assert df.state() == States.E
    df.system_turn()
    assert df.state() == States.E

    df.reset()
    assert df.state() == States.A
    assert df.speaker() == Speaker.SYSTEM
    # response = df.system_turn()
    df.system_turn()
    df.user_turn("")
    assert df.state() == States.E
    df.system_turn()
    assert df.state() == States.E
예제 #3
0
def test_user_multi_hop():
    df = DialogueFlow(States.A, initial_speaker=Speaker.USER)
    df.add_state(States.B, error_successor=States.B, user_multi_hop=True)
    df.add_state(States.C, error_successor=States.A, user_multi_hop=True)
    df.add_state(States.D, error_successor=States.A)
    df.add_state(States.E, error_successor=States.A)
    df.add_user_transition(States.A, States.B, "[{hey, hello}]")
    df.add_user_transition(States.A, States.C, "[{excuse, pardon}]")
    df.add_user_transition(States.B, States.D, "[how, you]")
    df.add_user_transition(States.C, States.E, "[{where, how, what}]")
    df.set_state(States.A)
    df.user_turn("hey", debugging=False)
    assert df.state() == States.B
    df.set_state(States.A)
    df.set_speaker(Speaker.USER)
    df.user_turn("hey how are you")
    assert df.state() == States.D
    df.set_state(States.A)
    df.set_speaker(Speaker.USER)
    df.user_turn("excuse me where do i go")
    assert df.state() == States.E
    df.set_state(States.A)
    df.set_speaker(Speaker.USER)
    df.user_turn("excuse me")
    assert df.state() == States.A
예제 #4
0
def test_global_transition_list_disjunction():
    df = DialogueFlow("root", initial_speaker=Speaker.USER)
    nlu = ["hi", "hello"]
    df.add_state("root", "x")
    df.add_system_transition("x", "y", "hello")
    df.add_global_nlu("x", nlu)
    df.user_turn("ok")
    df.system_turn()
    df.user_turn("hi")
    assert df.state() == "x"
예제 #5
0
def test_global_transition_list_disjunction():
    df = DialogueFlow('root', initial_speaker=Speaker.USER)
    nlu = [
        'hi',
        'hello'
    ]
    df.add_state('root', 'x')
    df.add_system_transition('x', 'y', 'hello')
    df.add_global_nlu('x', nlu)
    df.user_turn('ok')
    df.system_turn()
    df.user_turn('hi')
    assert df.state() == 'x'
예제 #6
0
def test_system_multi_hop():
    df = DialogueFlow(States.A, initial_speaker=Speaker.SYSTEM)
    df.add_state(States.B, error_successor=States.B, system_multi_hop=True)
    df.add_state(States.C, error_successor=States.A, system_multi_hop=True)
    df.add_state(States.D, error_successor=States.A)
    df.add_state(States.E, error_successor=States.A)
    df.add_system_transition(States.A, States.B, "{hey, hello}")
    df.add_system_transition(States.A, States.C, "excuse me")
    df.add_system_transition(States.B, States.D, "how are you")
    df.add_system_transition(States.C, States.E, "what")
    for _ in range(100):
        df.set_state(States.A)
        df.set_speaker(Speaker.SYSTEM)
        response = df.system_turn()
        assert response in {"hey how are you", "hello how are you", "excuse me what"}
예제 #7
0
def test_system_multi_hop():
    df = DialogueFlow(States.A, initial_speaker=Speaker.SYSTEM)
    df.add_state(States.B, error_successor=States.B, system_multi_hop=True)
    df.add_state(States.C, error_successor=States.A, system_multi_hop=True)
    df.add_state(States.D, error_successor=States.A)
    df.add_state(States.E, error_successor=States.A)
    df.add_system_transition(States.A, States.B, '{hey, hello}')
    df.add_system_transition(States.A, States.C, 'excuse me')
    df.add_system_transition(States.B, States.D, 'how are you')
    df.add_system_transition(States.C, States.E, 'what')
    for _ in range(100):
        df.set_state(States.A)
        df.set_speaker(Speaker.SYSTEM)
        response = df.system_turn()
        assert response in {'hey how are you', 'hello how are you',
                            'excuse me what'}
예제 #8
0
def test_serialization():
    df = DialogueFlow(States.A, initial_speaker=Speaker.SYSTEM)
    df.add_state(States.B, error_successor=States.E)
    df.add_state(States.C, error_successor=States.E)
    df.add_system_transition(States.A, States.B, "#GATE $spoken=B")
    df.add_system_transition(States.A, States.C, "#GATE(spoken) $spoken=C")
    df.add_system_transition(States.E, States.E, "$spoken=E")
    df.add_user_transition(States.B, States.A, "$heard=b")
    df.add_user_transition(States.E, States.A, "$heard=e")

    df.system_turn()
    assert df.state() == States.B
    assert df.vars()["spoken"] == "B"
    df.user_turn("b")
    df.system_turn()
    assert df.state() == States.C
    assert df.vars()["spoken"] == "C"
    expected_gates = {"States.B": [{}], "States.C": [{"spoken": "B"}]}
    assert df.gates() == expected_gates
    df.vars()["testing_none"] = None
    d = df.serialize()

    df2 = DialogueFlow(States.A, initial_speaker=Speaker.SYSTEM)
    df2.add_state(States.B, error_successor=States.E)
    df2.add_state(States.C, error_successor=States.E)
    df2.add_system_transition(States.A, States.B, "#GATE $spoken=B")
    df2.add_system_transition(States.A, States.C, "#GATE(spoken) $spoken=C")
    df2.add_system_transition(States.E, States.E, "$spoken=E")
    df2.add_user_transition(States.B, States.A, "$heard=b")
    df2.add_user_transition(States.E, States.A, "$heard=e")
    assert df2.state() == States.A
    assert "spoken" not in df2.vars() and "heard" not in df2.vars()
    assert len(df2.gates()) == 0

    df2.deserialize(d)

    assert df.vars() == df2.vars()
    assert df.gates() == df2.gates()
    assert df.state() == df2.state()
    assert df2.vars()["testing_none"] is None
예제 #9
0
def test_serialization():
    df = DialogueFlow(States.A, initial_speaker=Speaker.SYSTEM)
    df.add_state(States.B, error_successor=States.E)
    df.add_state(States.C, error_successor=States.E)
    df.add_system_transition(States.A, States.B, '#GATE $spoken=B')
    df.add_system_transition(States.A, States.C, '#GATE(spoken) $spoken=C')
    df.add_system_transition(States.E, States.E, '$spoken=E')
    df.add_user_transition(States.B, States.A, '$heard=b')
    df.add_user_transition(States.E, States.A, '$heard=e')

    df.system_turn()
    assert df.state() == States.B
    assert df.vars()["spoken"] == "B"
    df.user_turn('b')
    df.system_turn()
    assert df.state() == States.C
    assert df.vars()["spoken"] == "C"
    expected_gates = {'States.B': [{}], 'States.C': [{'spoken': 'B'}]}
    assert df.gates() == expected_gates
    df.vars()["testing_none"] = None
    d = df.serialize()

    df2 = DialogueFlow(States.A, initial_speaker=Speaker.SYSTEM)
    df2.add_state(States.B, error_successor=States.E)
    df2.add_state(States.C, error_successor=States.E)
    df2.add_system_transition(States.A, States.B, '#GATE $spoken=B')
    df2.add_system_transition(States.A, States.C, '#GATE(spoken) $spoken=C')
    df2.add_system_transition(States.E, States.E, '$spoken=E')
    df2.add_user_transition(States.B, States.A, '$heard=b')
    df2.add_user_transition(States.E, States.A, '$heard=e')
    assert df2.state() == States.A
    assert 'spoken' not in df2.vars() and 'heard' not in df2.vars()
    assert len(df2.gates()) == 0

    df2.deserialize(d)

    assert df.vars() == df2.vars()
    assert df.gates() == df2.gates()
    assert df.state() == df2.state()
    assert df2.vars()["testing_none"] is None
예제 #10
0
def test_ontology():
    kb = KnowledgeBase()
    ontology = {
        "ontology": {
            "season": [
                "fall",
                "spring",
                "summer",
                "winter"
            ],
            "month": [
                "january",
                "february",
                "march",
                "april",
                "may",
                "june",
                "july",
                "august",
                "september",
                "october",
                "november",
                "december"
            ]
        }
    }
    kb.load_json(ontology)
    df = DialogueFlow(States.A, Speaker.USER, kb=kb)
    df.add_state(States.A)
    df.add_state(States.B)
    df.add_state(States.C)
    df.add_state(States.D)
    df.add_state(States.E)
    df.set_error_successor(States.A, States.E)
    df.set_error_successor(States.B, States.E)
    df.set_error_successor(States.C, States.E)
    df.set_error_successor(States.D, States.E)
    df.add_user_transition(States.A, States.B, "[#ONT(month)]")
    df.add_system_transition(States.B, States.C, "B to C")
    df.add_user_transition(States.C, States.D, "[$m=#ONT(month), $s=#ONT(season)]")

    df.user_turn("january")
    assert df.state() == States.B
    assert df.system_turn() == "B to C"
    df.user_turn("october is in the fall season")
    assert df.state() == States.D
    assert df._vars["m"] == "october"
    assert df._vars["s"] == "fall"

    df.set_state(States.A)
    df.set_speaker(Speaker.USER)
    df.user_turn("hello there", debugging=False)
    assert df.state() == States.E
예제 #11
0
def test_check():
    df = DialogueFlow(States.A)
    df.add_state(States.B, error_successor=States.C)
    df.add_state(States.D, error_successor=States.A)
    df.add_system_transition(States.A, States.B, "hello")
    df.add_user_transition(States.B, States.C, "[{hi, hello, hey, [how, you]}]")
    df.add_user_transition(States.B, States.A, "[{bye, goodbye, see you, see ya, later}]")
    df.add_system_transition(States.C, States.D, "ok")
    assert df.check()
    df = DialogueFlow(States.A)
    df.add_state(States.B, error_successor=States.C)
    df.add_system_transition(States.A, States.B, "hello")
    df.add_user_transition(States.B, States.C, "[{hi, hello, hey, [how, you]}]")
    df.add_user_transition(States.B, States.A, "[{bye, goodbye, see you, see ya, later}]")
    df.add_system_transition(States.C, States.D, "ok")
    assert not df.check()
    df = DialogueFlow(States.A)
    df.add_state(States.B, error_successor=States.C)
    df.add_state(States.D, error_successor=States.A)
    df.add_system_transition(States.A, States.B, "hello")
    df.add_user_transition(States.B, States.C, "[{hi, hello, hey, [how, you]}]")
    df.add_user_transition(States.B, States.A, "[{bye, goodbye, see you, see ya, later}]")
    df.add_system_transition(States.C, States.D, "ok then $X alright")
    assert not df.check()
예제 #12
0
    ("growl", "quality", "scary"),
    ("also", "type", "also_syns"),
    ("too", "type", "also_syns"),
    ("basketball", "type", "unknown_hobby"),
    ("basketball", "expr", "bball"),
    ("john", "type", "male"),
    ("mary", "type", "female"),
    ("male", "type", "person"),
    ("female", "type", "person"),
    ("friend", "type", "person"),
])
ont = {"ontology": {"person": ["mom", "dad", "sister", "brother"]}}
kb.load_json(ont)

df = DialogueFlow(initial_state="start")
df.add_state("start", "start")
df.add_system_transition("start", "start", "hello")

macros = {
    # "ONT": ONTE(kb),
    # "KBQ": KBE(kb),
    # "EXP": EXP(kb),
    # "U": UnionMacro(),
    # "I": Intersection(),
    # "DIF": Difference(),
    # "SET": SetVars(),
    # "ALL": CheckVarsConjunction(),
    # "ANY": CheckVarsDisjunction(),
    # "NOT": NOT(),
    # "ISP": IsPlural(),
    # "FPP": FirstPersonPronoun(kb),
예제 #13
0
kb = KnowledgeBase([
    ('lion', 'type', 'cat'), ('panther', 'type', 'cat'),
    ('cat', 'type', 'animal'), ('animal', 'type', 'thing'),
    ('lion', 'sound', 'roar'), ('roar', 'quality', 'loud'),
    ('panther', 'sound', 'growl'), ('growl', 'quality', 'scary'),
    ('also', 'type', 'also_syns'), ('too', 'type', 'also_syns'),
    ('basketball', 'type', 'unknown_hobby'), ('basketball', 'expr', 'bball'),
    ('john', 'type', 'male'), ('mary', 'type', 'female'),
    ('male', 'type', 'person'), ('female', 'type', 'person'),
    ('friend', 'type', 'person')
])
ont = {"ontology": {"person": ["mom", "dad", "sister", "brother"]}}
kb.load_json(ont)

df = DialogueFlow(initial_state='start')
df.add_state('start', 'start')
df.add_system_transition('start', 'start', 'hello')

macros = {
    'ONT': ONTE(kb),
    'KBQ': KBE(kb),
    'EXP': EXP(kb),
    'U': UnionMacro(),
    'I': Intersection(),
    'DIF': Difference(),
    'SET': SetVars(),
    'ALL': CheckVarsConjunction(),
    'ANY': CheckVarsDisjunction(),
    'NOT': NOT(),
    'ISP': IsPlural(),
    'FPP': FirstPersonPronoun(kb),