Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
ontoScorer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Tetras MARS
ontoScorer
Commits
4a82231f
Commit
4a82231f
authored
1 year ago
by
Aurélien Lamercerie
Browse files
Options
Downloads
Patches
Plain Diff
New module: ontology_utils
parent
1530e185
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
ontoScorer/ontology_utils.py
+32
-0
32 additions, 0 deletions
ontoScorer/ontology_utils.py
tests/dev_test_2.py
+127
-0
127 additions, 0 deletions
tests/dev_test_2.py
tests/test_ontology_utils.py
+42
-0
42 additions, 0 deletions
tests/test_ontology_utils.py
with
201 additions
and
0 deletions
ontoScorer/ontology_utils.py
0 → 100644
+
32
−
0
View file @
4a82231f
#!/usr/bin/python3.10
# -*-coding:Utf-8 -*
#==============================================================================
# Ontology Utilities
#------------------------------------------------------------------------------
# --
#==============================================================================
from
rdflib
import
Graph
def
harmonize_prefixes
(
graph
,
source_uri
,
target_uri
):
for
s
,
p
,
o
in
graph
.
triples
((
None
,
None
,
None
)):
s
=
s
if
not
s
.
startswith
(
source_uri
)
else
s
.
replace
(
source_uri
,
target_uri
)
p
=
p
if
not
p
.
startswith
(
source_uri
)
else
p
.
replace
(
source_uri
,
target_uri
)
o
=
o
if
not
isinstance
(
o
,
str
)
or
not
o
.
startswith
(
source_uri
)
else
o
.
replace
(
source_uri
,
target_uri
)
graph
.
add
((
s
,
p
,
o
))
return
graph
def
extract_prefixes_from_graph
(
graph
):
return
{
prefix
:
namespace
for
prefix
,
namespace
in
graph
.
namespaces
()}
def
uri_to_short_form
(
uri
,
prefix_mappings
,
graph_prefixes
):
for
prefix
,
*
uris
in
prefix_mappings
:
for
u
in
uris
:
if
u
in
uri
:
return
f
"
{
prefix
}
:
{
uri
.
replace
(
u
,
''
)
}
"
for
prefix
,
namespace
in
graph_prefixes
.
items
():
if
namespace
in
uri
:
return
f
"
{
prefix
}
:
{
uri
.
replace
(
namespace
,
''
)
}
"
return
uri
This diff is collapsed.
Click to expand it.
tests/dev_test_2.py
0 → 100644
+
127
−
0
View file @
4a82231f
#!/usr/bin/python3.10
# -*-coding:Utf-8 -*
print
(
"
=== DEV TEST 1
"
)
import
os
from
rdflib
import
Graph
,
Namespace
DATA_FOLDER_PATH
=
f
'
{
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
}
/test_data
'
ontology_a
=
f
"
{
DATA_FOLDER_PATH
}
/ontology_a.ttl
"
ontology_b
=
f
"
{
DATA_FOLDER_PATH
}
/ontology_b.ttl
"
# Charger la première ontologie
graph_a
=
Graph
()
graph_a
.
parse
(
ontology_a
,
format
=
"
ttl
"
)
# Charger la deuxième ontologie
graph_b
=
Graph
()
graph_b
.
parse
(
ontology_b
,
format
=
"
ttl
"
)
from
rdflib
import
URIRef
def
harmonize_prefixes
(
graph
,
old_prefix
,
new_prefix
):
new_graph
=
Graph
()
for
s
,
p
,
o
in
graph
:
if
isinstance
(
s
,
URIRef
)
and
old_prefix
in
str
(
s
):
s
=
URIRef
(
str
(
s
).
replace
(
old_prefix
,
new_prefix
))
if
isinstance
(
p
,
URIRef
)
and
old_prefix
in
str
(
p
):
p
=
URIRef
(
str
(
p
).
replace
(
old_prefix
,
new_prefix
))
if
isinstance
(
o
,
URIRef
)
and
old_prefix
in
str
(
o
):
o
=
URIRef
(
str
(
o
).
replace
(
old_prefix
,
new_prefix
))
new_graph
.
add
((
s
,
p
,
o
))
return
new_graph
graph_a
=
harmonize_prefixes
(
graph_a
,
"
https://tenet.tetras-libre.fr/base-ontology#
"
,
"
https://reference.tetras-libre.fr/base-ontology#
"
)
def
compare_graphs
(
graph1
,
graph2
):
diff1
=
set
(
graph1
)
-
set
(
graph2
)
diff2
=
set
(
graph2
)
-
set
(
graph1
)
return
diff1
,
diff2
diff_a_b
,
diff_b_a
=
compare_graphs
(
graph_a
,
graph_b
)
print
(
f
"
Result:
{
len
(
diff_a_b
)
}
|
{
len
(
diff_b_a
)
}
"
)
print
(
"
\n
=== DEV TEST 2
"
)
equivalent_prefix
=
[
(
"
base
"
,
"
https://reference.tetras-libre.fr/base-ontology#
"
,
"
https://tenet.tetras-libre.fr/base-ontology#
"
),
(
"
result
"
,
"
https://reference.tetras-libre.fr/expected-result#
"
,
"
https://tenet.tetras-libre.fr/extract-result#
"
)
]
# from rdflib import URIRef
# def update_prefixes(graph, prefix_equivalences):
# new_graph = Graph()
# for s, p, o in graph:
# new_s, new_p, new_o = s, p, o
# for prefix, old_prefix_uri, new_prefix_uri in prefix_equivalences:
# if isinstance(new_s, URIRef) and old_prefix_uri in str(new_s):
# new_s = URIRef(str(new_s).replace(old_prefix_uri, new_prefix_uri))
# if isinstance(new_p, URIRef) and old_prefix_uri in str(new_p):
# new_p = URIRef(str(new_p).replace(old_prefix_uri, new_prefix_uri))
# if isinstance(new_o, URIRef) and old_prefix_uri in str(new_o):
# new_o = URIRef(str(new_o).replace(old_prefix_uri, new_prefix_uri))
# new_graph.add((new_s, new_p, new_o))
# return new_graph
def
uri_to_short_form
(
uri
,
prefix_mappings
):
for
prefix
,
*
uris
in
prefix_mappings
:
for
u
in
uris
:
if
u
in
uri
:
return
f
"
{
prefix
}
:
{
uri
.
replace
(
u
,
''
)
}
"
return
uri
# Si aucune correspondance n'est trouvée, retourne l'URI original.
uri
=
"
https://reference.tetras-libre.fr/expected-result#SolarSystem
"
short_form
=
uri_to_short_form
(
uri
,
equivalent_prefix
)
print
(
short_form
)
# Affiche "result:SolarSystem"
print
(
"
\n
=== DEV TEST 3
"
)
def
extract_prefixes_from_graph
(
graph
):
return
{
prefix
:
namespace
for
prefix
,
namespace
in
graph
.
namespaces
()}
def
uri_to_short_form
(
uri
,
prefix_mappings
,
graph_prefixes
):
# Vérifier d'abord les correspondances dans equivalent_prefix
for
prefix
,
*
uris
in
prefix_mappings
:
for
u
in
uris
:
if
u
in
uri
:
return
f
"
{
prefix
}
:
{
uri
.
replace
(
u
,
''
)
}
"
# Si aucune correspondance n'est trouvée dans equivalent_prefix, utiliser les préfixes du graphe
for
prefix
,
namespace
in
graph_prefixes
.
items
():
if
namespace
in
uri
:
return
f
"
{
prefix
}
:
{
uri
.
replace
(
namespace
,
''
)
}
"
return
uri
# Si aucune correspondance n'est trouvée, retourne l'URI original.
graph
=
Graph
()
graph
.
parse
(
ontology_a
,
format
=
"
ttl
"
)
graph_prefixes
=
extract_prefixes_from_graph
(
graph
)
uri
=
"
http://www.w3.org/2000/01/rdf-schema#label
"
short_form
=
uri_to_short_form
(
uri
,
equivalent_prefix
,
graph_prefixes
)
print
(
short_form
)
# Affiche "rdfs:label"
This diff is collapsed.
Click to expand it.
tests/test_ontology_utils.py
0 → 100644
+
42
−
0
View file @
4a82231f
#!/usr/bin/python3.10
# -*-coding:Utf-8 -*
#==============================================================================
# test_ontology_modules: Ontology Modules Testing
#------------------------------------------------------------------------------
# Contains tests for verifying functionality of the ontology submodules.
#==============================================================================
import
unittest
from
rdflib
import
Graph
# Importing necessary modules from the ontoScorer package for testing.
from
context
import
ontoScorer
from
ontoScorer
import
ontology_utils
as
ou
class
TestOntologyUtils
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
equivalent_prefix
=
[
(
"
base
"
,
"
https://reference.tetras-libre.fr/base-ontology#
"
,
"
https://tenet.tetras-libre.fr/base-ontology#
"
),
(
"
result
"
,
"
https://reference.tetras-libre.fr/expected-result#
"
,
"
https://tenet.tetras-libre.fr/extract-result#
"
)
]
self
.
rdf_data
=
"""
@prefix ns1: <https://tenet.tetras-libre.fr/base-ontology#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
"""
self
.
graph
=
Graph
().
parse
(
data
=
self
.
rdf_data
,
format
=
"
turtle
"
)
def
test_uri_to_short_form
(
self
):
graph_prefixes
=
ou
.
extract_prefixes_from_graph
(
self
.
graph
)
short_form
=
ou
.
uri_to_short_form
(
"
https://reference.tetras-libre.fr/expected-result#SolarSystem
"
,
self
.
equivalent_prefix
,
graph_prefixes
)
self
.
assertEqual
(
short_form
,
"
result:SolarSystem
"
)
short_form2
=
ou
.
uri_to_short_form
(
"
http://www.w3.org/2000/01/rdf-schema#label
"
,
self
.
equivalent_prefix
,
graph_prefixes
)
self
.
assertEqual
(
short_form2
,
"
rdfs:label
"
)
if
__name__
==
'
__main__
'
:
unittest
.
main
()
This diff is collapsed.
Click to expand it.
Aurélien Lamercerie
@alam
mentioned in issue
#3 (closed)
·
1 year ago
mentioned in issue
#3 (closed)
mentioned in issue #3
Toggle commit list
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment