[text] sex

Viewer

  1. import py2neo
  2. import pytholog as pl
  3. graph = py2neo.Graph("bolt://localhost:7687", auth=("neo4j", "12345678"))
  4.  
  5. #input_name = input("Enter the name of the character: ")
  6.  
  7. #create_node_from_variable = f"""
  8. #MERGE (n:SocialNetwork {{name: '{input_name}'}})
  9. #"""
  10. #graph.run(create_node_from_variable)
  11.  
  12. #line = "mother(Sarah, Alisha)"
  13.  
  14. #line = line.replace(")", "")  # remove the closing parenthesis
  15. #predicate, arguments = line.split("(")  # split at the opening parenthesis
  16. #argument1, argument2 = arguments.split(", ")  # split the arguments at the comma
  17. #print(predicate, argument1, argument2)
  18.  
  19. #query = f"""
  20. #MERGE (n:SocialNetwork {{name: '{argument1}'}})-[r:IS_{predicate.upper()}_OF]-(m:SocialNetwork {{name: '{argument2}'}})
  21. #"""
  22. #graph.run(query)
  23.  
  24. kb = pl.KnowledgeBase("kb")
  25. list = [
  26. "male(ali)",
  27. "male(azaril)",
  28. "male(qasim)",
  29. "female(sarah)",
  30. "female(alisha)",
  31. "food(macaroni)",
  32. "parent(qasim, ali)",
  33. "parent(ali, azaril)",
  34. "parent(ali, alisha)",
  35. "parent(sarah, azaril)",
  36. "parent(sarah, alisha)",
  37. "likes(ali, macaroni)",
  38. "father(X, Y) :- parent(X, Y), male(X)"
  39. ]
  40. kb(list)
  41.  
  42. prolog_query = "father(X, Y)"
  43. # This is a query from prolog (pytholog) knowledge-base.
  44. results = kb.query(pl.Expr(prolog_query))
  45. predicate, _ = prolog_query.split("(")
  46.  
  47. print(predicate)
  48.  
  49. print(results)
  50.  
  51. x = [result['X'] for result in results if 'X' in result]
  52. y = [result['Y'] for result in results if 'Y' in result]
  53.  
  54. while x:
  55.     query = f"""
  56.     MERGE (n:SocialNetwork {{name: '{x[0]}'}})-[r:IS_{predicate.upper()}_OF]-(m:SocialNetwork {{name: '{y[0]}'}})
  57.     """
  58.     x.pop(0)
  59.     y.pop(0)
  60.     graph.run(query)
  61.     

Editor

You can edit this paste and save as new:


File Description
  • sex
  • Paste Code
  • 07 May-2024
  • 1.62 Kb
You can Share it: