Skip to content

Commit

Permalink
Quartz sync: Aug 4, 2024, 10:36 PM
Browse files Browse the repository at this point in the history
  • Loading branch information
saviorand committed Aug 4, 2024
1 parent e3891f4 commit de69f16
Show file tree
Hide file tree
Showing 16 changed files with 811 additions and 158 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ tsconfig.tsbuildinfo
private/
.replit
replit.nix
.vscode
.vscode
content/excalibrain.md
content/Scripts/.env
60 changes: 0 additions & 60 deletions content/Entries/text_to_prolog.py

This file was deleted.

5 changes: 5 additions & 0 deletions content/References/computerProgram/SynaLinksHybridAGI.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[🇿](zotero://select/library/items/V387NZWL)


# SynaLinks/HybridAGI (2024)

5 changes: 5 additions & 0 deletions content/References/computerProgram/neo4jNaLLM.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[🇿](zotero://select/library/items/7XHYWD58)


# neo4j/NaLLM (2024)

5 changes: 5 additions & 0 deletions content/References/computerProgram/rahulnykgraph_maker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[🇿](zotero://select/library/items/MESBET73)

[[Entries/Individuals/ rahulnyk]]
# rahulnyk/graph_maker (2024)

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[🇿](zotero://select/library/items/UYJXAI9D)

[[Entries/Individuals/ rahulnyk]]
# rahulnyk/knowledge_graph_maker (2024)

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[🇿](zotero://select/library/items/369FGT9H)

[[Entries/Individuals/Mohamad Yaser Jaradeh]] [[Entries/Individuals/Kuldeep Singh]] [[Entries/Individuals/Markus Stocker]] [[Entries/Individuals/Andreas Both]] [[Entries/Individuals/Sören Auer]]
# Information extraction pipelines for knowledge graphs (2023)

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[🇿](zotero://select/library/items/MVFJ5Z2R)

[[Entries/Individuals/Aman Kumar]] [[Entries/Individuals/Swathi Dinakaran]]
# TEXTBOOK TO TRIPLES: Creating knowledge graph in the form of triples from AI TextBook

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[🇿](zotero://select/library/items/GDR8CVEQ)

[[Entries/Individuals/Rahul Nayak]]
# How to Convert Any Text Into a Graph of Concepts (2023)

17 changes: 17 additions & 0 deletions content/Scripts/pdf_to_txt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import sys
from pypdf import PdfReader

def pdf_to_txt(pdf_path, txt_path):
with open(pdf_path, 'rb') as pdf_file, open(txt_path, 'w', encoding='utf-8') as txt_file:
pdf_reader = PdfReader(pdf_file)
for page in pdf_reader.pages:
txt_file.write(page.extract_text())

if len(sys.argv) < 3:
print("Usage: python pdf_to_txt.py input_pdf output_txt")
sys.exit(1)

pdf_path = sys.argv[1]
txt_path = sys.argv[2]

pdf_to_txt(pdf_path, txt_path)
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
import sys
import os
import re
from utils import parse_prolog_file

def parse_prolog_file(file_path):
with open(file_path, 'r') as file:
content = file.read()

arity_1_predicates = {}
arity_2_predicates = []

# Parse arity 1 predicates
for match in re.finditer(r'(\w+)\((\w+)\)\.', content):
predicate, parameter = match.groups()
if predicate not in arity_1_predicates:
arity_1_predicates[predicate] = set()
arity_1_predicates[predicate].add(parameter)

# Parse arity 2 predicates
for match in re.finditer(r'(\w+)\((\w+),\s*(\w+)\)\.', content):
predicate, param1, param2 = match.groups()
arity_2_predicates.append((predicate, param1, param2))

return arity_1_predicates, arity_2_predicates
def create_folders_and_files(arity_1_predicates, arity_2_predicates, output_dir):
os.makedirs(output_dir, exist_ok=True)

Expand Down Expand Up @@ -58,9 +39,13 @@ def create_folders_and_files(arity_1_predicates, arity_2_predicates, output_dir)
print("Folders and files created successfully!")

def main():
input_file = "./test.pl" # Change this to your Prolog file name
output_dir = "output" # Change this to your desired output directory

if len(sys.argv) < 3:
print("Usage: python prolog_to_folder.py input_file output_dir")
sys.exit(1)

input_file = sys.argv[1]
output_dir = sys.argv[2]

arity_1_predicates, arity_2_predicates = parse_prolog_file(input_file)
create_folders_and_files(arity_1_predicates, arity_2_predicates, output_dir)

Expand Down
71 changes: 71 additions & 0 deletions content/Scripts/prompts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
def correctness_check_prompt(domain_subjects):
return f'''You are a domain expert in the field of {domain_subjects}.
Check the Prolog code for correctness based on the text. Ensure all relationships are logically sound and perfectly consistent with the text.
If you find any inconsistencies, correct them in the Prolog code.
Please respond with prolog code only.'''

relation_prompt = '''You are an expert at creating Knowledge Graphs in Prolog.
Translate sentences in the text into Prolog code using predicates of arity 2.
Arity 2 predicates define relationships (verbs) between nouns, they are provided below.
You can ONLY use the following predicates:
category/2
challenges/2
contributes/2
creates/2
does/2
provokes/2
targets/2
parent/2
Please respond with prolog code only.
Example:
Text: "John creates a project. The project targets education."
Step-by-Step Translation:
a. Entities and Objects: "John", "project", "education"
b. Relationships: "creates", "targets"
c. Arity 2 Predicates:
creates(john, project).
targets(project, education).
e. Validation: Ensure all entities and relationships are included and correctly formatted.
f. Output:
creates(john, project).
targets(project, education).
Text:'''

categories_prompt = '''You are an expert at creating Knowledge Graphs in Prolog.
Categorize the list of entities using Prolog predicates of arity 1. Only assign 1 category to 1 entity.
You can ONLY use the following predicates:
abstract_concept/1
physical_entity/1
country/1
person/1
organization/1
event/1
theoretical_framework/1
Please respond with prolog code only.
Example:
Entities: "John", "Cuba", "Mount Everest", "education"
Step-by-Step Translation:
a. Entities: "John", "Cuba", "Mount Everest", "education"
b. Categories: "person", "country", "physical_entity", "abstract_concept"
c. Arity 1 Predicates:
person(john).
country(cuba).
physical_entity(mount_everest).
abstract_concept(education).
d. Validation: Ensure all entities are categorized and correctly formatted.
e. Output:
person(john).
country(cuba).
physical_entity(mount_everest).
abstract_concept(education).
Entities:
'''
29 changes: 29 additions & 0 deletions content/Scripts/text_to_prolog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import sys
from utils import parse_prolog_predicates, call_gpt_api, relation_correctness_check, file_to_chunks, text_to_relations, prolog_predicates_to_entities, entities_to_categorized_entities
from prompts import relation_prompt, categories_prompt, correctness_check_prompt

if len(sys.argv) < 3:
print("Usage: python text_to_prolog.py input_file domain_subjects")
sys.exit(1)

input_file = sys.argv[1] # e.g. 'nieo.txt'
domain_subjects = sys.argv[2] # e.g. 'NIEO, international relations, economics'
name = input_file.split('.')[0]

original_text_chunk_size = 2000
original_text_chunks = file_to_chunks(input_file, original_text_chunk_size)

relation_output_file = f'{name}_relations.pl'
entities_output_file = f'{name}_entities.pl'
categories_output_file = f'{name}_categories.pl'

output_relations = text_to_relations(original_text_chunks, relation_output_file, relation_prompt, correctness_check_prompt(domain_subjects))

entity_predicates = parse_prolog_predicates(output_relations)

prolog_predicates_to_entities(entity_predicates, entities_output_file)

entities_chunk_size = 2000
entities_chunks = file_to_chunks(entities_output_file, entities_chunk_size)

entities_to_categorized_entities(entities_chunks, categories_output_file, categories_prompt)
Loading

0 comments on commit de69f16

Please sign in to comment.