def process_intent(text):
    """
    Attempt to process what the intent of a given text is
    :param text: Text to process
    :return: Intent name, if any
    """

    # Thresholds to check
    thresholds = [1.0, 0.9, 0.8, 0.7, 0.6, 0.5]

    logger.info('Matching text to intent: %s' % text)

    # For each threshold (starting at 100% match to 50% match)
    for threshold in thresholds:
        logger.info('Trying intent matching with threshold = %f' % threshold)

        # For each known intent
        for intent in intents:

            # Check to see if the text loosely matches the intent's utterance with the current threshold
            if loose_match(words=text,
                           expression=intent['text'],
                           threshold=threshold):
                return intent['intents']

    return None
コード例 #2
0
def dispatch(intent, robot):
    """
    Dispatch an intent
    :param intent: Intent name
    :param robot: Cozmo robot
    :return:
    """
    logger.info('Dispatching intent: %s' % intent)
    intent_callbacks[intent](robot)
コード例 #3
0
def register_intent(intent_name, callback):
    """
    Register an intent callback
    :param intent_name: Intent name (must be configured in `utterances.txt`)
    :param callback: Function that takes in a Cozmo robot object
    :return: None
    """
    logger.info('Intent registered: %s -> %s' %
                (intent_name, callback.__name__))
    intent_callbacks[intent_name] = callback
コード例 #4
0
def intent_talk(robot: cozmo.robot.Robot):
    cb = CleverWrap(CLEVERBOT_API_KEY)
    robot.say_text('What do you want to talk about?').wait_for_completed()

    while True:
        speech = csr.wait_for_speech(robot)

        if csr.loose_matches_any(speech, ['bye', 'goodbye', 'good bye'], 1):
            robot.say_text('Goodbye').wait_for_completed()
            return

        reply = cb.say(speech)
        logger.info('Cleverbot says: %s' % reply)

        robot.say_text(reply).wait_for_completed()
def load_intents():
    """
    Load configured intents from `utterances.txt`
    :return: None
    """
    global intents

    # Format: <IntentName> <speech utterance>
    r_utterance = re.compile('^(\w+) (.*)$')

    logger.info('Loading utterances ...')

    with open('utterances.txt') as file:
        utterances = file.read().split('\n')
        for utterance in utterances:
            match = r_utterance.match(utterance)
            intents.append({'intents': match.group(1), 'text': match.group(2)})

    logger.info('Loaded %d utterance(s)' % len(intents))
コード例 #6
0
def intent_twitter_message(robot: cozmo.robot.Robot):
    robot.move_lift(-3)
    robot.set_head_angle(cozmo.robot.MAX_HEAD_ANGLE).wait_for_completed()

    robot.say_text('Stand in front of me').wait_for_completed()

    try:
        robot.world.wait_for_observed_face(timeout=5)

        logger.info('Face found')

        robot.say_text('three').wait_for_completed()
        robot.say_text('two').wait_for_completed()
        robot.say_text('one').wait_for_completed()
        robot.say_text('Say cheese!').wait_for_completed()

        logger.info('Taking picture ...')

        latest_image = robot.world.latest_image
        latest_image.raw_image.convert('L').save('picture.png')

        robot.say_text('What should I put in the tweet?').wait_for_completed()
        speech = csr.wait_for_speech(robot)

        robot.say_text('I will tweet ' + speech).wait_for_completed()

        tweet = '@UBHacking ' + speech + ' #UBHacking'

        logger.info('Tweeting: %s' % tweet)

        api.PostMedia(tweet, 'picture.png')

    except asyncio.TimeoutError:
        robot.say_text('I can\'t find you')
def _listen(robot: cozmo.robot.Robot, timeout, phrase_time_limit):
    """
    Listen for speech input until some is found
    :param robot: Cozmo robot
    :param timeout: The timeout for listening
    :param phrase_time_limit: The max length of the phrase
    :return: The detected speech
    """
    logger.info(
        'Starting audio listening, timeout = %s, phrase_time_limit = %s' %
        (timeout, phrase_time_limit))

    # Enable backpack lights to signify listening
    robot.set_all_backpack_lights(cozmo.lights.blue_light)

    # Grab some audio from the microphone
    with sr.Microphone() as source:
        audio = r.listen(source,
                         timeout=timeout,
                         phrase_time_limit=phrase_time_limit)

    # Disable backpack lights during processing
    robot.set_backpack_lights_off()

    logger.info('Recognizing audio ...')

    try:
        # Use google to parse the audio, ensure the resulting string is all lowercase, and remove apostrophes
        text = str(r.recognize_google(audio)).lower().replace('\'', '')
        logger.info('Words: %s' % text)
        return text
    except sr.UnknownValueError:
        logger.info('Failed to recognize any words')
    except sr.RequestError:
        logger.error('Service down')

    # There was an exception, return None
    return None