process.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import re
  2. from mapping import mapping
  3. from py2neo import Graph
  4. #text = "飞机启动过程中断电;飞机正常上电失败怎么解决"
  5. #text = "液压告警灯灭;液压2系统压力正常的原因"
  6. #text = "继电器失效;电源故障"
  7. cause_qwds = ['原因','成因', '为什么', '怎么会', '怎样会', '为啥', '为何']
  8. solve_qwds = ['怎么解决','怎么处理','怎么修理','怎么修复','怎么维修','解决方法','处理方法','修理方法','修复方法','维修方法','怎么修','咋修','怎么办','处置']
  9. with open("./dict/fault.txt", 'r',encoding='utf-8') as file:
  10. lines = file.readlines()
  11. # 去除每行末尾的换行符并拆分为词
  12. faults = []
  13. for line in lines:
  14. line = line.strip() # 去除换行符
  15. words = line.split() # 拆分为词
  16. faults.extend(words) # 添加到数组
  17. def check_words(wds, sent):
  18. for wd in wds:
  19. if wd in sent:
  20. return True
  21. return False
  22. def process(text):
  23. # 使用正则表达式按中文分号拆分句子
  24. sentences1 = re.split(";", text)
  25. # 去除空白字符
  26. sentences = [s.strip() for s in sentences1 if s.strip()]
  27. entities = []
  28. for sentence in sentences:
  29. result = mapping(sentence)
  30. if(result != None):
  31. entities.append(result)
  32. #print(entities)
  33. # 连接 Neo4j 数据库
  34. graph = Graph("bolt://localhost:7687", auth=("neo4j", "123456"))
  35. answer = ""
  36. query = ""
  37. #entities = ["液压2系统压力正常", "液压告警灯熄灭","再次进行电传PBIT测试,依旧报此故障"]
  38. if(check_words(solve_qwds, text)):
  39. if(entities == []):
  40. return"未找到对应实体"
  41. for i in entities:
  42. query += "MATCH (a)-[:故障现象]->(:故障现象{name:'" + i + "'}) "
  43. query += "MATCH (a)-[:排故流程]->(b:故障流程) RETURN a, b"
  44. #print(query)
  45. # 构建 Cypher 查询语句
  46. #query = "MATCH (a:故障名称)-[:故障原因]->(:故障原因{name:'2号电传计算机故障'}), (a)-[:故障现象]->(:故障现象{name:'液压告警灯熄灭'}) MATCH (a)-[:排故流程]->(b:故障流程) RETURN a,b"
  47. # 执行查询
  48. result = graph.run(query, entities=entities)
  49. # Process the query result
  50. for record in result:
  51. fault_name = record['a']['name']
  52. fault_process = record['b']['name']
  53. answer += fault_name + "的排故流程是:\n" + fault_process + "\n"
  54. #print(f"故障名称: {fault_name}")
  55. #print(f"排故流程: {fault_process}")
  56. elif(check_words(faults, text)):
  57. for i in sentences:
  58. query = "MATCH (a:故障名称{name:'" + i +"'})-[:排故流程]->(b:故障流程) RETURN a, b"
  59. result = graph.run(query, entities=entities)
  60. for record in result:
  61. fault_name = record['a']['name']
  62. fault_process = record['b']['name']
  63. answer += fault_name + "的排故流程是:\n" + fault_process + "\n"
  64. elif(check_words(cause_qwds, text)):
  65. if(entities == []):
  66. return"未找到对应实体"
  67. for i in entities:
  68. query += "MATCH (a)-[:故障现象]->(:故障现象{name:'" + i + "'}) "
  69. query += "MATCH (a)-[:故障原因]->(b:故障原因) RETURN a, b"
  70. #print(query)
  71. # 构建 Cypher 查询语句
  72. #query = "MATCH (a:故障名称)-[:故障原因]->(:故障原因{name:'2号电传计算机故障'}), (a)-[:故障现象]->(:故障现象{name:'液压告警灯熄灭'}) MATCH (a)-[:排故流程]->(b:故障流程) RETURN a,b"
  73. # 执行查询
  74. result = graph.run(query, entities=entities)
  75. for i in entities:
  76. answer += i
  77. answer += "的原因是:\n"
  78. # Process the query result
  79. for record in result:
  80. fault_process = record['b']['name']
  81. answer += fault_process + "\n"
  82. #print(f"故障名称: {fault_name}")
  83. #print(f"排故流程: {fault_process}")
  84. return answer
  85. #print(process(text))