本文整理匯總了Python中wit.Wit方法的典型用法代碼示例。如果您正苦於以下問題:Python wit.Wit方法的具體用法?Python wit.Wit怎麽用?Python wit.Wit使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類wit
的用法示例。
在下文中一共展示了wit.Wit方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: teach_wit
# 需要導入模塊: import wit [as 別名]
# 或者: from wit import Wit [as 別名]
def teach_wit(wit_token, entity, values, doc=""):
import requests
print('*** TEACHING WIT ***')
params = {'v':'20160526'}
print('Inserting values of {}'.format(entity))
rsp = requests.request(
'PUT',
'https://api.wit.ai/entities/'+entity,
headers={
'authorization': 'Bearer ' + wit_token,
'accept': 'application/json'
},
params=params,
json={'doc':doc or entity, 'values':values}
)
if rsp.status_code > 200:
raise ValueError('Wit responded with status: ' + str(rsp.status_code) +
' (' + rsp.reason + '): ' + rsp.text)
json = rsp.json()
if 'error' in json:
raise ValueError('Wit responded with an error: ' + json['error'])
示例2: initialize
# 需要導入模塊: import wit [as 別名]
# 或者: from wit import Wit [as 別名]
def initialize(self, bot_handler: Any) -> None:
config = bot_handler.get_config_info('witai')
token = config.get('token')
if not token:
raise KeyError('No `token` was specified')
# `handler_location` should be the location of a module which contains
# the function `handle`. See `doc.md` for more details.
handler_location = config.get('handler_location')
if not handler_location:
raise KeyError('No `handler_location` was specified')
self.handle = get_handle(handler_location)
help_message = config.get('help_message')
if not help_message:
raise KeyError('No `help_message` was specified')
self.help_message = help_message
self.client = wit.Wit(token)
示例3: get_handle
# 需要導入模塊: import wit [as 別名]
# 或者: from wit import Wit [as 別名]
def get_handle(location: str) -> Callable[[Dict[str, Any]], Optional[str]]:
'''Returns a function to be used when generating a response from Wit.ai
bot. This function is the function named `handle` in the module at the
given `location`. For an example of a `handle` function, see `doc.md`.
For example,
handle = get_handle('/Users/someuser/witai_handler.py') # Get the handler function.
res = witai_client.message(message['content']) # Get the Wit.ai response.
message_res = self.handle(res) # Handle the response and find what to show the user.
bot_handler.send_reply(message, message_res) # Send it to the user.
Parameters:
- location: The absolute path to the module to look for `handle` in.
'''
try:
spec = importlib.util.spec_from_file_location('module.name', location)
handler = importlib.util.module_from_spec(spec)
spec.loader.exec_module(handler)
return handler.handle # type: ignore
except Exception as e:
print(e)
return None
示例4: clear_wit_cache
# 需要導入模塊: import wit [as 別名]
# 或者: from wit import Wit [as 別名]
def clear_wit_cache():
cache = settings.GOLEM_CONFIG.get('WIT_CACHE')
if cache:
print('Clearing Wit cache...')
db = get_redis()
db.delete('wit_cache')
示例5: init_wit_client
# 需要導入模塊: import wit [as 別名]
# 或者: from wit import Wit [as 別名]
def init_wit_client(self):
actions = {
'send': self.send_message,
'Welcome': self.send_welcome,
'GetLocation': self.send_location,
'GetEventTime': self.send_event_time,
'GetProgramHelp': self.get_program_help,
'FindProgramWithRoom': self.find_program_with_room,
'ShowTransportType': self.show_transport_types,
'ShowTransport': self.show_transport_result,
'ShowSponsors': self.show_sponsors,
'ShowSponsorIntro': self.show_sponsor_intro,
'ShowBooths': self.show_booths,
'ShowBoothIntro': self.show_booth_intro,
'ShowDirty': self.send_dirty,
'ShowPokemon': self.send_pokemon,
'ShowNothankyou': self.send_no_thankyou,
}
try:
new_action = self.dao.get_nlp_response('ACTIONMAP', self.lang)
if len(new_action) > 0:
new_actions = new_action[0].decode("utf-8").split(";")
for ac in new_actions:
if ac == '':
continue
self.logger.info('Add dyn action %s' % ac)
actions[ac] = self.send_simple_response
except Exception as ex:
self.logger.exception(ex)
return Wit(access_token=self.token, actions=actions)
示例6: process_receive
# 需要導入模塊: import wit [as 別名]
# 或者: from wit import Wit [as 別名]
def process_receive(self, receive):
mid = receive['from_mid']
try:
message = receive['content']['text']
self.logger.info('Wit process new message %s' % message)
session_id = self.get_session_id(mid)
result = self.client.run_actions(session_id, message, self.get_session_context(mid, receive),
action_confidence=0.3)
# if 'stop' in result:
# Action done. Clear cache data.
# self.clear_session_id(mid)
if 'processed' not in result:
self.logger.warning('Message [%s] not run in action.' % message)
self.clear_session_id(mid)
response = random_get_result(self.dao.get_nlp_response(NLPActions.Error, self.lang))
self.bot_api.reply_text(receive, response)
except wit.WitError as we:
self.logger.warning('Wit Process error %s' % we)
self.clear_session(mid)
response = random_get_result(self.dao.get_nlp_response(NLPActions.Error, self.lang))
self.bot_api.reply_text(receive, response)
except Exception as ex:
self.logger.exception(ex)
self.clear_session(mid)
response = random_get_result(self.dao.get_nlp_response(NLPActions.Error, self.lang))
self.bot_api.reply_text(receive, response)
示例7: send_message
# 需要導入模塊: import wit [as 別名]
# 或者: from wit import Wit [as 別名]
def send_message(self, request, response):
mid = request['context']['from_mid']
msg = utils.to_utf8_str(response['text'])
logging.info('Wit send message [%s] to [%s]', mid, msg)
self.bot_api.send_text(to_mid=mid, text=msg)
示例8: __init__
# 需要導入模塊: import wit [as 別名]
# 或者: from wit import Wit [as 別名]
def __init__(self):
from wit import Wit
access_token = config["wit_token"]
actions = {}
global client
client = Wit(access_token, actions)
示例9: __init__
# 需要導入模塊: import wit [as 別名]
# 或者: from wit import Wit [as 別名]
def __init__(self):
self.client = Wit(access_token= ACCESSTOKEN)
示例10: create_client
# 需要導入模塊: import wit [as 別名]
# 或者: from wit import Wit [as 別名]
def create_client(send_function, access_token=tokens.WIT_APP_TOKEN):
"""
Generates Wit Client. Called from apollobot.py
"""
print("Generated Wit Client with 'send' function provided: %r" % (send_function))
actions['send'] = send_function
return Wit(access_token=access_token, actions=actions)
示例11: get_client
# 需要導入模塊: import wit [as 別名]
# 或者: from wit import Wit [as 別名]
def get_client(send=None):
actions = ACTIONS.copy()
if send:
actions['send'] = send
return Wit(access_token=settings.WIT_TOKEN, actions=actions)
示例12: usage
# 需要導入模塊: import wit [as 別名]
# 或者: from wit import Wit [as 別名]
def usage(self) -> str:
return '''
Wit.ai bot uses pywit API to interact with Wit.ai. In order to use
Wit.ai bot, `witai.conf` must be set up. See `doc.md` for more details.
'''