Example #1
0
def say_message(message):
  global messageIndex
  print("We say '%s'" % message)

  (action, restOfPhrase) = extractAction(message)

  print("action: %s" % action)
  print("restOfPhrase: %s" % restOfPhrase)

  if action is not None:
    if action.lower() == "twitter":
      import brain as Brain
      Brain.searchTwitterForValue(restOfPhrase)
      return
    elif action == "neutral":
      ACTION.action_be_neutral()
    elif action == "happy":
      ACTION.action_be_happy()
    elif action == "unhappy":
      ACTION.action_be_unhappy()
    elif action == "angry":
      ACTION.action_be_angry()
    elif action == "surprised":
      ACTION.action_be_surprised()
  else:
    RESPONSE_INFO.analyse(message)

  thisIndex = messageIndex

  filename = os.path.realpath('audio/%s.wav' % thisIndex)
  os.system("espeak -g 10 -w %s \"%s\"" % (filename, restOfPhrase))
  messageIndex = messageIndex + 1
  for client in SocketManager.clients:
        client.write_message("audio:http://bb4d0cce.ngrok.io/audio/%s.wav" % thisIndex)
def analyse(message):
  values = {
    'key' : '8ad960732d31af8741e42faf716b7d14',
    'txt' : message,
    'model' : 'general_en'
  }
  data = urllib.urlencode(values)
  req = urllib2.Request("https://api.meaningcloud.com/sentiment-2.0", data)
  response = urllib2.urlopen(req)
  data = json.load(response)

  if data['subjectivity'] == "SUBJECTIVE":
    # Opinion
    if data['score_tag'] == "P" or data['score_tag'] == "P+":
      print("Be happy")
      ACTION.action_be_happy()

    elif data['score_tag'] == "N" or data['score_tag'] == "N+":
      print("Be Sad")
      ACTION.action_be_unhappy()
    else:
      ACTION.action_be_surprised()
      print("No Comment")
  else:
    if data['score_tag'] == "P" or data['score_tag'] == "P+":
      print("I agree")
     
      ACTION.action_be_surprised()

    elif data['score_tag'] == "N" or data['score_tag'] == "N+":
      print("Be Angry")
      
      ACTION.action_be_angry()
    else:
      
      ACTION.action_be_surprised()