get_entity.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from py2neo import Graph
  2. # Neo4j数据库连接信息
  3. uri = "bolt://localhost:7687" # Neo4j数据库URI
  4. username = "neo4j" # Neo4j数据库用户名
  5. password = "123456" # Neo4j数据库密码
  6. # 连接Neo4j数据库
  7. graph = Graph(uri, auth=(username, password))
  8. query = "MATCH (n) RETURN n.name AS name"
  9. result = graph.run(query)
  10. # 将节点名称写入文件
  11. with open("./dict/entity.txt", "w",encoding = 'utf-8') as file:
  12. for record in result:
  13. name = record["name"]
  14. file.write(name + "\n")
  15. # 获取所有标签为"故障代码"的节点的name属性,并保存到txt文件中
  16. def get_code():
  17. query = "MATCH (n:故障代码) RETURN n.name AS name"
  18. results = graph.run(query)
  19. names = [record["name"] for record in results]
  20. with open("./dict/code.txt", "w",encoding = 'utf-8') as file:
  21. for name in names:
  22. file.write(name + "\n")
  23. print("故障代码已保存到code.txt文件中。")
  24. def get_fault():
  25. query = "MATCH (n:故障名称) RETURN n.name AS name"
  26. results = graph.run(query)
  27. names = [record["name"] for record in results]
  28. with open("./dict/fault.txt", "w",encoding = 'utf-8') as file:
  29. for name in names:
  30. file.write(name + "\n")
  31. print("故障名称已保存到fault.txt文件中。")
  32. def get_phenomenon():
  33. query = "MATCH (n:故障现象) RETURN n.name AS name"
  34. results = graph.run(query)
  35. names = [record["name"] for record in results]
  36. with open("./dict/phenomenon.txt", "w",encoding = 'utf-8') as file:
  37. for name in names:
  38. file.write(name + "\n")
  39. print("故障现象已保存到phenomenon.txt文件中。")
  40. def get_cause():
  41. query = "MATCH (n:故障原因) RETURN n.name AS name"
  42. results = graph.run(query)
  43. names = [record["name"] for record in results]
  44. with open("./dict/cause.txt", "w",encoding = 'utf-8') as file:
  45. for name in names:
  46. file.write(name + "\n")
  47. print("故障原因已保存到cause.txt文件中。")
  48. def get_maintain():
  49. query = "MATCH (n:故障流程) RETURN n.name AS name"
  50. results = graph.run(query)
  51. names = [repr(record["name"]) for record in results]
  52. with open("./dict/maintain.txt", "w",encoding = 'utf-8') as file:
  53. for name in names:
  54. file.write(name + "\n")
  55. print("故障现象已保存到maintain.txt文件中。")
  56. def get_level():
  57. query = "MATCH (n:故障等级) RETURN n.name AS name"
  58. results = graph.run(query)
  59. names = [record["name"] for record in results]
  60. with open("./dict/level.txt", "w",encoding = 'utf-8') as file:
  61. for name in names:
  62. file.write(name + "\n")
  63. print("故障等级已保存到level.txt文件中。")
  64. # 从txt文件中读取节点的name属性
  65. def read_nodes_from_file():
  66. with open("./dict/maintain.txt", "r",encoding = 'utf-8') as file:
  67. names = [eval(line.strip()) for line in file]
  68. return names
  69. # 示例代码的执行
  70. get_code() # 获取并保存节点到txt文件中
  71. get_cause()
  72. get_fault()
  73. get_level()
  74. get_maintain()
  75. get_phenomenon()
  76. node_names = read_nodes_from_file() # 从txt文件中读取节点名称
  77. print("从txt文件中读取的节点名称:", node_names)