Skip to content

Commit

Permalink
Merge pull request #936 from PrimozGodec/ontology-fixes
Browse files Browse the repository at this point in the history
[FIX] Ontology - Show labels instead of names for imported ontolgies
  • Loading branch information
VesnaT authored Mar 7, 2023
2 parents 9ca2cd1 + e6321c3 commit ae848dc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion orangecontrib/text/widgets/owontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ def _tree_to_html(tree: Dict) -> str:
def _onto_to_tree(thing: Thing, world: World) -> Dict:
tree = {}
for cl in list(thing.subclasses(world=world)):
tree[cl.name] = _onto_to_tree(cl, world)
label = (cl.label.first() or cl.name).replace("\n", " ").strip()
tree[label] = _onto_to_tree(cl, world)
return tree


Expand Down
29 changes: 29 additions & 0 deletions orangecontrib/text/widgets/tests/test_owontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
QItemSelectionRange
from AnyQt.QtWidgets import QFileDialog
from AnyQt.QtTest import QTest
from owlready2 import get_ontology, Thing, types

from Orange.data import Table
from Orange.widgets.tests.base import WidgetTest
Expand Down Expand Up @@ -279,6 +280,34 @@ def test_library_import(self, _):
os.path.basename(f.name))
self.assertEqual(get_ontology_data(), ontology)

def test_library_import_owl(self):
# create ontology
onto = get_ontology("http://test.org/onto.owl")
with onto:
# foo3 will be missing the label attribute - widget display name
foo3 = types.new_class("foo3", (Thing,))
# other two classes will have label attribute - widget display label
for c, label in (("bar3", "Bar 3"), ("baz3", "Baz 3")):
thing = types.new_class(c, (foo3,))
thing.label = label

get_ontology_data = self.widget._OWOntology__ontology_view.get_data
with tempfile.TemporaryDirectory() as tmp_dir_name:
# safe to file
path = os.path.join(tmp_dir_name, "onto.owl")
onto.save(file=path, format="rdfxml")

# open with widget
with patch.object(
QFileDialog, "getOpenFileName", Mock(return_value=(path, None))
):
self.widget._OWOntology__on_import_file()
self.assertEqual(self.widget._OWOntology__get_selected_row(), 2)
name = self.widget._OWOntology__model[2].name
self.assertEqual(name, os.path.basename(path))
expected = {"foo3": {"Bar 3": {}, "Baz 3": {}}}
self.assertEqual(get_ontology_data(), expected)

def test_library_save(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".pkl",
delete=False) as f:
Expand Down

0 comments on commit ae848dc

Please sign in to comment.