微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用PYTHON Wit.ai如果traits:TypeError中没有trait,则不能迭代类型为'NoneType'的参数

如何解决使用PYTHON Wit.ai如果traits:TypeError中没有trait,则不能迭代类型为'NoneType'的参数

def first_trait_value(traits,trait):
  """
  Returns first trait value
  """
  if trait not in traits:
    return None
  val = traits[trait][0]['value']
  if not val:
    return None
return val

从Facebook页面输入消息时出现错误。但是当我把信息手册放进去的时候。没有错误发生。上面是那个功能

def message_form_entity(msg):
  response = None
  entity,trait,value = wit_response(msg)
  greeting = first_trait_value(trait,'wit$greetings')
  sentiment = first_trait_value(trait,'wit$sentiment')
  course = first_trait_value(trait,'wit_course')
  print(sentiment,greeting,course)
  if greeting:
    response = "{}! \nwelcome suza bot.\ncan I help you ?".format(str(value))
  elif entity == 'closes:closes':
    response = "{} :) \n you are welcome".format(str(value))
  elif course:
    response = "PhD in Kiswahili. \n Masters of Chemistry. \nMasters of  Science in Environmental Science. \nDegree of Science in Environmental Health. \nBachelor Degree of information Application and Management. \nBachelor Degree of Arts in Geography and Environmental Studies."
#     # response = random.choice(resp) 
  elif sentiment:
    response = 'Glad you liked it.' if sentiment == 'positive' else 'Hmm.' 
  if response == None:
    response = 'sorry not understand this'

  return (response)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。