def from_thing(thing): """ Map a :class:`skosprovider_sqlalchemy.models.Thing` to a :class:`skosprovider.skos.Concept` or a :class:`skosprovider.skos.Collection`, depending on the type. :param skosprovider_sqlalchemy.models.Thing thing: Thing to map. :rtype: :class:`~skosprovider.skos.Concept` or :class:`~skosprovider.skos.Collection`. """ if thing.type and thing.type == 'collection': return Collection( id=thing.concept_id, uri=thing.uri, concept_scheme=ConceptScheme(thing.conceptscheme.uri), labels=[ Label(l.label, l.labeltype_id, l.language_id) for l in thing.labels ], sources=[ Source(s.citation) for s in thing.sources ], members=[member.concept_id for member in thing.members] if hasattr(thing, 'members') else [], member_of=[c.concept_id for c in thing.member_of], superordinates=[broader_concept.concept_id for broader_concept in thing.broader_concepts] ) else: matches = {} for m in thing.matches: key = m.matchtype.name[:m.matchtype.name.find('Match')] if key not in matches: matches[key] = [] matches[key].append(m.uri) return Concept( id=thing.concept_id, uri=thing.uri, concept_scheme=ConceptScheme(thing.conceptscheme.uri), labels=[ Label(l.label, l.labeltype_id, l.language_id) for l in thing.labels ], notes=[ Note(n.note, n.notetype_id, n.language_id) for n in thing.notes ], sources=[ Source(s.citation) for s in thing.sources ], broader=[c.concept_id for c in thing.broader_concepts], narrower=[c.concept_id for c in thing.narrower_concepts], related=[c.concept_id for c in thing.related_concepts], member_of=[c.concept_id for c in thing.member_of], subordinate_arrays=[narrower_collection.concept_id for narrower_collection in thing.narrower_collections], matches=matches, )
def test_exact_precedes_inexact_match(self): khnl = Label('Knokke-Heist', type="prefLabel", language='nl') chnl = Label('Cnock-Heyst', type="altLabel", language='nl') khen = Label('Knocke-Heyst', type="prefLabel", language='en') khnlbe = self._get_knokke_heist_nl() chnlbe = self._get_cnocke_heyst_nl() khengb = self._get_knokke_heist_en() labels = [chnl, khen, khnlbe, khnl, chnlbe, khengb] assert khnlbe == label(labels, 'nl-BE') assert khnl == label(labels, 'nl') assert label(labels, 'en-US') in [khen, khengb]
def _get_concept_scheme(self): ''' Find a :class:`skosprovider.skos.ConceptScheme` for this provider. :param id: Id of a conceptscheme. :rtype: :class:`skosprovider.skos.ConceptScheme` ''' csm = self.session\ .query(ConceptSchemeModel)\ .options(joinedload('labels'))\ .options(joinedload('notes'))\ .options(joinedload('languages'))\ .options(joinedload('sources'))\ .get(self.conceptscheme_id) return ConceptScheme( uri=csm.uri, labels=[ Label(l.label, l.labeltype_id, l.language_id) for l in csm.labels ], notes=[ Note(n.note, n.notetype_id, n.language_id, n.markup) for n in csm.notes ], languages=[l.id for l in csm.languages], sources=[Source(s.citation, s.markup) for s in csm.sources])
def _get_materials_provider(self): import json materials_data = json.load( open( os.path.join(os.path.dirname(__file__), 'data', 'materiaal.txt')), )['materiaal'] from skosprovider.providers import DictionaryProvider from skosprovider.uri import UriPatternGenerator from skosprovider.skos import ConceptScheme, Label, Note materials = DictionaryProvider( {'id': 'Materials'}, materials_data, uri_generator=UriPatternGenerator( 'https://id.erfgoed.net/thesauri/materialen/%s'), conceptscheme=ConceptScheme( uri='https://id.erfgoed.net/thesauri/materialen', labels=[ Label(type='prefLabel', language='nl', label='Materialen') ], notes=[ Note( type='scopeNote', language='nl', note= 'Materialen zijn grondstoffen of halfafgewerkte producten die vaak een rol spelen bij onroerend erfgoed.' ) ])) return materials
def conceptscheme_from_uri(conceptscheme_uri, **kwargs): ''' Read a SKOS Conceptscheme from a :term:`URI` :param string conceptscheme_uri: URI of the conceptscheme. :rtype: skosprovider.skos.ConceptScheme ''' s = kwargs.get('session', requests.Session()) graph = uri_to_graph('%s.rdf' % (conceptscheme_uri), session=s) notes = [] labels = [] if graph is not False: for s, p, o in graph.triples( (URIRef(conceptscheme_uri), RDFS.label, None)): label = Label(o.toPython(), "prefLabel", 'en') labels.append(label) for s, p, o in graph.triples( (URIRef(conceptscheme_uri), DCTERMS.description, None)): note = Note(o.toPython(), "scopeNote", 'en') notes.append(note) # get the conceptscheme conceptscheme = ConceptScheme(conceptscheme_uri, labels=labels, notes=notes) return conceptscheme
def test_sortlabel_broader(self): ''' Test that a broader sortlabel gets picked up for a regional sort. ''' kh = self._get_knokke_heist_nl() ch = self._get_cnocke_heyst_nl() sl = Label('Eerst', type='sortLabel', language='nl') labels = [kh, ch, sl] assert sl == label(labels, 'nl-BE', True)
def testSortKey(self): labels = self._get_labels() sl = Label('allereerste', type='sortLabel', language='nl-BE') labels.append(sl) cs = ConceptScheme(uri='urn:x-skosprovider:gemeenten', labels=labels) self.assertEqual('allereerste', cs._sortkey('sortlabel')) self.assertEqual('allereerste', cs._sortkey('sortlabel', 'nl')) self.assertEqual('communities', cs._sortkey('sortlabel', 'en')) self.assertEqual('urn:x-skosprovider:gemeenten', cs._sortkey('uri'))
def testSortKey(self): labels = self._get_labels() sl = Label('allereerste', type='sortLabel', language='nl-BE') labels.append(sl) c = Concept(1, labels=labels) self.assertEqual('allereerste', c._sortkey('sortlabel')) self.assertEqual('allereerste', c._sortkey('sortlabel', 'nl')) self.assertEqual('knocke-heyst', c._sortkey('sortlabel', 'en')) self.assertEqual('', c._sortkey('uri'))
def testSortkey(self): labels = self._get_labels() sl = Label('allereerste', type='sortLabel', language='nl-BE') labels.append(sl) coll = Collection(350, labels=labels) self.assertEqual('allereerste', coll._sortkey('sortlabel')) self.assertEqual('allereerste', coll._sortkey('sortlabel', 'nl')) self.assertEqual('allereerste', coll._sortkey('sortlabel', 'en')) self.assertEqual('deelgemeenten', coll._sortkey('label', 'nl')) self.assertEqual('', coll._sortkey('uri'))
def _create_from_subject_predicate(self, subject, predicate): list = [] for s, p, o in self.graph.triples((subject, predicate, None)): type = predicate.split('#')[-1] if Label.is_valid_type(type): o = self._create_label(o, type) elif Note.is_valid_type(type): o = self._create_note(o, type) else: o = self._get_id_for_subject(o, self.to_text(o)) list.append(o) return list
def test_get_conceptscheme(self): request = self._get_dummy_request() request.matchdict = {'scheme_id': 'TREES'} pv = self._get_provider_view(request) cs = pv.get_conceptscheme() self.assertEqual( { 'id': 'TREES', 'uri': 'http://python.com/trees', 'label': 'Different types of trees', 'subject': [], 'labels': [ Label('Different types of trees', 'prefLabel', 'en'), Label('Verschillende soorten bomen', 'prefLabel', 'nl') ], 'notes': [], 'sources': [], 'languages': [] }, cs )
def _create_from_subject_predicate(graph, subject, predicate): list = [] for s, p, o in graph.triples((subject, predicate, None)): type = predicate.split('#')[-1] if Label.is_valid_type(type): o = _create_label(o, type) elif Note.is_valid_type(type): o = _create_note(o, type) else: o = _split_uri(o, 1) if o: list.append(o) return list
def _create_from_subject_predicate(graph, subject, predicate, note_uris=None): list = [] for s, p, o in graph.triples((subject, predicate, None)): type = predicate.split('#')[-1] if Label.is_valid_type(type): o = _create_label(o, type) elif Note.is_valid_type(type): if o.toPython() not in note_uris: note_uris.append(o.toPython()) o = _create_note(graph, o, type, False) else: o = None else: o = uri_to_id(o) if o: list.append(o) return list
def _get_und(self): return Label('nokke-eist', type='prefLabel', language='und')
def _get_knokke_heist_en(self): return Label('Knocke-Heyst', type="prefLabel", language='en-GB')
def _get_cnocke_heyst_nl(self): return Label('Cnock-Heyst', type="altLabel", language='nl-BE')
def _get_knokke_heist_nl(self): return Label('Knokke-Heist', type="prefLabel", language='nl-BE')
def testConstructor(self): l = Label('Knokke-Heist', type="prefLabel", language='nl-BE') self.assertEqual('Knokke-Heist', l.label) self.assertEqual('prefLabel', l.type) self.assertEqual('nl-BE', l.language)
def testRepr(self): l = Label('Knokke-Heist', type="prefLabel", language='nl-BE') self.assertEqual("Label('Knokke-Heist', 'prefLabel', 'nl-BE')", l.__repr__())
def _get_prefusiegemeenten_nl(self): return Label('Prefusiegemeenten', type="altLabel", language='nl-BE')
def testIsValidType(self): self.assertTrue(Label.is_valid_type('prefLabel')) self.assertFalse(Label.is_valid_type('voorkeursLabel')) l = Label('Knokke-Heist') self.assertTrue(l.is_valid_type('prefLabel'))
def _get_deelgemeenten_nl(self): return Label('Deelgemeenten', type="prefLabel", language='nl-BE')
def testIsValidType(self): self.assertTrue(Note.is_valid_type('note')) self.assertFalse(Label.is_valid_type('notitie')) n = Note('A community in West-Flanders.', 'definition', 'en') self.assertTrue(n.is_valid_type('definition'))
def testConstructorInvalidLanguage(self): with self.assertRaises(ValueError): l = Label('Knokke-Heist', type="prefLabel", language='nederlands') l = Label('Knokke-Heist', type='prefLabel', language=None) assert l.language == 'und'
def _get_sortlabel(self): return Label('allereerste', type='sortLabel', language='nl-BE')
def testInequality(self): l1 = Label('Knokke-Heist') l2 = Label('Knokke', 'altLabel') self.assertNotEqual(l1, l2)
def testDictToLabelWithlabel(self): l = dict_to_label(Label('A label.', 'prefLabel')) self.assertEqual('A label.', l.label) self.assertEqual('prefLabel', l.type) self.assertEqual('und', l.language)
def testDictEquality(self): l1 = Label('Knokke-Heist') l2 = {'label': 'Knokke-Heist', 'type': 'prefLabel', 'language': 'und'} self.assertEqual(l1, l2)
def testEquality(self): l1 = Label('Knokke-Heist') l2 = Label('Knokke-Heist', 'prefLabel', 'und') self.assertEqual(l1, l2)
def testDictInequality(self): l1 = Label('Knokke-Heist') l2 = {'label': 'Knokke', 'type': 'altLabel', 'language': None} self.assertNotEqual(l1, l2)
def _get_communities_en(self): return Label('Communities', type="prefLabel", language='en')