my_util.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import sys
  2. import os
  3. import json
  4. def get_home_env():
  5. if sys.platform == "win32":
  6. return 'APPDATA'
  7. return 'HOME'
  8. def gen_home_doc_path():
  9. if sys.platform == "win32":
  10. import ctypes.wintypes
  11. path_id = 5 # 5:文档 0:桌面
  12. buf = ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH)
  13. ctypes.windll.shell32.SHGetFolderPathW(None, path_id, None, 0, buf)
  14. path = buf.value
  15. elif sys.platform == "darwin":
  16. path = "%s/.iflytek" % (os.environ[get_home_env()])
  17. elif sys.platform == "linux":
  18. path = "%s/.iflytek" % (os.environ[get_home_env()])
  19. else:
  20. path = "%s/.iflytek" % (os.environ[get_home_env()])
  21. return path
  22. def gen_output_path(_input):
  23. _output = f"{_input}/output"
  24. if not os.path.exists(_output):
  25. os.makedirs(_output)
  26. return _output
  27. # 生成json数据模版
  28. def gen_q_data(code, data):
  29. return {"code": code, "data": data}
  30. # 判断字符串是否包含标点
  31. def symbol_in_word(word):
  32. if "," in word:
  33. return 1
  34. if "." in word:
  35. return 1
  36. if "," in word:
  37. return 1
  38. if "。" in word:
  39. return 1
  40. if ";" in word:
  41. return 1
  42. if ";" in word:
  43. return 1
  44. if "?" in word:
  45. return 1
  46. if "?" in word:
  47. return 1
  48. if "!" in word:
  49. return 1
  50. if "!" in word:
  51. return 1
  52. return 0
  53. def parse_aiui_v2_result(result_json):
  54. if result_json and result_json.get("code") == "0" and result_json.get("action") == "result":
  55. data = result_json.get("data")
  56. if data and data.get("sub"):
  57. sub = data.get("sub")
  58. result = {"code": "0", "sub": sub, "is_last": data.get("is_last"), "is_finish": data.get("is_finish")}
  59. if sub == "iat":
  60. text = data.get("text")
  61. result["pgs"] = text.get("pgs")
  62. ws_list = text.get("ws")
  63. words = list(map(lambda ws: ws.get("cw")[0].get("w"), ws_list))
  64. word = "".join(words)
  65. result["service"] = "raw"
  66. result["rawText"] = word
  67. return result
  68. elif sub == "nlp":
  69. intent = data.get("intent")
  70. if intent:
  71. service = intent.get("service")
  72. text = intent.get("text")
  73. # answer_text = intent.get("answer").get("text")
  74. result["service"] = service
  75. result["rawText"] = text
  76. return result
  77. elif sub == "tpp":
  78. content = data.get("content")
  79. if content:
  80. js = json.loads(content)
  81. result.update(js)
  82. return result
  83. return result_json