123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- import re
- from mapping import mapping
- from py2neo import Graph
- #text = "飞机启动过程中断电;飞机正常上电失败怎么解决"
- #text = "液压告警灯灭;液压2系统压力正常的原因"
- #text = "继电器失效;电源故障"
- cause_qwds = ['原因','成因', '为什么', '怎么会', '怎样会', '为啥', '为何']
- solve_qwds = ['怎么解决','怎么处理','怎么修理','怎么修复','怎么维修','解决方法','处理方法','修理方法','修复方法','维修方法','怎么修','咋修','怎么办','处置']
- with open("./dict/fault.txt", 'r',encoding='utf-8') as file:
- lines = file.readlines()
- # 去除每行末尾的换行符并拆分为词
- faults = []
- for line in lines:
- line = line.strip() # 去除换行符
- words = line.split() # 拆分为词
- faults.extend(words) # 添加到数组
- def check_words(wds, sent):
- for wd in wds:
- if wd in sent:
- return True
- return False
- def process(text):
- # 使用正则表达式按中文分号拆分句子
- sentences1 = re.split(";", text)
- # 去除空白字符
- sentences = [s.strip() for s in sentences1 if s.strip()]
- entities = []
- for sentence in sentences:
- result = mapping(sentence)
- if(result != None):
- entities.append(result)
- #print(entities)
- # 连接 Neo4j 数据库
- graph = Graph("bolt://localhost:7687", auth=("neo4j", "123456"))
- answer = ""
- query = ""
- #entities = ["液压2系统压力正常", "液压告警灯熄灭","再次进行电传PBIT测试,依旧报此故障"]
- if(check_words(solve_qwds, text)):
- if(entities == []):
- return"未找到对应实体"
- for i in entities:
- query += "MATCH (a)-[:故障现象]->(:故障现象{name:'" + i + "'}) "
- query += "MATCH (a)-[:排故流程]->(b:故障流程) RETURN a, b"
- #print(query)
- # 构建 Cypher 查询语句
- #query = "MATCH (a:故障名称)-[:故障原因]->(:故障原因{name:'2号电传计算机故障'}), (a)-[:故障现象]->(:故障现象{name:'液压告警灯熄灭'}) MATCH (a)-[:排故流程]->(b:故障流程) RETURN a,b"
- # 执行查询
- result = graph.run(query, entities=entities)
-
- # Process the query result
- for record in result:
- fault_name = record['a']['name']
- fault_process = record['b']['name']
- answer += fault_name + "的排故流程是:\n" + fault_process + "\n"
- #print(f"故障名称: {fault_name}")
- #print(f"排故流程: {fault_process}")
- elif(check_words(faults, text)):
- for i in sentences:
- query = "MATCH (a:故障名称{name:'" + i +"'})-[:排故流程]->(b:故障流程) RETURN a, b"
- result = graph.run(query, entities=entities)
- for record in result:
- fault_name = record['a']['name']
- fault_process = record['b']['name']
- answer += fault_name + "的排故流程是:\n" + fault_process + "\n"
-
- elif(check_words(cause_qwds, text)):
- if(entities == []):
- return"未找到对应实体"
- for i in entities:
- query += "MATCH (a)-[:故障现象]->(:故障现象{name:'" + i + "'}) "
- query += "MATCH (a)-[:故障原因]->(b:故障原因) RETURN a, b"
- #print(query)
- # 构建 Cypher 查询语句
- #query = "MATCH (a:故障名称)-[:故障原因]->(:故障原因{name:'2号电传计算机故障'}), (a)-[:故障现象]->(:故障现象{name:'液压告警灯熄灭'}) MATCH (a)-[:排故流程]->(b:故障流程) RETURN a,b"
- # 执行查询
- result = graph.run(query, entities=entities)
- for i in entities:
- answer += i
- answer += "的原因是:\n"
- # Process the query result
- for record in result:
- fault_process = record['b']['name']
- answer += fault_process + "\n"
- #print(f"故障名称: {fault_name}")
- #print(f"排故流程: {fault_process}")
-
- return answer
- #print(process(text))
|