RhinoModule.java
Go to the documentation of this file.
1 /*
2  Copyright 2020-2021 Picovoice Inc.
3 
4  You may not use this file except in compliance with the license. A copy of the license is
5  located in the "LICENSE" file accompanying this source.
6 
7  Unless required by applicable law or agreed to in writing, software distributed under the
8  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
9  express or implied. See the License for the specific language governing permissions and
10  limitations under the License.
11 */
12 
13 package ai.picovoice.reactnative.rhino;
14 
15 import com.facebook.react.bridge.Arguments;
16 import com.facebook.react.bridge.Promise;
17 import com.facebook.react.bridge.ReactApplicationContext;
18 import com.facebook.react.bridge.ReactContextBaseJavaModule;
19 import com.facebook.react.bridge.ReactMethod;
20 import com.facebook.react.bridge.ReadableArray;
21 import com.facebook.react.bridge.WritableMap;
22 
23 import java.io.File;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.Map;
27 
28 import ai.picovoice.rhino.*;
29 
30 
31 public class RhinoModule extends ReactContextBaseJavaModule {
32 
33  private static final String LOG_TAG = "PvRhino";
34 
35  private final ReactApplicationContext reactContext;
36  private final Map<String, Rhino> rhinoPool = new HashMap<String, Rhino>();
37 
38  public RhinoModule(ReactApplicationContext reactContext) {
39  super(reactContext);
40  this.reactContext = reactContext;
41  }
42 
43  @Override
44  public String getName() {
45  return "PvRhino";
46  }
47 
48  @ReactMethod
49  public void create(String accessKey, String modelPath, String contextPath, Float sensitivity, Boolean requireEndpoint, Promise promise) {
50  try {
51  Rhino rhino = new Rhino.Builder()
52  .setAccessKey(accessKey)
53  .setModelPath(modelPath)
54  .setContextPath(contextPath)
55  .setSensitivity(sensitivity)
56  .setRequireEndpoint(requireEndpoint)
58  String handle = String.valueOf(System.identityHashCode(rhino));
59  rhinoPool.put(handle, rhino);
60 
61  WritableMap paramMap = Arguments.createMap();
62  paramMap.putString("handle", handle);
63  paramMap.putInt("frameLength", rhino.getFrameLength());
64  paramMap.putInt("sampleRate", rhino.getSampleRate());
65  paramMap.putString("version", rhino.getVersion());
66  paramMap.putString("contextInfo", rhino.getContextInformation());
67  promise.resolve(paramMap);
68  } catch (RhinoException e) {
69  promise.reject(e.getClass().getSimpleName(), e.getMessage());
70  } catch (Exception e) {
71  promise.reject(RhinoException.class.getSimpleName(), e.getMessage());
72  }
73  }
74 
75  @ReactMethod
76  public void delete(String handle) {
77  if (rhinoPool.containsKey(handle)) {
78  rhinoPool.get(handle).delete();
79  rhinoPool.remove(handle);
80  }
81  }
82 
83  @ReactMethod
84  public void process(String handle, ReadableArray pcmArray, Promise promise) {
85  try {
86  if (!rhinoPool.containsKey(handle)) {
87  promise.reject(
88  RhinoInvalidStateException.class.getSimpleName(),
89  "Invalid Rhino handle provided to native module.");
90  return;
91  }
92 
93  Rhino rhino = rhinoPool.get(handle);
94 
95  ArrayList<Object> pcmArrayList = pcmArray.toArrayList();
96  short[] buffer = new short[pcmArray.size()];
97  for (int i = 0; i < pcmArray.size(); i++) {
98  buffer[i] = ((Number) pcmArrayList.get(i)).shortValue();
99  }
100 
101  boolean isFinalized = rhino.process(buffer);
102 
103  WritableMap inferenceMap = Arguments.createMap();
104  inferenceMap.putBoolean("isFinalized", isFinalized);
105 
106  if(!isFinalized){
107  promise.resolve(inferenceMap);
108  return;
109  }
110 
111  RhinoInference inference = rhino.getInference();
112  boolean isUnderstood = inference.getIsUnderstood();
113  inferenceMap.putBoolean("isUnderstood", isUnderstood);
114 
115  if(!isUnderstood){
116  promise.resolve(inferenceMap);
117  return;
118  }
119 
120  inferenceMap.putString("intent", inference.getIntent());
121  final Map<String, String> slots = inference.getSlots();
122  WritableMap slotMap = Arguments.createMap();
123  for (Map.Entry<String, String> slot : slots.entrySet()) {
124  slotMap.putString(slot.getKey(), slot.getValue());
125  }
126  inferenceMap.putMap("slots", slotMap);
127  promise.resolve(inferenceMap);
128 
129  } catch (RhinoException e) {
130  promise.reject(e.getClass().getSimpleName(), e.getMessage());
131  }
132  }
133 }
ai.picovoice.rhino.RhinoException
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/exception/RhinoException.java:14
ai.picovoice.rhino.Rhino.process
boolean process(short[] pcm)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:90
ai.picovoice.rhino.Rhino.Builder.setSensitivity
Builder setSensitivity(float sensitivity)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:221
ai.picovoice.rhino.RhinoInference.getIntent
String getIntent()
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/RhinoInference.java:30
ai.picovoice.rhino.Rhino.Builder.setContextPath
Builder setContextPath(String contextPath)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:216
ai.picovoice.reactnative.rhino.RhinoModule.create
void create(String accessKey, String modelPath, String contextPath, Float sensitivity, Boolean requireEndpoint, Promise promise)
Definition: RhinoModule.java:49
ai.picovoice.rhino.Rhino.Builder.setAccessKey
Builder setAccessKey(String accessKey)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:206
ai.picovoice.rhino.RhinoInvalidStateException
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/exception/RhinoInvalidStateException.java:13
ai.picovoice.rhino.Rhino.getSampleRate
native int getSampleRate()
ai.picovoice.reactnative.rhino.RhinoModule
Definition: RhinoModule.java:31
ai.picovoice.reactnative.rhino.RhinoModule.LOG_TAG
static final String LOG_TAG
Definition: RhinoModule.java:33
ai.picovoice.reactnative.rhino.RhinoModule.getName
String getName()
Definition: RhinoModule.java:44
ai.picovoice.reactnative.rhino.RhinoModule.reactContext
final ReactApplicationContext reactContext
Definition: RhinoModule.java:35
ai.picovoice.rhino.RhinoInference
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/RhinoInference.java:15
ai
ai.picovoice.rhino
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/exception/RhinoActivationException.java:11
ai.picovoice.rhino.Rhino.getVersion
native String getVersion()
ai.picovoice
ai.picovoice.rhino.Rhino
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:35
ai.picovoice.rhino.Rhino.Builder.setRequireEndpoint
Builder setRequireEndpoint(boolean requireEndpoint)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:226
ai.picovoice.rhino.Rhino.Builder.setModelPath
Builder setModelPath(String modelPath)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:211
ai.picovoice.rhino.Rhino.Builder
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:199
ai.picovoice.rhino.Rhino.getInference
RhinoInference getInference()
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:120
ai.picovoice.rhino.RhinoInference.getIsUnderstood
boolean getIsUnderstood()
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/RhinoInference.java:26
ai.picovoice.reactnative.rhino.RhinoModule.rhinoPool
final Map< String, Rhino > rhinoPool
Definition: RhinoModule.java:36
ai.picovoice.reactnative.rhino.RhinoModule.RhinoModule
RhinoModule(ReactApplicationContext reactContext)
Definition: RhinoModule.java:38
ai.picovoice.rhino.Rhino.Builder.build
Rhino build(Context context)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:266
ai.picovoice.rhino.Rhino.getFrameLength
native int getFrameLength()
ai.picovoice.reactnative.rhino.RhinoModule.process
void process(String handle, ReadableArray pcmArray, Promise promise)
Definition: RhinoModule.java:84
ai.picovoice.rhino.RhinoInference.getSlots
Map< String, String > getSlots()
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/RhinoInference.java:34
ai.picovoice.rhino.Rhino.getContextInformation
String getContextInformation()
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:157


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:14:50