Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Ontology - Show labels instead of names for imported ontolgies #936

Merged
merged 1 commit into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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