build_xunfei_grammar.cpp
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <fstream>
4 #include <iostream>
5 #include <string>
7 #include "common_config.h"
8 #include "asr/xunfei/msp_cmn.h"
10 #include "asr/xunfei/qisr.h"
11 
12 using std::endl;
13 using std::string;
14 
16 {
17 }
18 
20 {
21 }
22 
23 // 构建离线识别语法
24 void BuildGrammar::buildGrammar(UserData* udata, const string base_path, const string grammar_build_path,
25  const string asr_res_path, const string grammar_file)
26 {
27  using std::ifstream;
28  using std::ios;
29  using std::to_string;
30 
31  memset(udata, 0, sizeof(UserData));
32  std::cout << "Start building offline grammar for recognition ..." << std::endl;
33  std::cout << "Your grammar file is read from: " << grammar_file.c_str() << std::endl;
34  std::cout << "Your asr grammar file will save at: " << grammar_build_path.c_str() << std::endl;
35  // 将语法文件 grammar.bnf 内容读入 char* grm_content 中,并存储文件大小
36  char* grm_content;
37  int grm_cnt_len;
38  std::ifstream infile(grammar_file.c_str());
39  if (infile.is_open())
40  {
41  infile.seekg(0, std::ios::end);
42  grm_cnt_len = infile.tellg();
43  infile.seekg(0, std::ios::beg);
44  grm_content = new char[grm_cnt_len];
45  infile.read(grm_content, grm_cnt_len);
46  infile.close();
47  }
48  else
49  {
50  std::cout << "Fail to open expected Json file" << grammar_file << endl;
52  }
53 
54  // 构建 grm_build_param 参数
55  string tmp = string("engine_type = local, asr_res_path = " + asr_res_path + ", sample_rate = " +
56  to_string(SAMPLE_RATE_16K) + ", grm_build_path = " + grammar_build_path + ", ");
57  const char* grm_build_params = tmp.c_str();
58  std::cout << grm_build_params << std::endl;
59 
60  // QISRBuildGrammar 构建语法,生成语法ID。
61  // 返回: 构建成功返回0 否则返回错误码
62  // 参数:
63  // grammarType[in] - 语法类型,在线识别采用 abnf 格式,离线识别采用 bnf
64  // 格式。
65  // grammarContent[in] - 语法内容。
66  // grammarLength[in] - 语法长度。
67  // params[in] - 传入的参数
68  // http://mscdoc.xfyun.cn/windows/api/iFlytekMSCReferenceManual/qisr_8h.html#a1895f14ba0cfec5b9504890100c41652
69  // callback[in] - 构建语法回调接口。
70  // typedef int ( GrammarCallBack)( int errorCode,
71  // const char info, void* userData);
72  // userData[in/out] - 用户数据。
73  int ret_config =
74  QISRBuildGrammar("bnf", grm_content, grm_cnt_len, grm_build_params, BuildGrammar::buildGrammarCallback, udata);
75 
76  // 判断
77  if (MSP_SUCCESS != ret_config)
78  {
79  std::cout << "Building grammar failed!" << std::endl;
81  }
82  while (BUILD_FINISH != udata->build_fini)
83  {
84  usleep(300 * 1000);
85  }
86  if (MSP_SUCCESS != udata->errcode)
87  {
88  exit(MSP_SUCCESS);
89  }
90  // save ret_config into a file
91  string filepath = grammar_build_path + "/info.txt";
92  writeIntoFile(filepath, udata);
93  std::cout << "Build grammar finished!" << std::endl;
94 }
95 
96 // 构建离线识别语法的回调函数
97 int BuildGrammar::buildGrammarCallback(int ecode, const char* info, void* udata)
98 {
99  UserData* grm_data = (UserData*)udata;
100  if (NULL != grm_data)
101  {
102  grm_data->build_fini = 1;
103  grm_data->errcode = ecode;
104  }
105 
106  if (MSP_SUCCESS == ecode && NULL != info)
107  {
108  std::cout << "Build grammar success! Grammar ID: " << info << std::endl;
109  if (NULL != grm_data)
110  {
111  strcpy(grm_data->grammar_id, info);
112  }
113  }
114  else
115  {
116  std::cout << "Build grammar failed! Error code:" << ecode << std::endl;
117  }
118  return 0;
119 }
120 
121 int BuildGrammar::writeIntoFile(const string file_path, UserData* info)
122 {
123  using std::ofstream;
124 
125  std::ofstream outfile(file_path);
126 
127  if (outfile.is_open())
128  {
129  outfile << info->build_fini << '\n';
130  outfile << info->update_fini << '\n';
131  outfile << info->errcode << '\n';
132  outfile << info->grammar_id << '\n';
133  outfile.close();
134  }
135  else
136  {
137  std::cout << "Fail to open expected Json file" << file_path << std::endl;
139  }
140 
141  return 0;
142 }
std::string grammar_build_path
Definition: BuildGrammar.h:41
static const int SAMPLE_RATE_16K
Definition: BuildGrammar.h:20
int update_fini
Definition: BuildGrammar.h:32
int MSPAPI QISRBuildGrammar(const char *grammarType, const char *grammarContent, unsigned int grammarLength, const char *params, GrammarCallBack callback, void *userData)
std::string grammar_file
Definition: BuildGrammar.h:43
Mobile Speech Platform Common Interface Header File.
iFLY Speech Recognizer Header File
std::string asr_res_path
Definition: BuildGrammar.h:42
char grammar_id[MAX_GRAMMARID_LEN]
Definition: BuildGrammar.h:34
static int buildGrammarCallback(int ecode, const char *info, void *udata)
std::string base_path
Definition: BuildGrammar.h:40
int build_fini
Definition: BuildGrammar.h:31
static const int BUILD_FINISH
Definition: BuildGrammar.h:24
void buildGrammar(UserData *udata, const string base_path, const string grammar_build_path, const string asr_res_path, const string grammar_file)
static int writeIntoFile(const string file_path, UserData *info)


xbot_talker
Author(s): wangxiaoyun
autogenerated on Sat Oct 10 2020 03:27:53