rhino_recognizer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2021, Rein Appeldoorn
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #include <algorithm>
19 #include <stdexcept>
20 #include <iostream>
21 #include <yaml-cpp/yaml.h>
22 
23 #include "./rhino_recognizer.h"
24 
25 namespace picovoice_driver
26 {
27 std::ostream& operator<<(std::ostream& os, const RhinoRecognizerData::Parameters& p)
28 {
29  os << "Parameters(access_key=" << p.access_key_ << ", model_path=" << p.model_path_
30  << ", context_path=" << p.context_path_ << ", sensitivity=" << p.sensitivity_ << ")";
31  return os;
32 }
33 
34 RhinoRecognizerData::Result::KeyValue::KeyValue(const std::string& key, const std::string& value)
35  : key_(key), value_(value)
36 {
37 }
38 
39 std::ostream& operator<<(std::ostream& os, const RhinoRecognizerData::Result::KeyValue& kv)
40 {
41  os << "{" << kv.key_ << "=" << kv.value_ << "}";
42  return os;
43 }
44 
45 std::ostream& operator<<(std::ostream& os, const RhinoRecognizerData::Result& r)
46 {
47  os << "Result(is_understood=" << r.is_understood_ << ", intent=" << r.intent_ << ", slots=" << toString(r.slots_)
48  << ")";
49  return os;
50 }
51 
53 {
54  if (rhino_ != NULL)
55  {
56  pv_rhino_delete(rhino_);
57  }
58 }
59 
61 {
62  if (rhino_ != NULL)
63  {
64  pv_rhino_delete(rhino_);
65  }
66 
67  pv_status_t status =
68  pv_rhino_init(parameters.access_key_.data(), parameters.model_path_.data(), parameters.context_path_.data(),
69  static_cast<float>(parameters.sensitivity_), parameters.require_endpoint_, &rhino_);
70  if (status != PV_STATUS_SUCCESS)
71  {
72  throw std::runtime_error("Failed to initialize picovoice rhino with parameters " + toString(parameters) + ": " +
73  std::string(pv_status_to_string(status)));
74  }
75  const char* context_info = NULL;
76  status = pv_rhino_context_info(rhino_, &context_info);
77  if (status != PV_STATUS_SUCCESS)
78  {
79  throw std::runtime_error("Failed to get rhino context info: " + std::string(pv_status_to_string(status)));
80  }
81 
82  auto yaml = YAML::Load(context_info);
83  if (!yaml["context"])
84  {
85  throw std::runtime_error("Context missing key 'context': " + std::string(context_info));
86  }
87  if (!yaml["context"]["expressions"])
88  {
89  throw std::runtime_error("Context missing key 'context/expressions': " + std::string(context_info));
90  }
91 
92  std::vector<std::string> context_intents;
93  for (const auto& kv : yaml["context"]["expressions"])
94  {
95  context_intents.push_back(kv.first.as<std::string>());
96  }
97 
98  for (const auto& intent : parameters.intents_)
99  {
100  if (std::find(context_intents.begin(), context_intents.end(), intent) == context_intents.end())
101  {
102  throw std::runtime_error("Intent '" + intent +
103  "' not available in context. Available intents: " + toString(context_intents));
104  }
105  }
106 
107  intents_ = parameters.intents_;
108 }
109 
111 {
112  return result_;
113 }
114 
116 {
119  settings.sample_rate_ = pv_sample_rate();
120  return settings;
121 }
122 
124 {
125  result_ = RhinoRecognizerData::Result();
126  pv_rhino_reset(rhino_);
127 }
128 
130 {
131  bool is_finalized = false;
132  auto process_status = pv_rhino_process(rhino_, frames, &is_finalized);
133  if (process_status != PV_STATUS_SUCCESS)
134  {
135  throw std::runtime_error("Rhino process failed: " + std::string(pv_status_to_string(process_status)));
136  }
137 
138  if (is_finalized)
139  {
140  bool is_understood;
141  auto is_understood_status = pv_rhino_is_understood(rhino_, &is_understood);
142  if (is_understood_status != PV_STATUS_SUCCESS)
143  {
144  throw std::runtime_error("Rhino is understood failed: " + std::string(pv_status_to_string(is_understood_status)));
145  }
146 
147  if (!is_understood)
148  {
149  pv_rhino_reset(rhino_);
150  return false;
151  }
152 
153  const char* intent_char_array = NULL;
154  int32_t num_slots = 0;
155  const char** slots = NULL;
156  const char** values = NULL;
157 
158  auto intent_status = pv_rhino_get_intent(rhino_, &intent_char_array, &num_slots, &slots, &values);
159  if (intent_status != PV_STATUS_SUCCESS)
160  {
161  throw std::runtime_error("Rhino get intent failed: " + std::string(pv_status_to_string(is_understood_status)));
162  }
163 
164  std::string intent(intent_char_array);
165  if (!intents_.empty() && std::find(intents_.begin(), intents_.end(), std::string(intent)) == intents_.end())
166  {
167  pv_rhino_reset(rhino_);
168  return false;
169  }
170 
171  result_.is_understood_ = is_understood;
172  result_.intent_ = intent;
173  for (int32_t i = 0; i < num_slots; ++i)
174  {
175  result_.slots_.emplace_back(std::string(slots[i]), std::string(values[i]));
176  }
177  return true;
178  }
179  return false;
180 }
181 } // namespace picovoice_driver
picovoice_driver::RhinoRecognizerData::Result
Definition: rhino_recognizer.h:57
rhino_recognizer.h
NULL
#define NULL
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/speex_resampler/thirdparty/resample.c:92
picovoice_driver::RhinoRecognizerData::Parameters::sensitivity_
double sensitivity_
sensitivity_ Recognizer sensitivity
Definition: rhino_recognizer.h:54
pv_rhino_get_intent
PV_API pv_status_t pv_rhino_get_intent(const pv_rhino_t *object, const char **intent, int32_t *num_slots, const char ***slots, const char ***values)
picovoice_driver::operator<<
std::ostream & operator<<(std::ostream &os, const PorcupineRecognizerData::Parameters &p)
Definition: porcupine_recognizer.cpp:25
pv_rhino_process
PV_API pv_status_t pv_rhino_process(pv_rhino_t *object, const int16_t *pcm, bool *is_finalized)
picovoice_driver::RhinoRecognizer::configure
void configure(const RhinoRecognizerData::Parameters &parameters) override
Definition: rhino_recognizer.cpp:60
picovoice_driver::RhinoRecognizerData::Result::KeyValue::value_
std::string value_
Definition: rhino_recognizer.h:74
picovoice_driver::RhinoRecognizerData::Parameters::require_endpoint_
bool require_endpoint_
require_endpoint_ If true, Rhino requires an endpoint (chunk of silence) before finishing inference.
Definition: rhino_recognizer.h:49
pv_rhino_frame_length
PV_API int32_t pv_rhino_frame_length(void)
picovoice_driver::RecognizerData::Parameters::model_path_
std::string model_path_
model_path_ Path to the Picovoice model parameters
Definition: recognizer.h:38
picovoice_driver::RhinoRecognizer::getResult
RhinoRecognizerData::Result getResult() override
getResult Get the recognition result
Definition: rhino_recognizer.cpp:110
picovoice_driver::RhinoRecognizer::recognizeInit
void recognizeInit() override
Definition: rhino_recognizer.cpp:123
pv_rhino_delete
PV_API void pv_rhino_delete(pv_rhino_t *object)
PV_STATUS_SUCCESS
@ PV_STATUS_SUCCESS
Definition: porcupine/include/picovoice.h:34
pv_status_t
pv_status_t
Definition: porcupine/include/picovoice.h:33
pv_rhino_init
PV_API pv_status_t pv_rhino_init(const char *access_key, const char *model_path, const char *context_path, float sensitivity, bool require_endpoint, pv_rhino_t **object)
picovoice_driver::RhinoRecognizerData::Parameters
Definition: rhino_recognizer.h:34
picovoice_driver
Definition: porcupine_node.cpp:24
picovoice_driver::Recognizer::RecordSettings::frame_length_
size_t frame_length_
Definition: recognizer.h:80
pv_sample_rate
PV_API int32_t pv_sample_rate(void)
picovoice_driver::RhinoRecognizerData::Result::slots_
std::vector< KeyValue > slots_
slots_ The recognized intent slots
Definition: rhino_recognizer.h:80
picovoice_driver::RhinoRecognizer::recognizeProcess
bool recognizeProcess(int16_t *frames) override
Definition: rhino_recognizer.cpp:129
pv_rhino_reset
PV_API pv_status_t pv_rhino_reset(pv_rhino_t *object)
picovoice_driver::RhinoRecognizerData::Result::is_understood_
bool is_understood_
is_understood_ Whether the recognizer understood an intent
Definition: rhino_recognizer.h:62
picovoice_driver::Recognizer::RecordSettings::sample_rate_
size_t sample_rate_
Definition: recognizer.h:79
pv_rhino_context_info
PV_API pv_status_t pv_rhino_context_info(const pv_rhino_t *object, const char **context_info)
picovoice_driver::RhinoRecognizerData::Result::KeyValue::KeyValue
KeyValue(const std::string &key, const std::string &value)
Definition: rhino_recognizer.cpp:34
pv_rhino_is_understood
PV_API pv_status_t pv_rhino_is_understood(const pv_rhino_t *object, bool *is_understood)
picovoice_driver::RhinoRecognizer::~RhinoRecognizer
~RhinoRecognizer()
Definition: rhino_recognizer.cpp:52
picovoice_driver::Recognizer::RecordSettings
Definition: recognizer.h:77
picovoice_driver::RhinoRecognizerData::Parameters::context_path_
std::string context_path_
context_path_ Path to the Picovoice Rhino context.rhn
Definition: rhino_recognizer.h:39
picovoice_driver::RhinoRecognizerData::Result::KeyValue
Definition: rhino_recognizer.h:69
picovoice_driver::RhinoRecognizerData::Result::intent_
std::string intent_
intent_ The recognized intent
Definition: rhino_recognizer.h:67
picovoice_driver::toString
std::string toString(const T &v)
Definition: util.h:28
picovoice_driver::RhinoRecognizer::getRecordSettings
RecordSettings getRecordSettings() override
Definition: rhino_recognizer.cpp:115
pv_status_to_string
const PV_API char * pv_status_to_string(pv_status_t status)
picovoice_driver::RhinoRecognizerData::Parameters::intents_
std::vector< std::string > intents_
intents_ Indent candidates, if empty, all returned intents will be considered valid
Definition: rhino_recognizer.h:44
picovoice_driver::RecognizerData::Parameters::access_key_
std::string access_key_
access_key_ Picovoice access key
Definition: recognizer.h:33
picovoice_driver::RhinoRecognizerData::Result::KeyValue::key_
std::string key_
Definition: rhino_recognizer.h:73


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