Ejemplo n.º 1
0
 def __init__(self, sid):
     self.sid = sid
     self.sdata = SessionData()
     self.cache = ResponseCache()
     self.created = dt.datetime.now()
     self.init = self.created
     self.characters = []
Ejemplo n.º 2
0
def post_response(body):  # noqa: E501
    """Annotate a response

     # noqa: E501

    :param body: Object that provides annotation information
    :type body: 

    :rtype: object
    """
    response_cache = ResponseCache()
    response_cache.store_callback(body)
    return 'received!'
Ejemplo n.º 3
0
def _ask_characters(characters, question, lang, session):
    global response_caches
    chat_tries = 0
    if session not in response_caches:
        response_caches[session] = ResponseCache()
    cache = response_caches.get(session)

    weights = [c.weight for c in characters]
    _question = question.lower().strip()
    _question = ' '.join(_question.split())  # remove consecutive spaces
    num_tier = len(characters)
    while chat_tries < MAX_CHAT_TRIES:
        chat_tries += 1
        _responses = [c.respond(_question, lang, session) for c in characters]
        for r in _responses:
            assert isinstance(r, dict), "Response must be a dict"
        answers = [r.get('text', '') for r in _responses]

        # Each tier has weight*100% chance to be selected.
        # If the chance goes to the last tier, it will be selected anyway.
        for idx, answer in enumerate(answers):
            if not answer:
                continue
            if random.random() < weights[idx]:
                if cache.check(_question, answer):
                    cache.add(_question, answer)
                    return _responses[idx]

    c = get_character('sophia_pickup')
    if c is not None:
        chat_tries = 0
        while chat_tries < MAX_CHAT_TRIES:
            chat_tries += 1
            if random.random() > 0.7:
                _response = c.respond('early random pickup', lang, session)
                _response['state'] = 'early random pickup'
            else:
                _response = c.respond('mid random pickup', lang, session)
                _response['state'] = 'mid random pickup'
            answer = _response.get('text', '')
            if cache.check(_question, answer):
                cache.add(_question, answer)
                return _response

    _response = {}
    answer = "Sorry, I can't answer that"
    _response['text'] = answer
    _response['botid'] = "dummy"
    _response['botname'] = "dummy"
    cache.add(_question, answer)
    return _response
Ejemplo n.º 4
0
def get_response(response_id):  # noqa: E501
    """Request a previously stored response from the server

     # noqa: E501

    :param response_id: Identifier of the response to return
    :type response_id: str

    :rtype: Response
    """

    response_cache = ResponseCache()
    envelope = response_cache.get_response(response_id)
    return envelope
Ejemplo n.º 5
0
 def __init__(self, sid):
     self.sid = sid
     self.sdata = SessionData()
     self.cache = ResponseCache()
     self.created = dt.datetime.now()
     self.init = self.created
     self.characters = []
     dirname = os.path.join(HISTORY_DIR, self.created.strftime('%Y%m%d'))
     test_dirname = os.path.join(TEST_HISTORY_DIR, self.created.strftime('%Y%m%d'))
     self.fname = os.path.join(dirname, '{}.csv'.format(self.sid))
     self.test_fname = os.path.join(test_dirname, '{}.csv'.format(self.sid))
     self.dump_file = None
     self.removed = False
     self.active = False
     self.last_active_time = None
     self.test = False
Ejemplo n.º 6
0
 def __init__(self, sid):
     self.sid = sid
     self.session_context = SessionContext()
     self.cache = ResponseCache()
     self.created = dt.datetime.utcnow()
     self.characters = []
     dirname = os.path.join(HISTORY_DIR, self.created.strftime('%Y%m%d'))
     test_dirname = os.path.join(
         TEST_HISTORY_DIR, self.created.strftime('%Y%m%d'))
     self.fname = os.path.join(dirname, '{}.csv'.format(self.sid))
     self.test_fname = os.path.join(test_dirname, '{}.csv'.format(self.sid))
     self.dump_file = None
     self.closed = False
     self.active = False
     self.last_active_time = None
     self.test = False
     self.last_used_character = None
     self.open_character = None
Ejemplo n.º 7
0
import os
import sys
import json
import ast

from response_cache import ResponseCache

if len(sys.argv) < 2:
    print(
        "DANGER! Run reinitialize_database.pl with the parameter 'yes' if you really want to delete and re-create the Response database"
    )
    sys.exit(0)

if sys.argv[1] != 'yes':
    print(
        "If you really want to delete and re-create the feedback/Message database, use:"
    )
    print("  python reinitialize_database.py yes")
    sys.exit(0)

#### Create an RTX Feedback management object
response_cache = ResponseCache()

#### Purge and re-create the database
response_cache.create_database()
response_cache.prepopulate_database()

#### Connect to the database
response_cache.connect()