Exemple #1
0
def test_form():
    dm = CheckableFormFiller('tests/test_managers/form.yaml',
                             default_message=DEFAULT_MESSAGE)
    resp = dm.respond(make_context(new_session=True))
    assert resp.text == DEFAULT_MESSAGE

    for q, a in [
        ('start the test', 'Please tell me your name'),
        ('Bob',
         'Now tell me the year of your birth. Four digits, nothing more.'),
        ('not', 'Please try again. Your answer should be 4 digits.'),
        ('0', 'Please try again. Your answer should be 4 digits.'),
        ('1999',
         'Wonderful! Now choose the month of your birth (the first 3 letters).'
         ),
        ('lol',
         'The answer should be one of the suggested options - the first 3 letters of a month.'
         ),
        ('jan',
         'That\'s great! Finally, tell me the date of your birth - one or two digits'
         ),
        ('40',
         'Please try again. Your answer should be a whole number - the day of your birth.'
         ),
        ('02',
         'Thank you, Bob! Now we know: you are 20 years old and you are probably The Goat. Lucky you!'
         ), ('Okay, what\'s next?', DEFAULT_MESSAGE),
        ('But really', DEFAULT_MESSAGE)
    ]:
        resp = dm.respond(make_context(text=q, prev_response=resp))
        assert resp.text == a, 'expected "{}" to be responded by "{}", got "{}" instead'.format(
            q, a, resp.text)
Exemple #2
0
def test_default_transition(example_fsa):
    fsa: AutomatonDialogManager = example_fsa

    ctx0 = make_context(text='hi', new_session=True)
    resp0 = fsa.try_to_respond(ctx0)

    ctx1 = make_context('let\'s introduce ourselves', prev_response=resp0)
    resp1 = fsa.try_to_respond(ctx1)

    ctx2 = make_context('Stasy', prev_response=resp1)
    resp2 = fsa.try_to_respond(ctx2)
    assert resp2.text == 'Nice to meet you'
Exemple #3
0
def test_dm():
    manager = make_dm()
    ctx = make_context(new_session=True, text='шалом')
    ctx.source = 'text'
    resp = manager.respond(ctx)
    assert resp
    assert 'выход' in resp.suggests
    assert 'Привет' in resp.text
    assert 'Синоним' in resp.text

    c2 = make_context(prev_response=resp, text='собака')
    r2 = manager.respond(c2)
    assert 'собака' in r2.text
Exemple #4
0
def test_make_dm():
    dm = make_dm()
    ctx = make_context(text='начать', new_session=True)
    resp = dm.respond(ctx)
    assert resp
    assert 'бот' in resp.text
    assert 'начать' in resp.text
Exemple #5
0
def test_faq():
    dm = dialogic.dialog_manager.FAQDialogManager(
        'tests/test_managers/faq.yaml',
        matcher='cosine',
        default_message=DEFAULT_MESSAGE
    )
    r1 = dm.respond(make_context(new_session=True))
    assert r1.text == DEFAULT_MESSAGE
    first_responses = {dm.respond(make_context(text='hi there', prev_response=r1)).text for i in range(30)}
    assert first_responses == {'Hello!', 'Nice to meet you!', 'Have a good time, human.'}

    r2 = dm.respond(make_context(text='hi there', prev_response=r1))
    assert set(r2.suggests) == {'How are you?', 'What can you do?'}

    r3 = dm.respond(make_context(text='how are you', prev_response=r2))
    assert r3.text == "I'm fine, thanks"

    r4 = dm.respond(make_context(text='What can you do?', prev_response=r3))
    assert r4.text == DEFAULT_MESSAGE
Exemple #6
0
def test_minimal_fsa():
    cfg = {
        'states': {
            'first': {'a': 'hello'}
        }
    }
    fsa = AutomatonDialogManager(config=cfg, matcher='exact')
    assert len(fsa.states) == 2  # 'universal' state is always added but it never reached

    ctx = make_context(text='hi', new_session=True)
    resp = fsa.try_to_respond(ctx)
    assert resp.text == 'hello'
Exemple #7
0
def test_turn_dm():
    csc = Cascade()

    @csc.add_handler(priority=0)
    def fallback(turn: DialogTurn):
        turn.response_text = 'hi'

    @csc.add_handler(priority=1, intents=['shalom'])
    def fallback(turn: DialogTurn):
        turn.response_text = 'shalom my friend'

    dm = TurnDialogManager(cascade=csc,
                           intents_file='tests/test_managers/intents.yaml')
    ctx = make_context('shalom')
    resp = dm.respond(ctx)
    assert resp.text == 'shalom my friend'
Exemple #8
0
def test_postprocess():
    turn = DialogTurn(make_context(text='kek'), text='kek')
    turn.response_text = 'The weather is cool.'
    # without agenda, no postprocessors are called
    turn.release_control()
    csc.postprocess(turn)
    assert turn.response_text.endswith('cool.')
    # without control, no postprocessors are called
    turn.take_control()
    turn.add_agenda('ask_for_tea')
    csc.postprocess(turn)
    assert turn.response_text.endswith('cool.')
    # with control and agenda, postprocessors are called
    turn.release_control()
    csc.postprocess(turn)
    assert turn.response_text.endswith('tea?')
    # after postprocessing, agenda goes away
    assert not turn.agenda
Exemple #9
0
def test_basic_transition(example_fsa):
    fsa: AutomatonDialogManager = example_fsa

    # initialize
    ctx0 = make_context(text='hi', new_session=True)
    resp0 = fsa.try_to_respond(ctx0)
    assert resp0.text == 'hello'

    # successful transition
    ctx1 = make_context(text='can you tell me the time please', prev_response=resp0)
    resp1 = fsa.try_to_respond(ctx1)
    assert resp1.text == '8 pm'

    # the same transition from another state is not allowed
    ctx2 = make_context(text='can you tell me the time please', prev_response=resp1)
    resp2 = fsa.try_to_respond(ctx2)
    assert not resp2

    # failed transition: text was not matched
    ctx1 = make_context(text='you will not understand me', prev_response=resp0)
    resp1 = fsa.try_to_respond(ctx1)
    assert not resp1

    # transition from the universal state
    ctx1 = make_context(text='help', prev_response=resp0)
    resp1 = fsa.try_to_respond(ctx1)
    assert resp1.text == 'I am always here to help'

    # new session
    ctx2 = make_context(new_session=True, prev_response=resp1)
    resp2 = fsa.try_to_respond(ctx2)
    assert resp2.text == 'hello again'

    # after transient state, context is restored and previous transition is possible
    ctx3 = make_context(prev_response=resp2, text='thanks')
    resp3 = fsa.try_to_respond(ctx3)
    assert resp3.text == 'thank you'
    ctx4 = make_context(prev_response=resp3, text='tell me time now')
    resp4 = fsa.try_to_respond(ctx4)
    assert resp4.text == '8 pm'
Exemple #10
0
def test_ranking(intents, result):
    turn = DialogTurn(make_context(text='kek'), text='kek', intents=intents)
    assert csc(turn) == result