def fit(self, X=None, y=None, **kwargs): """Creates the mapping from concepts to the thesauri that are broader than the concept.""" broaders = list( t.extract_relation_by_uri(self.graph, self.thesaurus_relation, self.inverse_relation)) concept_po = _collect_po_from_tuples( t.filter_subject_tuples_from_set(broaders, self.concepts)) thesauri_po = _collect_po_from_tuples( t.filter_subject_tuples_from_set(broaders, self.thesauri), self.thesauri) for thesaurus in self.thesauri: thesauri_po[thesaurus].add(thesaurus) thesauri_closure = set_closure(thesauri_po) thesaurus_indices = dict( zip(thesauri_closure, range(len(thesauri_closure)))) concept_thesauri_mapping = { concept: set.union(*(thesauri_closure.get(broader, set()) for broader in broaders)) for concept, broaders in concept_po.items() } self.feature_dim_ = max(len(thesaurus_indices), 1) self.mapping_ = { str(concept): csr_matrix( ([1 for _ in thesaurii], ([0 for _ in thesaurii], [thesaurus_indices[thesaurus] for thesaurus in thesaurii])), shape=(1, len(thesaurus_indices))) for concept, thesaurii in concept_thesauri_mapping.items() } return self
def test_can_reverse(mocker): def mock_iter(a): yield (1, 3) yield (2, 4) g = graph.Graph() mocker.patch.object(g, 'subject_objects', mock_iter) res = t.extract_relation_by_uri(g, SKOS.broader, True) assert list(res) == [(3, 1), (4, 2)]
def test_extract_broader(mocker): g = graph.Graph() spy = mocker.spy(g, "subject_objects") t.extract_relation_by_uri(g, SKOS.broader, False) spy.assert_called_once_with(SKOS.broader)