13 package ai.picovoice.reactnative.rhino;
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;
24 import java.util.ArrayList;
25 import java.util.HashMap;
33 private static final String
LOG_TAG =
"PvRhino";
36 private final Map<String, Rhino>
rhinoPool =
new HashMap<String, Rhino>();
49 public void create(String accessKey, String modelPath, String contextPath, Float sensitivity, Boolean requireEndpoint, Promise promise) {
58 String handle = String.valueOf(System.identityHashCode(rhino));
61 WritableMap paramMap = Arguments.createMap();
62 paramMap.putString(
"handle", handle);
65 paramMap.putString(
"version", rhino.
getVersion());
67 promise.resolve(paramMap);
69 promise.reject(e.getClass().getSimpleName(), e.getMessage());
70 }
catch (Exception e) {
71 promise.reject(
RhinoException.class.getSimpleName(), e.getMessage());
76 public void delete(String handle) {
84 public void process(String handle, ReadableArray pcmArray, Promise promise) {
89 "Invalid Rhino handle provided to native module.");
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();
101 boolean isFinalized = rhino.
process(buffer);
103 WritableMap inferenceMap = Arguments.createMap();
104 inferenceMap.putBoolean(
"isFinalized", isFinalized);
107 promise.resolve(inferenceMap);
113 inferenceMap.putBoolean(
"isUnderstood", isUnderstood);
116 promise.resolve(inferenceMap);
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());
126 inferenceMap.putMap(
"slots", slotMap);
127 promise.resolve(inferenceMap);
130 promise.reject(e.getClass().getSimpleName(), e.getMessage());