-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
78e8274
commit 83181e4
Showing
2 changed files
with
35 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import argparse | ||
import rdflib | ||
|
||
def find_ontology_base(file_path): | ||
g = rdflib.Graph() | ||
g.parse(file_path, format='turtle') | ||
|
||
for s in g.subjects(predicate=rdflib.RDF.type, object=rdflib.OWL.Ontology): | ||
return str(s) | ||
return None | ||
|
||
def add_base_if_missing(file_path): | ||
base_uri = find_ontology_base(file_path) | ||
if base_uri is None: | ||
print("No owl:Ontology found in the RDF file!") | ||
return | ||
|
||
with open(file_path, 'r') as f: | ||
lines = f.readlines() | ||
|
||
# Insert the @base declaration before line #4 | ||
if "@base" not in lines[3]: # Checking to ensure it's not already added | ||
lines.insert(3, f"@base <{base_uri}> .\n") | ||
|
||
with open(file_path, 'w') as f: | ||
f.writelines(lines) | ||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser(description='Add @base to an RDF Turtle file if it is missing.') | ||
parser.add_argument('file_path', help='Path to the RDF Turtle file.') | ||
|
||
args = parser.parse_args() | ||
|
||
add_base_if_missing(args.file_path) |
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