| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- #include "Util.h"
- #include <chrono>
- #include <sstream>
- #include "tinyxml.h"
- #include <regex>
- #include <locale>
- #include <codecvt>
-
- /*
- <?xml version="1.0" encoding="utf-8"?>
<result>
- <interpretation grammar="session:hello" confidence="1">
- <instance>
- <result>也不准确。</result>
- <beginTime>2420</beginTime>
- <endTime>3540</endTime>
- <taskId>fba26d3861574ac4b55c01f4881c5bae</taskId>
- <waveformUri>b3f91c673b9146b7-7.wav</waveformUri>
- </instance>
- <input mode="speech">也不准确。</input>
- </interpretation>
- </result>
- */
- bool parseASRXml(const char* desc, std::string& result, std::string& error)
- {
- try {
- TiXmlDocument doc;
- doc.Parse(desc);
- auto pRoot = doc.RootElement();
- if (pRoot != nullptr && pRoot->ValueStr() == "result") {
- auto pElement = pRoot->FirstChildElement("interpretation");
- if (pElement) {
- auto pInput = pElement->FirstChildElement("input");
- if (pInput) {
- auto pText = pInput->GetText();
- result = pText != nullptr ? pText : "";
- return true;
- }
- }
- }
- }
- catch (const std::exception& e) {
- error = e.what();
- }
- return false;
- }
-
- int64_t GetUnixTime()
- {
- int64_t times = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
- return times;
- }
-
- std::string std::to_string(const char * src)
- {
- if (src == nullptr)return "";
- std::stringstream ss;
- ss << src;
- std::string str;
- ss >> str;
- return str;
- }
-
- std::wstring string2wstring(const std::string& s) {
- // 转换为宽字符串
- std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
- std::wstring wstr = converter.from_bytes(s);
- return wstr;
- }
-
- std::string wstring2string(const std::wstring& ws) {
- // 转换为窄字符串
- std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
- std::string str = converter.to_bytes(ws);
- return str;
- }
-
- std::string filter(const std::string& s) {
- std::string result;
- result = s;
- auto ws = string2wstring(result);
- auto result1 = std::regex_replace(ws, std::wregex(L"[,|。|!|;|:|,|\\.]"), L"");
- result = wstring2string(result1);
- return result;
- }
|