main.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import json
  2. import numpy as np
  3. # 从文件读取关键词与one-hot编码的字典
  4. with open("keyword_one_hot_dict.json", "r", encoding="utf-8") as json_file:
  5. keyword_one_hot_dict = json.load(json_file)
  6. # 从文件读取故障现象词的向量字典
  7. with open("phenomenon_vector_dict.json", "r", encoding="utf-8") as json_file:
  8. phenomenon_vector_dict = json.load(json_file)
  9. # 输入句子
  10. sentence = "液压2系统压力正常"
  11. # 初始化词向量
  12. sentence_vector = np.zeros(len(keyword_one_hot_dict[list(keyword_one_hot_dict.keys())[0]]), dtype=np.int32)
  13. # 遍历关键词表,逐个查找关键词是否在句子中
  14. for word in keyword_one_hot_dict.keys():
  15. if word in sentence:
  16. keyword_vector = np.array(keyword_one_hot_dict[word], dtype=np.int32)
  17. sentence_vector += keyword_vector
  18. # 判断是否有对应的故障现象
  19. matched_phenomenon = None
  20. for phenomenon, vector in phenomenon_vector_dict.items():
  21. if np.array_equal(sentence_vector, vector):
  22. matched_phenomenon = phenomenon
  23. break
  24. # 输出结果
  25. if matched_phenomenon:
  26. print("故障现象:", matched_phenomenon)
  27. else:
  28. print("无")