linux版本中间件

Util.cpp 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "Util.h"
  2. #include <chrono>
  3. #include <sstream>
  4. #include "tinyxml.h"
  5. #include <regex>
  6. #include <locale>
  7. #include <codecvt>
  8. /*
  9. <?xml version="1.0" encoding="utf-8"?> <result>
  10. <interpretation grammar="session:hello" confidence="1">
  11. <instance>
  12. <result>也不准确。</result>
  13. <beginTime>2420</beginTime>
  14. <endTime>3540</endTime>
  15. <taskId>fba26d3861574ac4b55c01f4881c5bae</taskId>
  16. <waveformUri>b3f91c673b9146b7-7.wav</waveformUri>
  17. </instance>
  18. <input mode="speech">也不准确。</input>
  19. </interpretation>
  20. </result>
  21. */
  22. bool parseASRXml(const char* desc, std::string& result, std::string& error)
  23. {
  24. try {
  25. TiXmlDocument doc;
  26. doc.Parse(desc);
  27. auto pRoot = doc.RootElement();
  28. if (pRoot != nullptr && pRoot->ValueStr() == "result") {
  29. auto pElement = pRoot->FirstChildElement("interpretation");
  30. if (pElement) {
  31. auto pInput = pElement->FirstChildElement("input");
  32. if (pInput) {
  33. auto pText = pInput->GetText();
  34. result = pText != nullptr ? pText : "";
  35. return true;
  36. }
  37. }
  38. }
  39. }
  40. catch (const std::exception& e) {
  41. error = e.what();
  42. }
  43. return false;
  44. }
  45. int64_t GetUnixTime()
  46. {
  47. int64_t times = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  48. return times;
  49. }
  50. std::string std::to_string(const char * src)
  51. {
  52. if (src == nullptr)return "";
  53. std::stringstream ss;
  54. ss << src;
  55. std::string str;
  56. ss >> str;
  57. return str;
  58. }
  59. std::wstring string2wstring(const std::string& s) {
  60. // 转换为宽字符串
  61. std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
  62. std::wstring wstr = converter.from_bytes(s);
  63. return wstr;
  64. }
  65. std::string wstring2string(const std::wstring& ws) {
  66. // 转换为窄字符串
  67. std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
  68. std::string str = converter.to_bytes(ws);
  69. return str;
  70. }
  71. std::string filter(const std::string& s) {
  72. std::string result;
  73. result = s;
  74. auto ws = string2wstring(result);
  75. auto result1 = std::regex_replace(ws, std::wregex(L"[,|。|!|;|:|,|\\.]"), L"");
  76. result = wstring2string(result1);
  77. return result;
  78. }