<commit_before>from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from rasa_core.events import ActionExecuted, UserUttered, BotUttered, SlotSet
from rasa_core.actions.forms import FormAction, EntityFormField
from rasa_core.domain import TemplateDomain
from rasa_core.tracker_store import InMemoryTrackerStore
from rasa_core.channels.direct import CollectingOutputChannel
from rasa_core.dispatcher import Dispatcher


class ActionSearchRestaurants(FormAction):

    REQUIRED_FIELDS = [
        EntityFormField("cuisine", "cuisine"),
        EntityFormField("number", "people")
    ]
    RANDOMIZE = False

    def name(self):
        return 'action_search_restaurants'

    def submit(self, dispatcher, tracker, domain):
        pass


def test_restaurant_form():
    domain = TemplateDomain.load("data/test_domains/restaurant_form.yml")
    tracker_store = InMemoryTrackerStore(domain)
    out = CollectingOutputChannel()
    sender_id = "test-restaurant"
    dispatcher = Dispatcher(sender_id, out, domain)
    tracker = tracker_store.get_or_create_tracker(sender_id)

    # first user utterance
    tracker.update(UserUttered("", intent={"name": "inform"}))
    events = ActionSearchRestaurants().run(dispatcher, tracker, domain)
    assert len(events) == 1
    assert isinstance(events[0], SlotSet)
    assert events[0].key == "requested_slot"
    assert events[0].value == "cuisine"
    tracker.update(events[0])

    # second user utterance
    tracker.update(
        UserUttered("",
                    intent={"name": "inform"},
                    entities=[{"entity": "cuisine", "value": "chinese"}]))

    events = ActionSearchRestaurants().run(dispatcher, tracker, domain)
    assert len(events) == 2
    assert isinstance(events[0], SlotSet)
    assert isinstance(events[1], SlotSet)

    assert events[0].key == "cuisine"
    assert events[0].value == "chinese"

    assert events[1].key == "requested_slot"
    assert events[1].value == "people"
<commit_msg>add test for form with free text input<commit_after>from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from rasa_core.events import ActionExecuted, UserUttered, BotUttered, SlotSet
from rasa_core.actions.forms import FormAction, EntityFormField
from rasa_core.domain import TemplateDomain
from rasa_core.tracker_store import InMemoryTrackerStore
from rasa_core.channels.direct import CollectingOutputChannel
from rasa_core.dispatcher import Dispatcher


class ActionSearchRestaurants(FormAction):

    REQUIRED_FIELDS = [
        EntityFormField("cuisine", "cuisine"),
        EntityFormField("number", "people")
    ]
    RANDOMIZE = False

    def name(self):
        return 'action_search_restaurants'

    def submit(self, dispatcher, tracker, domain):
        pass


class ActionSearchPeople(FormAction):

    REQUIRED_FIELDS = [
	FreeTextFormField("person_name")
    ]

    def name(self):
        return 'action_search_people'

    def submit(self, dispatcher, tracker, domain):
        pass



def test_restaurant_form():
    domain = TemplateDomain.load("data/test_domains/restaurant_form.yml")
    tracker_store = InMemoryTrackerStore(domain)
    out = CollectingOutputChannel()
    sender_id = "test-restaurant"
    dispatcher = Dispatcher(sender_id, out, domain)
    tracker = tracker_store.get_or_create_tracker(sender_id)

    # first user utterance
    tracker.update(UserUttered("", intent={"name": "inform"}))
    events = ActionSearchRestaurants().run(dispatcher, tracker, domain)
    assert len(events) == 1
    assert isinstance(events[0], SlotSet)
    assert events[0].key == "requested_slot"
    assert events[0].value == "cuisine"
    tracker.update(events[0])

    # second user utterance
    tracker.update(
        UserUttered("",
                    intent={"name": "inform"},
                    entities=[{"entity": "cuisine", "value": "chinese"}]))

    events = ActionSearchRestaurants().run(dispatcher, tracker, domain)
    assert len(events) == 2
    assert isinstance(events[0], SlotSet)
    assert isinstance(events[1], SlotSet)

    assert events[0].key == "cuisine"
    assert events[0].value == "chinese"

    assert events[1].key == "requested_slot"
    assert events[1].value == "people"


def test_people_form():
    domain = TemplateDomain.load("data/test_domains/people_form.yml")
    tracker_store = InMemoryTrackerStore(domain)
    out = CollectingOutputChannel()
    sender_id = "test-people"
    dispatcher = Dispatcher(sender_id, out, domain)
    tracker = tracker_store.get_or_create_tracker(sender_id)

    # first user utterance
    tracker.update(UserUttered("", intent={"name": "inform"}))
    events = ActionSearchRestaurants().run(dispatcher, tracker, domain)
    assert len(events) == 1
    assert isinstance(events[0], SlotSet)
    assert events[0].key == "requested_slot"
    assert events[0].value == "person_name"
    tracker.update(events[0])

    # second user utterance
    name = "<NAME>"
    tracker.update(
        UserUttered(name,
                    intent={"name": "inform"}))

    events = ActionSearchRestaurants().run(dispatcher, tracker, domain)
    assert len(events) == 1
    assert isinstance(events[0], SlotSet)

    assert events[0].key == "person_name"
    assert events[0].value == name

{'fraction_non_alphanumeric': 0.10774039345467917, 'fraction_numerical': 0.006067291781577496, 'mean_word_length': 4.08411214953271, 'pattern_counts': {'":': 10, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7516010', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1600, 'n_words': 361}