-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use single script for dbpedia concept learning with drill and tdl; up…
…date README
- Loading branch information
1 parent
0d135c2
commit 4ae9527
Showing
5 changed files
with
31 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
26 changes: 17 additions & 9 deletions
26
...pt_learning_with_drill_and_triplestore.py → ...bpedia_concept_learning_with_ontolearn.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,36 @@ | ||
import json, os | ||
import json, os, sys | ||
from owlapy.owl_individual import OWLNamedIndividual, IRI | ||
from ontolearn.learners import Drill | ||
from ontolearn.learners import Drill, TDL | ||
from ontolearn.learning_problem import PosNegLPStandard | ||
from ontolearn.triple_store import TripleStore | ||
from ontolearn.utils.static_funcs import save_owl_class_expressions | ||
from owlapy.render import DLSyntaxObjectRenderer | ||
# (1) Initialize Triplestore | ||
|
||
if len(sys.argv) < 2: | ||
print("You need to provide the model name; either tdl or drill") | ||
sys.exit(1) | ||
|
||
model_name = sys.argv[1] | ||
assert model_name.lower() in ["drill", "tdl"], "Currently, only Drill and TDL are supported" | ||
|
||
# (1) Initialize knowledge source with TripleStore | ||
kb = TripleStore(url="https://dbpedia.data.dice-research.org/sparql") | ||
# (2) Initialize a DL renderer. | ||
renderer = DLSyntaxObjectRenderer() | ||
# (3) Initialize a learner. | ||
model = Drill(knowledge_base=kb, max_runtime=240) | ||
model = Drill(knowledge_base=kb, max_runtime=240) if model_name.lower() == "drill" else TDL(knowledge_base=kb) | ||
# (4) Solve learning problems | ||
with open("./LPs/DBpedia2022-12/lps.json") as f: | ||
lps = json.load(f) | ||
for i, item in enumerate(lps): | ||
print("\nTarget expression: ", item["target expression"], "\n") | ||
lp = PosNegLPStandard(pos=set(list(map(OWLNamedIndividual,map(IRI.create, item["examples"]["positive examples"])))), | ||
neg=set(list(map(OWLNamedIndividual,map(IRI.create, item["examples"]["negative examples"]))))) | ||
# (5) Learn description logic concepts best fitting (4). | ||
# (5) Learn description logic concepts best fitting | ||
h = model.fit(learning_problem=lp).best_hypotheses() | ||
str_concept = renderer.render(h) | ||
print("Concept:", str_concept) # e.g. ∃ predecessor.WikicatPeopleFromBerlin | ||
# (6) Save ∃ predecessor.WikicatPeopleFromBerlin into disk | ||
if not os.path.exists("./learned_owl_expressions_drill"): | ||
os.mkdir("./learned_owl_expressions_drill") | ||
save_owl_class_expressions(expressions=h, path=f"./learned_owl_expressions_drill/owl_prediction_{i}") | ||
# (6) Save e.g., ∃ predecessor.WikicatPeopleFromBerlin into disk | ||
if not os.path.exists(f"./learned_owl_expressions_{model_name}"): | ||
os.mkdir(f"./learned_owl_expressions_{model_name}") | ||
save_owl_class_expressions(expressions=h, path=f"./learned_owl_expressions_{model_name}/owl_prediction_{i}") |
28 changes: 0 additions & 28 deletions
28
examples/dbpedia_concept_learning_with_tdl_and_triplestore.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from ontolearn.lp_generator import LPGen | ||
|
||
PATH = 'https://dbpedia.data.dice-research.org/sparql' | ||
STORAGE_DIR = 'DBpedia_LPs' | ||
|
||
def generate_lps(): | ||
lp_gen = LPGen(kb_path=PATH, storage_dir=STORAGE_DIR, refinement_expressivity=1e-7, use_triple_store=True, sample_fillers_count=1, num_sub_roots=1) | ||
lp_gen.generate() | ||
|
||
if __name__ == '__main__': | ||
generate_lps() |