44
55import json
66from builtins import object , str
7- from ctypes import byref , c_char_p , c_void_p , pointer , string_at , c_char , c_int
7+ from ctypes import byref , c_char_p , c_void_p , string_at , c_char , c_int
88from pathlib import Path
99
10- from snips_nlu_rust .utils import lib , string_pointer , CStringArray
10+ from snips_nlu_rust .utils import (
11+ lib , string_pointer , CStringArray , check_ffi_error )
1112
1213
1314class NLUEngine (object ):
@@ -32,69 +33,73 @@ class NLUEngine(object):
3233 ... engine_dir="/path/to/nlu_engine")
3334 >>> inference_engine.parse("Turn on the lights in the kitchen")
3435 """
36+
3537 def __init__ (self , engine_dir = None , engine_bytes = None ):
36- exit_code = 1
3738 self ._engine = None
3839
3940 if engine_dir is None and engine_bytes is None :
4041 raise ValueError ("Please specify engine_dir or engine_bytes" )
4142
4243 if engine_dir is not None :
4344 engine_dir = Path (engine_dir )
44- if not engine_dir .is_dir ():
45- raise OSError ("NLU engine directory not found: %s"
46- % str (engine_dir ))
47- self ._engine = pointer (c_void_p ())
45+ self ._engine = c_void_p ()
4846 exit_code = lib .ffi_snips_nlu_engine_create_from_dir (
4947 str (engine_dir ).encode ("utf8" ), byref (self ._engine ))
50- elif engine_bytes is not None :
51- self ._engine = pointer (c_void_p ())
48+ err_msg = "Something went wrong when creating the engine from " \
49+ "directory"
50+
51+ else :
52+ self ._engine = c_void_p ()
5253 bytearray_type = c_char * len (engine_bytes )
5354 exit_code = lib .ffi_snips_nlu_engine_create_from_zip (
5455 bytearray_type .from_buffer (engine_bytes ), len (engine_bytes ),
5556 byref (self ._engine ))
57+ err_msg = "Something went wrong when creating the engine from " \
58+ "bytes"
5659
57- if exit_code :
58- raise ImportError ('Something wrong happened while creating the '
59- 'intent parser. See stderr.' )
60+ check_ffi_error (exit_code , err_msg )
6061
6162 def parse (self , query , intents_whitelist = None , intents_blacklist = None ):
6263 """Extracts intent and slots from an input query
6364
6465 Args:
6566 query (str): input to process
66- intents_whitelist (list of str, optional): if defined, this will restrict the scope of
67- intent parsing to the provided intents
68- intents_blacklist (list of str, optional): if defined, these intents will be excluded
69- from the scope of intent parsing
67+ intents_whitelist (list of str, optional): if defined, this will
68+ restrict the scope of intent parsing to the provided intents
69+ intents_blacklist (list of str, optional): if defined, these
70+ intents will be excluded from the scope of intent parsing
7071
7172 Returns:
7273 A python dict containing data about intent and slots. See
73- https://snips-nlu.readthedocs.io/en/latest/tutorial.html#parsing for details about the
74- format.
74+ https://snips-nlu.readthedocs.io/en/latest/tutorial.html#parsing
75+ for details about the format.
7576 """
7677 if intents_whitelist is not None :
77- if not all (isinstance (intent , str ) for intent in intents_whitelist ):
78- raise TypeError (
79- "Expected 'intents_whitelist' to contain objects of type 'str'" )
78+ if not all (
79+ isinstance (intent , str ) for intent in intents_whitelist ):
80+ raise TypeError ("Expected 'intents_whitelist' to contain "
81+ "objects of type 'str'" )
8082 intents = [intent .encode ("utf8" ) for intent in intents_whitelist ]
8183 arr = CStringArray ()
8284 arr .size = c_int (len (intents ))
8385 arr .data = (c_char_p * len (intents ))(* intents )
8486 intents_whitelist = byref (arr )
8587 if intents_blacklist is not None :
86- if not all (isinstance (intent , str ) for intent in intents_blacklist ):
87- raise TypeError (
88- "Expected 'intents_blacklist' to contain objects of type 'str'" )
88+ if not all (
89+ isinstance (intent , str ) for intent in intents_blacklist ):
90+ raise TypeError ("Expected 'intents_blacklist' to contain "
91+ "objects of type 'str'" )
8992 intents = [intent .encode ("utf8" ) for intent in intents_blacklist ]
9093 arr = CStringArray ()
9194 arr .size = c_int (len (intents ))
9295 arr .data = (c_char_p * len (intents ))(* intents )
9396 intents_blacklist = byref (arr )
9497 with string_pointer (c_char_p ()) as ptr :
95- lib .ffi_snips_nlu_engine_run_parse_into_json (
96- self ._engine , query .encode ("utf8" ), intents_whitelist , intents_blacklist ,
97- byref (ptr ))
98+ exit_code = lib .ffi_snips_nlu_engine_run_parse_into_json (
99+ self ._engine , query .encode ("utf8" ), intents_whitelist ,
100+ intents_blacklist , byref (ptr ))
101+ msg = "Something went wrong when parsing query '%s'" % query
102+ check_ffi_error (exit_code , msg )
98103 result = string_at (ptr )
99104
100105 return json .loads (result .decode ("utf8" ))
@@ -108,13 +113,18 @@ def get_slots(self, query, intent):
108113
109114 Returns:
110115 A list of slots. See
111- https://snips-nlu.readthedocs.io/en/latest/tutorial.html#parsing for details about the
112- format.
116+ https://snips-nlu.readthedocs.io/en/latest/tutorial.html#parsing
117+ for details about the format.
113118 """
114119 with string_pointer (c_char_p ()) as ptr :
115- lib .ffi_snips_nlu_engine_run_get_slots_into_json (
116- self ._engine , query .encode ("utf8" ), intent .encode ("utf8" ), byref (ptr ))
120+ exit_code = lib .ffi_snips_nlu_engine_run_get_slots_into_json (
121+ self ._engine , query .encode ("utf8" ), intent .encode ("utf8" ),
122+ byref (ptr ))
123+ msg = "Something went wrong when extracting slots from query " \
124+ "'%s' with intent '%s'" % (query , intent )
125+ check_ffi_error (exit_code , msg )
117126 result = string_at (ptr )
127+
118128 return json .loads (result .decode ("utf8" ))
119129
120130 def get_intents (self , query ):
@@ -125,12 +135,15 @@ def get_intents(self, query):
125135
126136 Returns:
127137 A list of intents along with their probability. See
128- https://snips-nlu.readthedocs.io/en/latest/tutorial.html#parsing for details about the
129- format.
138+ https://snips-nlu.readthedocs.io/en/latest/tutorial.html#parsing
139+ for details about the format.
130140 """
131141 with string_pointer (c_char_p ()) as ptr :
132- lib .ffi_snips_nlu_engine_run_get_intents_into_json (
142+ exit_code = lib .ffi_snips_nlu_engine_run_get_intents_into_json (
133143 self ._engine , query .encode ("utf8" ), byref (ptr ))
144+ msg = "Something went wrong when extracting intents from query " \
145+ "'%s'" % query
146+ check_ffi_error (exit_code , msg )
134147 result = string_at (ptr )
135148 return json .loads (result .decode ("utf8" ))
136149
0 commit comments