├── .gitignore ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── SECURITY.md ├── python2 ├── luis_sdk │ ├── __init__.py │ ├── luis_action.py │ ├── luis_client.py │ ├── luis_composite_entity.py │ ├── luis_composite_entity_child.py │ ├── luis_dialog.py │ ├── luis_entity.py │ ├── luis_intent.py │ ├── luis_parameter.py │ ├── luis_parametervalue.py │ └── luis_response.py ├── sample_app.py └── sample_app_async.py ├── python3 ├── luis_sdk │ ├── __init__.py │ ├── luis_action.py │ ├── luis_client.py │ ├── luis_composite_entity.py │ ├── luis_composite_entity_child.py │ ├── luis_dialog.py │ ├── luis_entity.py │ ├── luis_intent.py │ ├── luis_parameter.py │ ├── luis_parametervalue.py │ └── luis_response.py ├── sample_app.py └── sample_app_async.py └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.py[cod] 2 | 3 | # C extensions 4 | *.so 5 | 6 | # Packages 7 | *.egg 8 | *.egg-info 9 | dist 10 | build 11 | eggs 12 | parts 13 | bin 14 | var 15 | sdist 16 | develop-eggs 17 | .installed.cfg 18 | lib 19 | lib64 20 | 21 | # Installer logs 22 | pip-log.txt 23 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Contributing to Microsoft Cognitive Services Client Libraries & Samples 2 | =============================================== 3 | So, you want to contribute on a client library or sample for one of the Microsoft Cognitive Services. 4 | Here's what you need to know. 5 | 6 | 1. Each SDK should include both a client library and a sample showing the API in 7 | action 8 | 9 | 2. When working on an SDK, it's important that we are consistent from project to project, so we ask you to follow the coding guidelines below: 10 | 11 | - Windows [(Coding guidelines for C#)](https://msdn.microsoft.com/en-us/library/ff926074.aspx) -- also reference our [common Windows code](https://github.com/Microsoft/Cognitive-common-windows) for building samples 12 | 13 | - Android [(Coding guidelines for 14 | Java)]() 15 | 16 | - iOS Objective-C [(Coding guidelines for 17 | Cocoa)]() 18 | 19 | - Optional: Client Javascript ([Coding guidelines for 20 | npm]()) 21 | 22 | 3. Samples are important for illustrating how to actually call into the API. 23 | Samples should be as visual and reusable as possible. 24 | 25 | - Do: 26 | 27 | - Create a UI sample when possible. 28 | 29 | - Make your sample user friendly. Expect that developers will want to try 30 | different mainline scenarios and key APIs. 31 | 32 | - Create code that's easy for other developers to copy/paste into their 33 | own solutions 34 | 35 | - Consider: 36 | 37 | - Adding UI to allow devs to quickly copy/paste subscription keys, instead 38 | of updating them in the code or using a config file. The 39 | FaceAPI-WPF-Samples.sln provides an example. 40 | 41 | - Don't: 42 | 43 | - Leave your subscription key in the source of samples. You do not want your key to be abused by others. 44 | 45 | Happy coding! 46 | 47 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Microsoft Cognitive Services SDK 2 | 3 | Copyright (c) Microsoft Corporation 4 | 5 | All rights reserved. 6 | 7 | *This license applies only to the SDK and the sample applications code. 8 | 9 | MIT License 10 | 11 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 12 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/microsoft/Cognitive-LUIS-Python/8ec463f320ebf86c5dae97ac5762de184fcd42d0/README.md -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## Security 4 | 5 | Microsoft takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations, which include [Microsoft](https://github.com/microsoft), [Azure](https://github.com/Azure), [DotNet](https://github.com/dotnet), [AspNet](https://github.com/aspnet), [Xamarin](https://github.com/xamarin), and [our GitHub organizations](https://opensource.microsoft.com/). 6 | 7 | If you believe you have found a security vulnerability in any Microsoft-owned repository that meets [Microsoft's definition of a security vulnerability](https://aka.ms/opensource/security/definition), please report it to us as described below. 8 | 9 | ## Reporting Security Issues 10 | 11 | **Please do not report security vulnerabilities through public GitHub issues.** 12 | 13 | Instead, please report them to the Microsoft Security Response Center (MSRC) at [https://msrc.microsoft.com/create-report](https://aka.ms/opensource/security/create-report). 14 | 15 | If you prefer to submit without logging in, send email to [secure@microsoft.com](mailto:secure@microsoft.com). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://aka.ms/opensource/security/pgpkey). 16 | 17 | You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://aka.ms/opensource/security/msrc). 18 | 19 | Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue: 20 | 21 | * Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.) 22 | * Full paths of source file(s) related to the manifestation of the issue 23 | * The location of the affected source code (tag/branch/commit or direct URL) 24 | * Any special configuration required to reproduce the issue 25 | * Step-by-step instructions to reproduce the issue 26 | * Proof-of-concept or exploit code (if possible) 27 | * Impact of the issue, including how an attacker might exploit the issue 28 | 29 | This information will help us triage your report more quickly. 30 | 31 | If you are reporting for a bug bounty, more complete reports can contribute to a higher bounty award. Please visit our [Microsoft Bug Bounty Program](https://aka.ms/opensource/security/bounty) page for more details about our active programs. 32 | 33 | ## Preferred Languages 34 | 35 | We prefer all communications to be in English. 36 | 37 | ## Policy 38 | 39 | Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://aka.ms/opensource/security/cvd). 40 | 41 | 42 | -------------------------------------------------------------------------------- /python2/luis_sdk/__init__.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | from .luis_client import LUISClient 35 | -------------------------------------------------------------------------------- /python2/luis_sdk/luis_action.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | from .luis_parameter import LUISParameter 35 | 36 | class LUISAction(object): 37 | ''' 38 | LUIS Action Class. 39 | Describes the LUIS Action structure. 40 | ''' 41 | 42 | def __init__(self, action): 43 | ''' 44 | A constructor for the LUISAction class. 45 | :param action: A dictionary containing the action data. 46 | ''' 47 | self._name = action[u'name'] 48 | self._triggered = action[u'triggered'] 49 | self._parameters = [] 50 | for parameter in action[u'parameters']: 51 | self._parameters.append(LUISParameter(parameter)) 52 | 53 | def get_name(self): 54 | ''' 55 | A getter for the action's name. 56 | :return: Actions's name. 57 | ''' 58 | return self._name 59 | 60 | def get_triggered(self): 61 | ''' 62 | A getter for the action's triggered flag. 63 | :return: A boolean that expresses whether the action was trigerred or not. 64 | ''' 65 | return self._triggered 66 | 67 | def get_parameters(self): 68 | ''' 69 | A getter for the action's parameters. 70 | :return: A list of parameter. 71 | ''' 72 | return self._parameters 73 | -------------------------------------------------------------------------------- /python2/luis_sdk/luis_client.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | import threading 35 | from urllib import quote 36 | import httplib 37 | from .luis_response import LUISResponse 38 | 39 | class LUISClient(object): 40 | ''' 41 | This is the interface of the LUIS 42 | Constructs a LUISClient with the corresponding user's App Id and Subscription Keys 43 | Starts the prediction procedure for the user's text, and accepts a callback function 44 | ''' 45 | _LUISURL = u'westus.api.cognitive.microsoft.com' 46 | _PredictMask = u'/luis/v2.0/apps/%s?subscription-key=%s&q=%s&verbose=%s' 47 | _ReplyMask = u'/luis/v2.0/apps/%s?subscription-key=%s&q=%s&contextid=%s&verbose=%s' 48 | 49 | def __init__(self, app_id, app_key, verbose=True): 50 | ''' 51 | A constructor for the LUISClient class. 52 | :param app_id: A string containing the application id. 53 | :param app_key: A string containing the subscription key. 54 | :param verbose: A boolean to indicate whether the verbose version should used or not. 55 | ''' 56 | if app_id is None: 57 | raise TypeError(u'NULL App Id') 58 | if not app_id: 59 | raise ValueError(u'Empty App Id') 60 | if u' ' in app_id: 61 | raise ValueError(u'Invalid App Id') 62 | if app_key is None: 63 | raise TypeError(u'NULL Subscription Key') 64 | if not app_key: 65 | raise ValueError(u'Empty Subscription Key') 66 | if u' ' in app_key: 67 | raise ValueError(u'Invalid Subscription Key') 68 | 69 | self._app_id = app_id 70 | self._app_key = app_key 71 | self._verbose = u'true' if verbose else u'false' 72 | 73 | def predict(self, text, response_handlers=None, daemon=False): 74 | ''' 75 | Routes the prediction routine to either sync or async 76 | based on the presence or absence of a callback fucntion. 77 | :param text: the text to be analysed and predicted. 78 | :param response_handlers: a dictionary that contains two keys on_success and on_failure, 79 | whose values are two functions to be executed if async. 80 | :param daemon: defines whether the new thread used for async will be daemon or not. 81 | :return: LUISResponse if sync, thread object to give control over the thread if async. 82 | ''' 83 | if text is None: 84 | raise TypeError(u'NULL text to predict') 85 | text = text.strip() 86 | if not text: 87 | raise ValueError(u'Empty text to predict') 88 | if response_handlers is None: 89 | return self.predict_sync(text) 90 | else: 91 | return self.predict_async(text, response_handlers, daemon) 92 | 93 | def predict_sync(self, text): 94 | ''' 95 | Predicts synchronously and returns a LUISResponse. 96 | :param text: The text to be analysed and predicted. 97 | :return: A LUISResponse object containing the response data. 98 | ''' 99 | try: 100 | conn = httplib.HTTPSConnection(self._LUISURL) 101 | conn.request(u'GET', self._predict_url_gen(text)) 102 | res = conn.getresponse() 103 | return LUISResponse(res.read().decode(u'UTF-8')) 104 | except Exception: 105 | raise 106 | 107 | def predict_async(self, text, response_handlers, daemon): 108 | ''' 109 | Predicts asynchronously and executes a callback function at the end. 110 | :param text: The text to be analysed and predicted. 111 | :param response_handlers: A dictionary that contains two keys on_success and on_failure, 112 | whose values are two functions to be executed if async. 113 | :param daemon: Defines whether the new thread will be daemon or not. 114 | :return: A thread object to give control over the thread. 115 | ''' 116 | if u'on_success' not in response_handlers: 117 | raise KeyError(u'You have to specify the success handler with key: "on_success"') 118 | if u'on_failure' not in response_handlers: 119 | raise KeyError(u'You have to specify the failure handler with key: "on_failure"') 120 | predict_thread = threading.Thread(target=self._predict_async_helper 121 | , args=(text, response_handlers)) 122 | predict_thread.daemon = daemon 123 | predict_thread.start() 124 | return predict_thread 125 | 126 | def _predict_url_gen(self, text): 127 | ''' 128 | Returns the suitable LUIS API predict url. 129 | :param text: The text to be analysed and predicted. 130 | :return: LUIS API predicton url. 131 | ''' 132 | return self._PredictMask%(self._app_id, self._app_key, quote(text), self._verbose) 133 | 134 | def _predict_async_helper(self, text, response_handlers): 135 | ''' 136 | A wrapper function to be executed asynchronously in an external thread. 137 | It executes the predict routine and then executes a callback function. 138 | :param text: The text to be analysed and predicted. 139 | :param response: A LUISResponse that contains the context Id. 140 | :param response_handlers: A dictionary that contains two keys on_success and on_failure, 141 | whose values are two functions to be executed if async. 142 | :return: None. 143 | ''' 144 | res = None 145 | try: 146 | res = self.predict(text) 147 | except Exception, exc: 148 | response_handlers[u'on_failure'](exc) 149 | return 150 | response_handlers[u'on_success'](res) 151 | 152 | def reply(self, text, response, response_handlers=None, force_set_parameter_name=None, daemon=False): 153 | ''' 154 | Routes the reply routine to either sync or async 155 | based on the presence or absence of a callback fucntion. 156 | :param text: The text to be analysed and predicted. 157 | :param response: A LUISResponse object that contains the context Id. 158 | :param response_handlers: A dictionary that contains two keys on_success and on_failure, 159 | whose values are two functions 160 | to be executed if async. 161 | :param force_set_parameter_name: The name of a parameter the needs to be reset in dialog. 162 | :param daemon: Defines whether the new thread used for async will be daemon or not. 163 | :return: A LUISResponse object if sync, a thread object to control the thread if async. 164 | ''' 165 | if text is None: 166 | raise TypeError(u'NULL text to predict') 167 | text = text.strip() 168 | if not text: 169 | raise ValueError(u'Empty text to predict') 170 | if response_handlers is None: 171 | return self.reply_sync(text, response, force_set_parameter_name) 172 | else: 173 | return self.reply_async(text, response, response_handlers, force_set_parameter_name, daemon) 174 | 175 | def reply_sync(self, text, response, force_set_parameter_name=None): 176 | ''' 177 | Replies synchronously and returns a LUISResponse object. 178 | :param text: The text to be analysed and predicted. 179 | :param response: A LUISResponse object that contains the context Id. 180 | :param force_set_parameter_name: The name of a parameter the needs to be reset in dialog. 181 | :return: A LUISResponse object containg the response data. 182 | ''' 183 | try: 184 | conn = httplib.HTTPSConnection(self._LUISURL) 185 | conn.request(u'GET', self._reply_url_gen(text, response, force_set_parameter_name)) 186 | res = conn.getresponse() 187 | return LUISResponse(res.read().decode(u'UTF-8')) 188 | except Exception: 189 | raise 190 | 191 | def reply_async(self, text, response, response_handlers, force_set_parameter_name, daemon): 192 | ''' 193 | Predicts asynchronously and executes a callback function at the end. 194 | :param text: The text to be analysed and predicted. 195 | :param response: A LUISResponse object that contains the context Id. 196 | :param response_handlers: A dictionary that contains two keys on_success and on_failure, 197 | whose values are two functions 198 | to be executed if async. 199 | :param force_set_parameter_name: The name of a parameter the needs to be reset in dialog. 200 | :param daemon: Defines whether the new thread used will be daemon or not. 201 | :return: A thread object to give control over the thread. 202 | ''' 203 | if u'on_success' not in response_handlers: 204 | raise KeyError(u'You have to specify the success handler with key: "on_success"') 205 | if u'on_failure' not in response_handlers: 206 | raise KeyError(u'You have to specify the failure handler with key: "on_failure"') 207 | reply_thread = threading.Thread(target=self._reply_async_helper 208 | , args=(text, response, response_handlers, force_set_parameter_name)) 209 | reply_thread.daemon = daemon 210 | reply_thread.start() 211 | return reply_thread 212 | 213 | def _reply_url_gen(self, text, response, force_set_parameter_name): 214 | ''' 215 | Generates the suitable LUIS API reply url. 216 | :param text: The text to be analysed and predicted. 217 | :param response: A LUISResponse object that contains the context Id. 218 | :param force_set_parameter_name: The name of a parameter the needs to be reset in dialog. 219 | :return: LUIS API reply url. 220 | ''' 221 | url = self._ReplyMask%(self._app_id, self._app_key, quote(text) 222 | , response.get_dialog().get_context_id(), self._verbose) 223 | if force_set_parameter_name is not None: 224 | url += u'&forceset=%s'%(force_set_parameter_name) 225 | return url 226 | 227 | def _reply_async_helper(self, text, response, response_handlers, force_set_parameter_name): 228 | ''' 229 | A wrapper function to be executed asynchronously in an external thread. 230 | It executes the reply routine and then executes a callback function. 231 | :param text: The text to be analysed and predicted. 232 | :param response: A LUISResponse object that contains the context Id. 233 | :param response_handlers: A dictionary that contains two keys on_success and on_failure, 234 | whose values are two functions to be executed if async. 235 | :param force_set_parameter_name: The name of a parameter the needs to be reset in dialog. 236 | :return: None. 237 | ''' 238 | res = None 239 | try: 240 | res = self.reply(text, response, force_set_parameter_name) 241 | except Exception, exc: 242 | response_handlers[u'on_failure'](exc) 243 | return 244 | response_handlers[u'on_success'](res) 245 | -------------------------------------------------------------------------------- /python2/luis_sdk/luis_composite_entity.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | from .luis_composite_entity_child import LUISCompositeEntityChild 35 | 36 | class LUISCompositeEntity: 37 | ''' 38 | LUIS Composite Entity Class. 39 | Describes the LUIS Composite Entity structure. 40 | ''' 41 | 42 | def __init__(self, composite_entity): 43 | ''' 44 | A constructor for the LUISCompositeEntity class. 45 | :param composite_entity: A dictionary containing the composite entity data. 46 | ''' 47 | self._parent_type = composite_entity['parentType'] 48 | self._value = composite_entity['value'] 49 | self._composite_entity_children = [] 50 | for composite_entity_child in composite_entity['children']: 51 | self._composite_entity_children.append(LUISCompositeEntityChild(composite_entity_child)) 52 | 53 | def get_parent_type(self): 54 | ''' 55 | A getter for the composite entity's parent type. 56 | :return: Composite Entity's parent type. 57 | ''' 58 | return self._parent_type 59 | 60 | def get_value(self): 61 | ''' 62 | A getter for the composite entity's value. 63 | :return: Composite Entity's value. 64 | ''' 65 | return self._value 66 | 67 | def get_children(self): 68 | ''' 69 | A getter for the composite entity's children. 70 | :return: Composite_Entity's children. 71 | ''' 72 | return self._composite_entity_children 73 | -------------------------------------------------------------------------------- /python2/luis_sdk/luis_composite_entity_child.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | class LUISCompositeEntityChild: 35 | ''' 36 | LUIS Composite Entity Child Class. 37 | Describes the LUIS Composite Entity Child structure. 38 | ''' 39 | 40 | def __init__(self, composite_entity_child): 41 | ''' 42 | A constructor for the LUISCompositeEntityChild class. 43 | :param composite_entity: A dictionary containing the composite entity child data. 44 | ''' 45 | self._type = composite_entity_child['type'] 46 | self._value = composite_entity_child['value'] 47 | 48 | def get_type(self): 49 | ''' 50 | A getter for the composite entity child's type. 51 | :return: Composite Entity Child's type. 52 | ''' 53 | return self._type 54 | 55 | def get_value(self): 56 | ''' 57 | A getter for the composite entity child's value. 58 | :return: Composite Entity Child's value. 59 | ''' 60 | return self._value 61 | -------------------------------------------------------------------------------- /python2/luis_sdk/luis_dialog.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | class LUISDialog(object): 35 | ''' 36 | LUIS Dialog Class. 37 | Describes the LUIS Action structure. 38 | ''' 39 | 40 | def __init__(self, dialog): 41 | ''' 42 | A constructor for the LUISDialog class. 43 | :param action: A dictionary containing the dialog data. 44 | ''' 45 | if u'prompt' in dialog: 46 | self._prompt = dialog[u'prompt'] 47 | else: 48 | self._prompt = None 49 | 50 | if u'parameterName' in dialog: 51 | self._parameter_name = dialog[u'parameterName'] 52 | else: 53 | self._parameter_name = None 54 | 55 | self._context_id = dialog[u'contextId'] 56 | self._status = dialog[u'status'] 57 | self._finished = self._status == u'Finished' 58 | 59 | def get_prompt(self): 60 | ''' 61 | A getter for the dialog's prompt. 62 | :return: Dialog's prompt. 63 | ''' 64 | return self._prompt 65 | 66 | def get_parameter_name(self): 67 | ''' 68 | A getter for the dialog's parameter name. 69 | :return: Dialog's parameter name. 70 | ''' 71 | return self._parameter_name 72 | 73 | def get_context_id(self): 74 | ''' 75 | A getter for the dialog's context Id. 76 | :return: Dialog's prompt. 77 | ''' 78 | return self._context_id 79 | 80 | def get_status(self): 81 | ''' 82 | A getter for the dialog's status. 83 | :return: Dialog's status. 84 | ''' 85 | return self._status 86 | 87 | def is_finished(self): 88 | ''' 89 | Checks whether the dialog has finished or not. 90 | :return: A boolean that expresses whether the dialog has finished or not. 91 | ''' 92 | return self._finished 93 | -------------------------------------------------------------------------------- /python2/luis_sdk/luis_entity.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | class LUISEntity(object): 35 | ''' 36 | LUIS Entity Class. 37 | Describes the LUIS Entity structure. 38 | ''' 39 | 40 | def __init__(self, entity): 41 | ''' 42 | A constructor for the LUISEntity class. 43 | :param entity: A dictionary containing the entity data. 44 | ''' 45 | self._name = entity[u'entity'] 46 | self._type = entity[u'type'] 47 | if u'startIndex' in entity: 48 | self._start_idx = entity[u'startIndex'] 49 | else: 50 | self._start_idx = None 51 | if u'endIndex' in entity: 52 | self._end_idx = entity[u'endIndex'] 53 | else: 54 | self._end_idx = None 55 | if u'score' in entity: 56 | self._score = entity[u'score'] 57 | else: 58 | self._score = None 59 | if u'resolution' in entity: 60 | self._resolution = entity[u'resolution'] 61 | else: 62 | self._resolution = None 63 | 64 | def get_name(self): 65 | ''' 66 | A getter for the entity's name. 67 | :return: Entity's name. 68 | ''' 69 | return self._name 70 | 71 | def get_type(self): 72 | ''' 73 | A getter for the entity's type. 74 | :return: Entity's type. 75 | ''' 76 | return self._type 77 | 78 | def get_start_idx(self): 79 | ''' 80 | A getter for the entity's start index. 81 | :return: Entity's start index. 82 | ''' 83 | return self._start_idx 84 | 85 | def get_end_idx(self): 86 | ''' 87 | A getter for the entity's end index. 88 | :return: Entity's end index. 89 | ''' 90 | return self._end_idx 91 | 92 | def get_score(self): 93 | ''' 94 | A getter for the entity's score. 95 | :return: Entity's score. 96 | ''' 97 | return self._score 98 | 99 | 100 | def get_resolution(self): 101 | ''' 102 | A getter for the entity's resolution. 103 | :return: Entity's resolution. 104 | ''' 105 | return self._resolution 106 | -------------------------------------------------------------------------------- /python2/luis_sdk/luis_intent.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | from .luis_action import LUISAction 35 | 36 | class LUISIntent(object): 37 | ''' 38 | LUIS Intent Class. 39 | Describes the LUIS Intent structure. 40 | ''' 41 | 42 | def __init__(self, intent): 43 | ''' 44 | A constructor for the LUISIntent class. 45 | :param intent: A dictionary containing the intent data. 46 | ''' 47 | self._name = intent[u'intent'] 48 | self._score = intent[u'score'] 49 | self._actions = [] 50 | 51 | if u'actions' in intent: 52 | for action in intent[u'actions']: 53 | self._actions.append(LUISAction(action)) 54 | 55 | 56 | def get_name(self): 57 | ''' 58 | A getter for the intent's name. 59 | :return: Intent's name. 60 | ''' 61 | return self._name 62 | 63 | def get_score(self): 64 | ''' 65 | A getter for the intent's score. 66 | :return: Intent's score. 67 | ''' 68 | return self._score 69 | 70 | def get_actions(self): 71 | ''' 72 | A getter for the entity's actions. 73 | :return: A list of actions. 74 | ''' 75 | return self._actions 76 | -------------------------------------------------------------------------------- /python2/luis_sdk/luis_parameter.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | from .luis_parametervalue import LUISParameterValue 35 | import luis_parametervalue 36 | 37 | class LUISParameter(object): 38 | ''' 39 | LUIS Parameter Class. 40 | Describes the LUIS Parameter structure. 41 | ''' 42 | 43 | def __init__(self, parameter): 44 | ''' 45 | A constructor for the LUISParameter class. 46 | :param parameter: A dictionary containing the parameter data. 47 | ''' 48 | self._name = parameter[u'name'] 49 | self._required = parameter[u'required'] 50 | self._parameter_values = [] 51 | if parameter[u'value']: 52 | for parameter_value in parameter[u'value']: 53 | self._parameter_values.append(LUISParameterValue(parameter_value)) 54 | 55 | def get_name(self): 56 | ''' 57 | A getter for the parameter's name. 58 | :return: Parameter's name. 59 | ''' 60 | return self._name 61 | 62 | def get_required(self): 63 | ''' 64 | A getter for the parameter's required flag. 65 | :return: A boolean that expresses whether the parameter is required or not. 66 | ''' 67 | return self._required 68 | 69 | def get_parameter_values(self): 70 | ''' 71 | A getter for the parameters's values. 72 | :return: A list of parameter values. 73 | ''' 74 | return self._parameter_values 75 | -------------------------------------------------------------------------------- /python2/luis_sdk/luis_parametervalue.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | class LUISParameterValue(object): 35 | ''' 36 | LUIS Paramater Value Class. 37 | Describes the LUIS Paramter Value structure. 38 | ''' 39 | 40 | def __init__(self, parameter_value): 41 | ''' 42 | A constructor for the LUISAction class. 43 | :param parameter_value: A dictionary containing the parameter value data. 44 | ''' 45 | self._name = parameter_value[u'entity'] 46 | self._type = parameter_value[u'type'] 47 | if u'score' in parameter_value: 48 | self._score = parameter_value[u'score'] 49 | else: 50 | self._score = None 51 | if u'resolution' in parameter_value: 52 | self._resolution = parameter_value[u'resolution'] 53 | else: 54 | self._resolution = None 55 | 56 | def get_name(self): 57 | ''' 58 | A getter for the parameter value's name. 59 | :return: Parameter value's name. 60 | ''' 61 | return self._name 62 | 63 | def get_type(self): 64 | ''' 65 | A getter for the parameter value's type. 66 | :return: Parameter values's type. 67 | ''' 68 | return self._type 69 | 70 | def get_score(self): 71 | ''' 72 | A getter for the parameter value's score. 73 | :return: Parameter values's score. 74 | ''' 75 | return self._score 76 | 77 | def get_resolution(self): 78 | ''' 79 | A getter for the parameter value's resolution. 80 | :return: Parameter value's resolution. 81 | ''' 82 | return self._resolution 83 | -------------------------------------------------------------------------------- /python2/luis_sdk/luis_response.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | import json 35 | from .luis_intent import LUISIntent 36 | from .luis_entity import LUISEntity 37 | from .luis_composite_entity import LUISCompositeEntity 38 | from .luis_dialog import LUISDialog 39 | 40 | class LUISResponse(object): 41 | ''' 42 | LUIS Response Class. 43 | Describes the response structure, and is the main point 44 | to access the response sent by LUIS after prediction. 45 | ''' 46 | 47 | def __init__(self, JSONResponse): 48 | ''' 49 | A constructor for the LUISResponse class. 50 | :param JSONResponse: A string containing the incoming JSON. 51 | ''' 52 | if JSONResponse is None: 53 | raise TypeError(u'NULL JSON response') 54 | if not JSONResponse: 55 | raise ValueError(u'Invalid App Id') 56 | 57 | if isinstance(JSONResponse, unicode): 58 | try: 59 | response = json.loads(JSONResponse) 60 | except Exception: 61 | raise Exception(u'Error in parsing json') 62 | else: 63 | response = JSONResponse 64 | 65 | if u'statusCode' in response: 66 | raise Exception(u'Invalid Subscription Key') 67 | 68 | self._query = response[u'query'] 69 | 70 | if u'dialog' in response: 71 | self._dialog = LUISDialog(response[u'dialog']) 72 | else: 73 | self._dialog = None 74 | 75 | self._intents = [] 76 | self._entities = [] 77 | self._composite_entities = [] 78 | 79 | self._top_scoring_intent = LUISIntent(response[u'topScoringIntent']) 80 | 81 | if u'intents' in response: 82 | for intent in response[u'intents']: 83 | self._intents.append(LUISIntent(intent)) 84 | else: 85 | self._intents.append(self._top_scoring_intent) 86 | 87 | for entity in response[u'entities']: 88 | self._entities.append(LUISEntity(entity)) 89 | 90 | if u'compositeEntities' in response: 91 | for composite_entity in response[u'compositeEntities']: 92 | self._composite_entities.append(LUISCompositeEntity(composite_entity)) 93 | 94 | def get_query(self): 95 | ''' 96 | A getter for the response's query. 97 | :return: Response's query. 98 | ''' 99 | return self._query 100 | 101 | def get_top_intent(self): 102 | ''' 103 | A getter for the response's top scoring intent. 104 | :return: Response's top scoring intent. 105 | ''' 106 | return self._top_scoring_intent 107 | 108 | def get_intents(self): 109 | ''' 110 | A getter for the response's intents. 111 | :return: A list of intents. 112 | ''' 113 | return self._intents 114 | 115 | def get_entities(self): 116 | ''' 117 | A getter for the response's entities. 118 | :return: A list of entities. 119 | ''' 120 | return self._entities 121 | 122 | def get_composite_entities(self): 123 | ''' 124 | A getter for the response's composite entities. 125 | :return: A list of composite entities. 126 | ''' 127 | return self._composite_entities 128 | 129 | def get_dialog(self): 130 | ''' 131 | A getter for the response's dialog. 132 | :return: Response's dialog. 133 | ''' 134 | return self._dialog 135 | -------------------------------------------------------------------------------- /python2/sample_app.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | from luis_sdk import LUISClient 35 | 36 | def process_res(res): 37 | ''' 38 | A function that processes the luis_response object and prints info from it. 39 | :param res: A LUISResponse object containing the response data. 40 | :return: None 41 | ''' 42 | print(u'---------------------------------------------') 43 | print(u'LUIS Response: ') 44 | print(u'Query: ' + res.get_query()) 45 | print(u'Top Scoring Intent: ' + res.get_top_intent().get_name()) 46 | if res.get_dialog() is not None: 47 | if res.get_dialog().get_prompt() is None: 48 | print(u'Dialog Prompt: None') 49 | else: 50 | print(u'Dialog Prompt: ' + res.get_dialog().get_prompt()) 51 | if res.get_dialog().get_parameter_name() is None: 52 | print(u'Dialog Parameter: None') 53 | else: 54 | print('Dialog Parameter Name: ' + res.get_dialog().get_parameter_name()) 55 | print(u'Dialog Status: ' + res.get_dialog().get_status()) 56 | print(u'Entities:') 57 | for entity in res.get_entities(): 58 | print(u'"%s":' % entity.get_name()) 59 | print(u'Type: %s, Score: %s' % (entity.get_type(), entity.get_score())) 60 | 61 | try: 62 | APPID = raw_input(u'Please enter your app Id:\n') 63 | APPKEY = raw_input(u'Please input your subscription key:\n') 64 | TEXT = raw_input(u'Please input the text to predict:\n') 65 | CLIENT = LUISClient(APPID, APPKEY, True) 66 | res = CLIENT.predict(TEXT) 67 | while res.get_dialog() is not None and not res.get_dialog().is_finished(): 68 | TEXT = raw_input(u'%s\n'%res.get_dialog().get_prompt()) 69 | res = CLIENT.reply(TEXT, res) 70 | process_res(res) 71 | except Exception, exc: 72 | print(exc) 73 | -------------------------------------------------------------------------------- /python2/sample_app_async.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | from luis_sdk import LUISClient 35 | 36 | def on_success(res): 37 | ''' 38 | A callback function that processes the luis_response object 39 | if the prediction succeeds. 40 | :param res: a luis_response object containing the response data. 41 | :return: None 42 | ''' 43 | print(u'---------------------------------------------') 44 | print(u'LUIS Response: ') 45 | print(u'Query: ' + res.get_query()) 46 | print(u'Top Scoring Intent: ' + res.get_top_intent().get_name()) 47 | if res.get_dialog() is not None: 48 | if res.get_dialog().get_prompt() is None: 49 | print(u'Dialog Prompt: None') 50 | else: 51 | print(u'Dialog Prompt: ' + res.get_dialog().get_prompt()) 52 | if res.get_dialog().get_parameter_name() is None: 53 | print(u'Dialog Parameter: None') 54 | else: 55 | print('Dialog Parameter Name: ' + res.get_dialog().get_parameter_name()) 56 | print(u'Dialog Status: ' + res.get_dialog().get_status()) 57 | print(u'Entities:') 58 | for entity in res.get_entities(): 59 | print(u'"%s":' % entity.get_name()) 60 | print(u'Type: %s, Score: %s' % (entity.get_type(), entity.get_score())) 61 | 62 | def on_failure(err): 63 | ''' 64 | A callback function that processes the error object 65 | if the prediction fails. 66 | :param err: An Exception object. 67 | :return: None 68 | ''' 69 | print err 70 | 71 | try: 72 | APPID = raw_input(u'Please enter your app Id:\n') 73 | APPKEY = raw_input(u'Please input your subscription key:\n') 74 | TEXT = raw_input(u'Please input the text to predict:\n') 75 | CLIENT = LUISClient(APPID, APPKEY, True) 76 | CLIENT.predict(TEXT, {u'on_success': on_success, u'on_failure': on_failure}) 77 | print u'-------\nMain thread finishing!!\n-------' 78 | except Exception, exc: 79 | print exc 80 | -------------------------------------------------------------------------------- /python3/luis_sdk/__init__.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | from .luis_client import LUISClient 35 | -------------------------------------------------------------------------------- /python3/luis_sdk/luis_action.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | from .luis_parameter import LUISParameter 35 | 36 | class LUISAction: 37 | ''' 38 | LUIS Action Class. 39 | Describes the LUIS Action structure. 40 | ''' 41 | 42 | def __init__(self, action): 43 | ''' 44 | A constructor for the LUISAction class. 45 | :param action: A dictionary containing the action data. 46 | ''' 47 | self._name = action['name'] 48 | self._triggered = action['triggered'] 49 | self._parameters = [] 50 | for parameter in action['parameters']: 51 | self._parameters.append(LUISParameter(parameter)) 52 | 53 | def get_name(self): 54 | ''' 55 | A getter for the action's name. 56 | :return: Actions's name. 57 | ''' 58 | return self._name 59 | 60 | def get_triggered(self): 61 | ''' 62 | A getter for the action's triggered flag. 63 | :return: A boolean that expresses whether the action was trigerred or not. 64 | ''' 65 | return self._triggered 66 | 67 | def get_parameters(self): 68 | ''' 69 | A getter for the action's parameters. 70 | :return: A list of parameter. 71 | ''' 72 | return self._parameters 73 | -------------------------------------------------------------------------------- /python3/luis_sdk/luis_client.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | import threading 35 | from urllib.parse import quote 36 | import http.client 37 | from .luis_response import LUISResponse 38 | 39 | class LUISClient: 40 | ''' 41 | This is the interface of the LUIS 42 | Constructs a LUISClient with the corresponding user's App Id and Subscription Keys 43 | Starts the prediction procedure for the user's text, and accepts a callback function 44 | ''' 45 | _LUISURL = 'westus.api.cognitive.microsoft.com' 46 | _PredictMask = '/luis/v2.0/apps/%s?subscription-key=%s&q=%s&verbose=%s' 47 | _ReplyMask = '/luis/v2.0/apps/%s?subscription-key=%s&q=%s&contextid=%s&verbose=%s' 48 | 49 | def __init__(self, app_id, app_key, verbose=True): 50 | ''' 51 | A constructor for the LUISClient class. 52 | :param app_id: A string containing the application id. 53 | :param app_key: A string containing the subscription key. 54 | :param verbose: A boolean to indicate whether the verbose version should used or not. 55 | ''' 56 | if app_id is None: 57 | raise TypeError('NULL App Id') 58 | if not app_id: 59 | raise ValueError('Empty App Id') 60 | if ' ' in app_id: 61 | raise ValueError('Invalid App Id') 62 | if app_key is None: 63 | raise TypeError('NULL Subscription Key') 64 | if not app_key: 65 | raise ValueError('Empty Subscription Key') 66 | if ' ' in app_key: 67 | raise ValueError('Invalid Subscription Key') 68 | 69 | self._app_id = app_id 70 | self._app_key = app_key 71 | self._verbose = 'true' if verbose else 'false' 72 | 73 | def predict(self, text, response_handlers=None, daemon=False): 74 | ''' 75 | Routes the prediction routine to either sync or async 76 | based on the presence or absence of a callback fucntion. 77 | :param text: the text to be analysed and predicted. 78 | :param response_handlers: a dictionary that contains two keys on_success and on_failure, 79 | whose values are two functions to be executed if async. 80 | :param daemon: defines whether the new thread used for async will be daemon or not. 81 | :return: LUISResponse if sync, thread object to give control over the thread if async. 82 | ''' 83 | if text is None: 84 | raise TypeError('NULL text to predict') 85 | text = text.strip() 86 | if not text: 87 | raise ValueError('Empty text to predict') 88 | if response_handlers is None: 89 | return self.predict_sync(text) 90 | else: 91 | return self.predict_async(text, response_handlers, daemon) 92 | 93 | def predict_sync(self, text): 94 | ''' 95 | Predicts synchronously and returns a LUISResponse. 96 | :param text: The text to be analysed and predicted. 97 | :return: A LUISResponse object containing the response data. 98 | ''' 99 | try: 100 | conn = http.client.HTTPSConnection(self._LUISURL) 101 | conn.request('GET', self._predict_url_gen(text)) 102 | res = conn.getresponse() 103 | return LUISResponse(res.read().decode('UTF-8')) 104 | except Exception: 105 | raise 106 | 107 | def predict_async(self, text, response_handlers, daemon): 108 | ''' 109 | Predicts asynchronously and executes a callback function at the end. 110 | :param text: The text to be analysed and predicted. 111 | :param response_handlers: A dictionary that contains two keys on_success and on_failure, 112 | whose values are two functions to be executed if async. 113 | :param daemon: Defines whether the new thread will be daemon or not. 114 | :return: A thread object to give control over the thread. 115 | ''' 116 | if 'on_success' not in response_handlers: 117 | raise KeyError('You have to specify the success handler with key: "on_success"') 118 | if 'on_failure' not in response_handlers: 119 | raise KeyError('You have to specify the failure handler with key: "on_failure"') 120 | predict_thread = threading.Thread(target=self._predict_async_helper 121 | , args=(text, response_handlers)) 122 | predict_thread.daemon = daemon 123 | predict_thread.start() 124 | return predict_thread 125 | 126 | def _predict_url_gen(self, text): 127 | ''' 128 | Returns the suitable LUIS API predict url. 129 | :param text: The text to be analysed and predicted. 130 | :return: LUIS API predicton url. 131 | ''' 132 | return self._PredictMask%(self._app_id, self._app_key, quote(text), self._verbose) 133 | 134 | def _predict_async_helper(self, text, response_handlers): 135 | ''' 136 | A wrapper function to be executed asynchronously in an external thread. 137 | It executes the predict routine and then executes a callback function. 138 | :param text: The text to be analysed and predicted. 139 | :param response: A LUISResponse object that contains the context Id. 140 | :param response_handlers: A dictionary that contains two keys on_success and on_failure, 141 | whose values are two functions to be executed if async. 142 | :return: None. 143 | ''' 144 | res = None 145 | try: 146 | res = self.predict_sync(text) 147 | except Exception as exc: 148 | response_handlers['on_failure'](exc) 149 | return 150 | response_handlers['on_success'](res) 151 | 152 | def reply(self, text, response, response_handlers=None, force_set_parameter_name=None, daemon=False): 153 | ''' 154 | Routes the reply routine to either sync or async 155 | based on the presence or absence of a callback fucntion. 156 | :param text: The text to be analysed and predicted. 157 | :param response: A LUISResponse object that contains the context Id. 158 | :param response_handlers: A dictionary that contains two keys on_success and on_failure, 159 | whose values are two functions 160 | to be executed if async. 161 | :param force_set_parameter_name: The name of a parameter the needs to be reset in dialog. 162 | :param daemon: Defines whether the new thread used for async will be daemon or not. 163 | :return: A LUISResponse object if sync, a thread object to control the thread if async. 164 | ''' 165 | if text is None: 166 | raise TypeError('NULL text to predict') 167 | text = text.strip() 168 | if not text: 169 | raise ValueError('Empty text to predict') 170 | if response_handlers is None: 171 | return self.reply_sync(text, response, force_set_parameter_name) 172 | else: 173 | return self.reply_async(text, response, response_handlers, force_set_parameter_name, daemon) 174 | 175 | def reply_sync(self, text, response, force_set_parameter_name): 176 | ''' 177 | Replies synchronously and returns a LUISResponse object. 178 | :param text: The text to be analysed and predicted. 179 | :param response: A LUISResponse object that contains the context Id. 180 | :param force_set_parameter_name: The name of a parameter the needs to be reset in dialog. 181 | :return: A LUISResponse object containg the response data. 182 | ''' 183 | try: 184 | conn = http.client.HTTPSConnection(self._LUISURL) 185 | conn.request('GET', self._reply_url_gen(text, response, force_set_parameter_name)) 186 | res = conn.getresponse() 187 | return LUISResponse(res.read().decode('UTF-8')) 188 | except Exception: 189 | raise 190 | 191 | def reply_async(self, text, response, response_handlers, force_set_parameter_name, daemon): 192 | ''' 193 | Predicts asynchronously and executes a callback function at the end. 194 | :param text: The text to be analysed and predicted. 195 | :param response: A LUISResponse object that contains the context Id. 196 | :param response_handlers: A dictionary that contains two keys on_success and on_failure, 197 | whose values are two functions 198 | to be executed if async. 199 | :param force_set_parameter_name: The name of a parameter the needs to be reset in dialog. 200 | :param daemon: Defines whether the new thread used will be daemon or not. 201 | :return: A thread object to give control over the thread. 202 | ''' 203 | if 'on_success' not in response_handlers: 204 | raise KeyError('You have to specify the success handler with key: "on_success"') 205 | if 'on_failure' not in response_handlers: 206 | raise KeyError('You have to specify the failure handler with key: "on_failure"') 207 | reply_thread = threading.Thread(target=self._reply_async_helper 208 | , args=(text, response, response_handlers, force_set_parameter_name)) 209 | reply_thread.daemon = daemon 210 | reply_thread.start() 211 | return reply_thread 212 | 213 | def _reply_url_gen(self, text, response, force_set_parameter_name): 214 | ''' 215 | Generates the suitable LUIS API reply url. 216 | :param text: The text to be analysed and predicted. 217 | :param response: A LUISResponse object that contains the context Id. 218 | :param force_set_parameter_name: The name of a parameter the needs to be reset in dialog. 219 | :return: LUIS API reply url. 220 | ''' 221 | url = self._ReplyMask%(self._app_id, self._app_key, quote(text) 222 | , response.get_dialog().get_context_id(), self._verbose) 223 | if force_set_parameter_name is not None: 224 | url += '&forceset=%s'%(force_set_parameter_name) 225 | return url 226 | 227 | def _reply_async_helper(self, text, response, response_handlers): 228 | ''' 229 | A wrapper function to be executed asynchronously in an external thread. 230 | It executes the reply routine and then executes a callback function. 231 | :param text: The text to be analysed and predicted. 232 | :param response: A LUISResponse object that contains the context Id. 233 | :param response_handlers: A dictionary that contains two keys on_success and on_failure, 234 | whose values are two functions to be executed if async. 235 | :param force_set_parameter_name: The name of a parameter the needs to be reset in dialog. 236 | :return: None. 237 | ''' 238 | res = None 239 | try: 240 | res = self.reply_sync(text, response, force_set_parameter_name) 241 | except Exception as exc: 242 | response_handlers['on_failure'](exc) 243 | return 244 | response_handlers['on_success'](res) 245 | -------------------------------------------------------------------------------- /python3/luis_sdk/luis_composite_entity.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | from .luis_composite_entity_child import LUISCompositeEntityChild 35 | 36 | class LUISCompositeEntity: 37 | ''' 38 | LUIS Composite Entity Class. 39 | Describes the LUIS Composite Entity structure. 40 | ''' 41 | 42 | def __init__(self, composite_entity): 43 | ''' 44 | A constructor for the LUISCompositeEntity class. 45 | :param composite_entity: A dictionary containing the composite entity data. 46 | ''' 47 | self._parent_type = composite_entity['parentType'] 48 | self._value = composite_entity['value'] 49 | self._composite_entity_children = [] 50 | for composite_entity_child in composite_entity['children']: 51 | self._composite_entity_children.append(LUISCompositeEntityChild(composite_entity_child)) 52 | 53 | def get_parent_type(self): 54 | ''' 55 | A getter for the composite entity's parent type. 56 | :return: Composite Entity's parent type. 57 | ''' 58 | return self._parent_type 59 | 60 | def get_value(self): 61 | ''' 62 | A getter for the composite entity's value. 63 | :return: Composite Entity's value. 64 | ''' 65 | return self._value 66 | 67 | def get_children(self): 68 | ''' 69 | A getter for the composite entity's children. 70 | :return: Composite_Entity's children. 71 | ''' 72 | return self._composite_entity_children 73 | -------------------------------------------------------------------------------- /python3/luis_sdk/luis_composite_entity_child.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | class LUISCompositeEntityChild: 35 | ''' 36 | LUIS Composite Entity Child Class. 37 | Describes the LUIS Composite Entity Child structure. 38 | ''' 39 | 40 | def __init__(self, composite_entity_child): 41 | ''' 42 | A constructor for the LUISCompositeEntityChild class. 43 | :param composite_entity: A dictionary containing the composite entity child data. 44 | ''' 45 | self._type = composite_entity_child['type'] 46 | self._value = composite_entity_child['value'] 47 | 48 | def get_type(self): 49 | ''' 50 | A getter for the composite entity child's type. 51 | :return: Composite Entity Child's type. 52 | ''' 53 | return self._type 54 | 55 | def get_value(self): 56 | ''' 57 | A getter for the composite entity child's value. 58 | :return: Composite Entity Child's value. 59 | ''' 60 | return self._value 61 | -------------------------------------------------------------------------------- /python3/luis_sdk/luis_dialog.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | class LUISDialog: 35 | ''' 36 | LUIS Dialog Class. 37 | Describes the LUIS Action structure. 38 | ''' 39 | 40 | def __init__(self, dialog): 41 | ''' 42 | A constructor for the LUISDialog class. 43 | :param action: A dictionary containing the dialog data. 44 | ''' 45 | if 'prompt' in dialog: 46 | self._prompt = dialog['prompt'] 47 | else: 48 | self._prompt = None 49 | 50 | if 'parameterName' in dialog: 51 | self._parameter_name = dialog['parameterName'] 52 | else: 53 | self._parameter_name = None 54 | 55 | self._context_id = dialog['contextId'] 56 | self._status = dialog['status'] 57 | self._finished = self._status == 'Finished' 58 | 59 | def get_prompt(self): 60 | ''' 61 | A getter for the dialog's prompt. 62 | :return: Dialog's prompt. 63 | ''' 64 | return self._prompt 65 | 66 | def get_parameter_name(self): 67 | ''' 68 | A getter for the dialog's parameter name. 69 | :return: Dialog's parameter name. 70 | ''' 71 | return self._parameter_name 72 | 73 | def get_context_id(self): 74 | ''' 75 | A getter for the dialog's context Id. 76 | :return: Dialog's prompt. 77 | ''' 78 | return self._context_id 79 | 80 | def get_status(self): 81 | ''' 82 | A getter for the dialog's status. 83 | :return: Dialog's status. 84 | ''' 85 | return self._status 86 | 87 | def is_finished(self): 88 | ''' 89 | Checks whether the dialog has finished or not. 90 | :return: A boolean that expresses whether the dialog has finished or not. 91 | ''' 92 | return self._finished 93 | -------------------------------------------------------------------------------- /python3/luis_sdk/luis_entity.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | class LUISEntity: 35 | ''' 36 | LUIS Entity Class. 37 | Describes the LUIS Entity structure. 38 | ''' 39 | 40 | def __init__(self, entity): 41 | ''' 42 | A constructor for the LUISEntity class. 43 | :param entity: A dictionary containing the entity data. 44 | ''' 45 | self._name = entity['entity'] 46 | self._type = entity['type'] 47 | if 'startIndex' in entity: 48 | self._start_idx = entity['startIndex'] 49 | else: 50 | self._start_idx = None 51 | if 'endIndex' in entity: 52 | self._end_idx = entity['endIndex'] 53 | else: 54 | self._end_idx = None 55 | if 'score' in entity: 56 | self._score = entity['score'] 57 | else: 58 | self._score = None 59 | if 'resolution' in entity: 60 | self._resolution = entity['resolution'] 61 | else: 62 | self._resolution = None 63 | 64 | def get_name(self): 65 | ''' 66 | A getter for the entity's name. 67 | :return: Entity's name. 68 | ''' 69 | return self._name 70 | 71 | def get_type(self): 72 | ''' 73 | A getter for the entity's type. 74 | :return: Entity's type. 75 | ''' 76 | return self._type 77 | 78 | def get_start_idx(self): 79 | ''' 80 | A getter for the entity's start index. 81 | :return: Entity's start index. 82 | ''' 83 | return self._start_idx 84 | 85 | def get_end_idx(self): 86 | ''' 87 | A getter for the entity's end index. 88 | :return: Entity's end index. 89 | ''' 90 | return self._end_idx 91 | 92 | def get_score(self): 93 | ''' 94 | A getter for the entity's score. 95 | :return: Entity's score. 96 | ''' 97 | return self._score 98 | 99 | def get_resolution(self): 100 | ''' 101 | A getter for the entity's resolution. 102 | :return: Entity's resolution. 103 | ''' 104 | return self._resolution 105 | -------------------------------------------------------------------------------- /python3/luis_sdk/luis_intent.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | from .luis_action import LUISAction 35 | 36 | class LUISIntent: 37 | ''' 38 | LUIS Intent Class. 39 | Describes the LUIS Intent structure. 40 | ''' 41 | 42 | def __init__(self, intent): 43 | ''' 44 | LUIS Intent Class. 45 | Describes the LUIS Intent structure. 46 | ''' 47 | self._name = intent['intent'] 48 | self._score = intent['score'] 49 | self._actions = [] 50 | 51 | if 'actions' in intent: 52 | for action in intent['actions']: 53 | self._actions.append(LUISAction(action)) 54 | 55 | 56 | def get_name(self): 57 | ''' 58 | A getter for the intent's name. 59 | :return: Intent's name. 60 | ''' 61 | return self._name 62 | 63 | def get_score(self): 64 | ''' 65 | A getter for the intent's score. 66 | :return: Intent's score. 67 | ''' 68 | return self._score 69 | 70 | def get_actions(self): 71 | ''' 72 | A getter for the entity's actions. 73 | :return: A list of actions. 74 | ''' 75 | return self._actions 76 | -------------------------------------------------------------------------------- /python3/luis_sdk/luis_parameter.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | from .luis_parametervalue import LUISParameterValue 35 | 36 | class LUISParameter: 37 | ''' 38 | LUIS Parameter Class. 39 | Describes the LUIS Parameter structure. 40 | ''' 41 | 42 | def __init__(self, parameter): 43 | ''' 44 | A constructor for the LUISParameter class. 45 | :param parameter: A dictionary containing the parameter data. 46 | ''' 47 | self._name = parameter['name'] 48 | self._required = parameter['required'] 49 | self._parameter_values = [] 50 | if parameter['value']: 51 | for parameter_value in parameter['value']: 52 | self._parameter_values.append(LUISParameterValue(parameter_value)) 53 | 54 | def get_name(self): 55 | ''' 56 | A getter for the parameter's name. 57 | :return: Parameter's name. 58 | ''' 59 | return self._name 60 | 61 | def get_required(self): 62 | ''' 63 | A getter for the parameter's required flag. 64 | :return: A boolean that expresses whether the parameter is required or not. 65 | ''' 66 | return self._required 67 | 68 | def get_parameter_values(self): 69 | ''' 70 | A getter for the parameters's values. 71 | :return: A list of parameter values. 72 | ''' 73 | return self._parameter_values 74 | -------------------------------------------------------------------------------- /python3/luis_sdk/luis_parametervalue.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | class LUISParameterValue: 35 | ''' 36 | LUIS Paramater Value Class. 37 | Describes the LUIS Paramter Value structure. 38 | ''' 39 | 40 | def __init__(self, parameter_value): 41 | ''' 42 | A constructor for the LUISAction class. 43 | :param parameter_value: A dictionary containing the parameter value data. 44 | ''' 45 | self._name = parameter_value['entity'] 46 | self._type = parameter_value['type'] 47 | if 'score' in parameter_value: 48 | self._score = parameter_value['score'] 49 | else: 50 | self._score = None 51 | if 'resolution' in parameter_value: 52 | self._resolution = parameter_value['resolution'] 53 | else: 54 | self._resolution = None 55 | 56 | 57 | def get_name(self): 58 | ''' 59 | A getter for the parameter value's name. 60 | :return: Parameter value's name. 61 | ''' 62 | return self._name 63 | 64 | def get_type(self): 65 | ''' 66 | A getter for the parameter value's type. 67 | :return: Parameter value's type. 68 | ''' 69 | return self._type 70 | 71 | def get_score(self): 72 | ''' 73 | A getter for the parameter value's score. 74 | :return: Parameter value's score. 75 | ''' 76 | return self._score 77 | 78 | def get_resolution(self): 79 | ''' 80 | A getter for the parameter value's resolution. 81 | :return: Parameter value's resolution. 82 | ''' 83 | return self._resolution 84 | -------------------------------------------------------------------------------- /python3/luis_sdk/luis_response.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | import json 35 | from .luis_intent import LUISIntent 36 | from .luis_entity import LUISEntity 37 | from .luis_composite_entity import LUISCompositeEntity 38 | from .luis_dialog import LUISDialog 39 | 40 | class LUISResponse: 41 | ''' 42 | LUIS Response Class. 43 | Describes the response structure, and is the main point 44 | to access the response sent by LUIS after prediction. 45 | ''' 46 | 47 | def __init__(self, JSONResponse): 48 | ''' 49 | A constructor for the LUISResponse class. 50 | :param JSONResponse: A string containing the incoming JSON. 51 | ''' 52 | if JSONResponse is None: 53 | raise TypeError('NULL JSON response') 54 | if not JSONResponse: 55 | raise ValueError('Invalid App Id') 56 | 57 | if isinstance(JSONResponse, str): 58 | try: 59 | response = json.loads(JSONResponse) 60 | except Exception: 61 | raise Exception('Error in parsing json') 62 | else: 63 | response = JSONResponse 64 | 65 | if 'statusCode' in response: 66 | raise Exception(u'Invalid Subscription Key') 67 | 68 | self._query = response['query'] 69 | 70 | if 'dialog' in response: 71 | self._dialog = LUISDialog(response['dialog']) 72 | else: 73 | self._dialog = None 74 | 75 | self._intents = [] 76 | self._entities = [] 77 | self._composite_entities = [] 78 | 79 | self._top_scoring_intent = LUISIntent(response['topScoringIntent']) 80 | 81 | if 'intents' in response: 82 | for intent in response['intents']: 83 | self._intents.append(LUISIntent(intent)) 84 | else: 85 | self._intents.append(self._top_scoring_intent) 86 | 87 | for entity in response['entities']: 88 | self._entities.append(LUISEntity(entity)) 89 | 90 | if 'compositeEntities' in response: 91 | for composite_entity in response['compositeEntities']: 92 | self._composite_entities.append(LUISCompositeEntity(composite_entity)) 93 | 94 | def get_query(self): 95 | ''' 96 | A getter for the response's query. 97 | :return: Response's query. 98 | ''' 99 | return self._query 100 | 101 | def get_top_intent(self): 102 | ''' 103 | A getter for the response's top scoring intent. 104 | :return: Response's top scoring intent. 105 | ''' 106 | return self._top_scoring_intent 107 | 108 | def get_intents(self): 109 | ''' 110 | A getter for the response's intents. 111 | :return: A list of intents. 112 | ''' 113 | return self._intents 114 | 115 | def get_entities(self): 116 | ''' 117 | A getter for the response's entities. 118 | :return: A list of entities. 119 | ''' 120 | return self._entities 121 | 122 | def get_composite_entities(self): 123 | ''' 124 | A getter for the response's composite entities. 125 | :return: A list of composite entities. 126 | ''' 127 | return self._composite_entities 128 | 129 | def get_dialog(self): 130 | ''' 131 | A getter for the response's dialog. 132 | :return: Response's dialog. 133 | ''' 134 | return self._dialog 135 | -------------------------------------------------------------------------------- /python3/sample_app.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | from luis_sdk import LUISClient 35 | 36 | def process_res(res): 37 | ''' 38 | A function that processes the luis_response object and prints info from it. 39 | :param res: A LUISResponse object containing the response data. 40 | :return: None 41 | ''' 42 | print('---------------------------------------------') 43 | print('LUIS Response: ') 44 | print('Query: ' + res.get_query()) 45 | print('Top Scoring Intent: ' + res.get_top_intent().get_name()) 46 | if res.get_dialog() is not None: 47 | if res.get_dialog().get_prompt() is None: 48 | print('Dialog Prompt: None') 49 | else: 50 | print('Dialog Prompt: ' + res.get_dialog().get_prompt()) 51 | if res.get_dialog().get_parameter_name() is None: 52 | print('Dialog Parameter: None') 53 | else: 54 | print('Dialog Parameter Name: ' + res.get_dialog().get_parameter_name()) 55 | print('Dialog Status: ' + res.get_dialog().get_status()) 56 | print('Entities:') 57 | for entity in res.get_entities(): 58 | print('"%s":' % entity.get_name()) 59 | print('Type: %s, Score: %s' % (entity.get_type(), entity.get_score())) 60 | 61 | try: 62 | APPID = input('Please enter your app Id:\n') 63 | APPKEY = input('Please input your subscription key:\n') 64 | TEXT = input('Please input the text to predict:\n') 65 | CLIENT = LUISClient(APPID, APPKEY, True) 66 | res = CLIENT.predict(TEXT) 67 | while res.get_dialog() is not None and not res.get_dialog().is_finished(): 68 | TEXT = input('%s\n'%res.get_dialog().get_prompt()) 69 | res = CLIENT.reply(TEXT, res) 70 | process_res(res) 71 | except Exception as exc: 72 | print(exc) 73 | -------------------------------------------------------------------------------- /python3/sample_app_async.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright (c) Microsoft. All rights reserved. 3 | Licensed under the MIT license. 4 | 5 | Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services 6 | 7 | Microsoft Cognitive Services (formerly Project Oxford) GitHub: 8 | https://github.com/Microsoft/ProjectOxford-ClientSDK 9 | 10 | Copyright (c) Microsoft Corporation 11 | All rights reserved. 12 | 13 | MIT License: 14 | Permission is hereby granted, free of charge, to any person obtaining 15 | a copy of this software and associated documentation files (the 16 | "Software"), to deal in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell copies of the Software, and to 19 | permit persons to whom the Software is furnished to do so, subject to 20 | the following conditions: 21 | 22 | The above copyright notice and this permission notice shall be 23 | included in all copies or substantial portions of the Software. 24 | 25 | THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, 26 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 | ''' 33 | 34 | from luis_sdk import LUISClient 35 | 36 | def on_success(res): 37 | ''' 38 | A callback function that processes the luis_response object 39 | if the prediction succeeds. 40 | :param res: a luis_response object containing the response data. 41 | :return: None 42 | ''' 43 | print('---------------------------------------------') 44 | print('LUIS Response: ') 45 | print('Query: ' + res.get_query()) 46 | print('Top Scoring Intent: ' + res.get_top_intent().get_name()) 47 | if res.get_dialog() is not None: 48 | if res.get_dialog().get_prompt() is None: 49 | print('Dialog Prompt: None') 50 | else: 51 | print('Dialog Prompt: ' + res.get_dialog().get_prompt()) 52 | if res.get_dialog().get_parameter_name() is None: 53 | print('Dialog Parameter: None') 54 | else: 55 | print('Dialog Parameter Name: ' + res.get_dialog().get_parameter_name()) 56 | print('Dialog Status: ' + res.get_dialog().get_status()) 57 | print('Entities:') 58 | for entity in res.get_entities(): 59 | print('"%s":' % entity.get_name()) 60 | print('Type: %s, Score: %s' % (entity.get_type(), entity.get_score())) 61 | 62 | def on_failure(err): 63 | ''' 64 | A callback function that processes the error object 65 | if the prediction fails. 66 | :param err: An Exception object. 67 | :return: None 68 | ''' 69 | print(err) 70 | 71 | try: 72 | APPID = input('Please enter your app Id:\n') 73 | APPKEY = input('Please input your subscription key:\n') 74 | TEXT = input('Please input the text to predict:\n') 75 | CLIENT = LUISClient(APPID, APPKEY, True) 76 | CLIENT.predict(TEXT, {'on_success': on_success, 'on_failure': on_failure}) 77 | print('-------\nMain thread finishing!!\n-------') 78 | except Exception as exc: 79 | print(exc) 80 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | try: 2 | from setuptools import setup 3 | except ImportError: 4 | from distutils.core import setup 5 | import sys 6 | 7 | pkgdir = {'': 'python%s' % sys.version_info[0]} 8 | VERSION = '0.1' 9 | 10 | setup(name='cognitive_luis', 11 | version=VERSION, 12 | description='LUIS SDK for python', 13 | url='https://github.com/Microsoft/Cognitive-LUIS-Python', 14 | author='Ahmed El-Hinidy', 15 | author_email='t-ahelhi@microsoft.com', 16 | license='MIT', 17 | package_dir=pkgdir, 18 | packages=['luis_sdk'], 19 | zip_safe=False 20 | ) --------------------------------------------------------------------------------