def testLabel(self): labels = self._get_labels() cs = ConceptScheme(uri='urn:x-skosprovider:gemeenten', labels=labels) self.assertEqual(label(labels), cs.label()) self.assertEqual(label(labels, 'nl'), cs.label('nl')) self.assertEqual(label(labels, 'en'), cs.label('en')) self.assertEqual(label(labels, None), cs.label(None))
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 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 testLabel(self): labels = self._get_labels() cs = ConceptScheme( uri='urn:x-skosprovider:gemeenten', labels=labels ) self.assertEqual(label(labels), cs.label()) self.assertEqual(label(labels, 'nl'), cs.label('nl')) self.assertEqual(label(labels, 'en'), cs.label('en')) self.assertEqual(label(labels, None), cs.label(None))
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 _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 _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 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 testSource(self): cs = ConceptScheme(uri='urn:x-skosprovider:gemeenten', sources=[{ 'citation': 'My citation' }]) self.assertEqual(1, len(cs.sources)) self.assertIsInstance(cs.sources[0], Source) self.assertEqual('My citation', cs.sources[0].citation)
def setUp(self): from skosprovider.uri import UriPatternGenerator self.ifile = open( os.path.join(os.path.dirname(__file__), 'data', 'menu.csv'), "r") reader = csv.reader(self.ifile) self.csvprovider = SimpleCsvProvider( {'id': 'MENU'}, reader, uri_generator=UriPatternGenerator('http://id.python.org/menu/%s'), concept_scheme=ConceptScheme('http://id.python.org/menu'))
def test_register_provider_wrong_scope(self): from skosprovider.skos import ConceptScheme from skosprovider.providers import DictionaryProvider trees = DictionaryProvider( {'id': 'TREES', 'default_language': 'nl'}, [larch, chestnut, species], concept_scheme=ConceptScheme('urn:something'), allowed_instance_scopes = ['threaded_thread'] ) with pytest.raises(RegistryException): self.reg.register_provider(trees)
def _cs_from_graph(self, metadata): cslist = [] for sub in self.graph.subjects(RDF.type, SKOS.ConceptScheme): uri = self.to_text(sub) cs = ConceptScheme(uri=uri, labels=self._create_from_subject_typelist( sub, Label.valid_types), notes=self._create_from_subject_typelist( sub, Note.valid_types), sources=self._create_sources(sub), languages=self._create_languages(sub)) cslist.append(cs) if len(cslist) == 0: return ConceptScheme( uri=DefaultConceptSchemeUrnGenerator().generate( id=metadata.get('id'))) elif len(cslist) == 1: return cslist[0] else: raise RuntimeError( 'This RDF file contains more than one ConceptScheme.')
def test_one_provider_getConceptByUriDifferentFromConceptScheme(self): from skosprovider.skos import ConceptScheme from skosprovider.providers import DictionaryProvider trees = DictionaryProvider( {'id': 'TREES', 'default_language': 'nl'}, [larch, chestnut, species], concept_scheme=ConceptScheme('urn:something') ) self.reg.register_provider(trees) c = self.reg.get_by_uri('http://id.trees.org/1') self.assertEqual(c.id, '1') self.assertEqual(c.uri, 'http://id.trees.org/1')
def test_override_get_vocabulary_uri(self): trees = DictionaryProvider( { 'id': 'TREES', 'uri': 'http://id.trees.org', 'default_language': 'nl', 'subject': ['biology'], 'dataset': { 'uri': 'http://id.trees.org/dataset' } }, [larch, chestnut, species], concept_scheme=ConceptScheme('http://idtoo.trees.org', labels=[{ 'type': 'prefLabel', 'language': 'nl', 'label': 'Soorten' }], languages=['nl', 'en'])) assert 'http://id.trees.org' == trees.get_vocabulary_uri() assert 'http://idtoo.trees.org' == trees.concept_scheme.uri
def _get_heritage_types(): import json typology_data = json.load( open(os.path.join(os.path.dirname(__file__), 'data', 'typologie.js')), )['typologie'] from skosprovider.providers import DictionaryProvider from skosprovider.uri import UriPatternGenerator from skosprovider.skos import ConceptScheme heritage_types = DictionaryProvider( {'id': 'HERITAGE_TYPES'}, typology_data, uri_generator=UriPatternGenerator( 'https://id.erfgoed.net/thesauri/erfgoedtypes/%s'), concept_scheme=ConceptScheme( uri='https://id.erfgoed.net/thesauri/erfgoedtypes', labels=[{ 'label': 'Erfgoedtypes', 'type': 'prefLabel', 'language': 'nl-BE' }, { 'label': 'Heritagetypes', 'type': 'prefLabel', 'language': 'en' }], notes=[{ 'note': 'Different types of heritage.', 'type': 'definition', 'language': 'en' }, { 'note': 'Verschillende types van erfgoed.', 'type': 'definition', 'language': 'nl' }], languages=['nl', 'en'])) return heritage_types
'type': 'prefLabel', 'language': 'nl', 'label': 'Bomen per soort' }, { 'type': 'prefLabel', 'language': 'nl', 'label': 'b' }], 'notes': [{ 'type': 'scopeNote', 'language': 'en', 'note': 'A division of trees.' }], 'type': 'collection', 'members': ['1', '2'] } trees = DictionaryProvider({ 'id': 'TREES', 'default_language': 'nl' }, [larch, chestnut, species], concept_scheme=ConceptScheme( uri='http://python.com/trees', labels=[ Label('Different types of trees', 'prefLabel', 'en'), Label('Verschillende soorten bomen', 'prefLabel', 'nl') ]))
def _create_test_data(self): self.graph = Graph() filepath = os.path.dirname(os.path.realpath(__file__)) abspath = os.path.abspath(filepath + "/data/simple_turtle_products") self.graph.parse(abspath, format="turtle") self.u_products = URIRef("http://www.products.com/") self.u_jewellery = URIRef("http://www.products.com/Jewellery") self.u_perfume = URIRef("http://www.products.com/Perfume") self.u_product = URIRef("http://www.products.com/Product") self.u_stuff = URIRef("http://www.products.com/Stuff") self.larch_dump = { 'id': '1', 'uri': 'http://id.trees.org/1', 'type': 'concept', 'labels': [{ 'type': 'prefLabel', 'language': 'en', 'label': 'The Larch' }, { 'type': 'prefLabel', 'language': 'nl', 'label': 'De Lariks' }], 'notes': [{ 'type': 'definition', 'language': 'en', 'note': 'A type of tree.' }, { 'type': 'definition', 'language': 'nl', 'note': '<p>Een soort boom.</p>', 'markup': 'HTML' }], 'narrower': [], 'broader': [], 'related': [], 'member_of': ['3'], 'sources': [{ 'citation': 'Monthy Python. Episode Three: How to recognise different types of trees from quite a long way away.' }] } self.chestnut_dump = { 'id': '2', 'uri': 'http://id.trees.org/2', 'type': 'concept', 'labels': [{ 'type': 'prefLabel', 'language': 'en', 'label': 'The Chestnut' }, { 'type': 'altLabel', 'language': 'nl', 'label': 'De Paardekastanje' }, { 'type': 'altLabel', 'language': 'fr', 'label': 'la châtaigne' }], 'notes': [{ 'type': 'definition', 'language': 'en', 'note': 'A different type of tree.' }], 'narrower': [], 'broader': [], 'related': [], 'member_of': ['3'], 'subordinate_arrays': [], 'sources': [{ 'citation': '<strong>Monthy Python.</strong> Episode Three: How to recognise different types of trees from quite a long way away.', 'markup': 'HTML' }] } self.oak_dump = { 'id': '4', 'uri': 'http://id.trees.org/4', 'type': 'concept', 'labels': [{ 'type': 'prefLabel', 'language': 'en', 'label': 'The Oak' }, { 'type': 'altLabel', 'language': 'nl', 'label': 'De Eik' }, { 'type': 'altLabel', 'language': 'fr', 'label': 'le chêne' }], 'notes': [{ 'type': 'definition', 'language': 'en', 'note': 'A different type of tree.' }], 'narrower': ['6'], 'broader': ['6'], 'related': ['6'], 'member_of': ['6'], 'subordinate_arrays': ['6', '3'], 'matches': { 'exact': ['http://blabla/2'], 'narrow': ['http://blabla/1', 'http://blabla/5'], } } self.species_dump = { 'id': 3, 'uri': 'http://id.trees.org/3', 'labels': [{ 'type': 'prefLabel', 'language': 'en', 'label': 'Trees by species' }, { 'type': 'prefLabel', 'language': 'nl', 'label': 'Bomen per soort' }], 'type': 'collection', 'members': ['1', '2'], 'member_of': [], 'superordinates': ['6', '4'] } self.world_dump = { 'id': '1', 'uri': 'urn:x-skosprovider:geography:1', 'type': 'concept', 'labels': [{ 'type': 'prefLabel', 'language': 'en', 'label': 'World' }], 'notes': [], 'narrower': [2, 3], 'broader': [], 'related': [], 'member_of': [], 'subordinate_arrays': [] } self.tree_provider = DictionaryProvider( { 'id': 'TREE', 'dataset': { 'uri': 'https://id.trees.org/dataset' } }, [self.larch_dump, self.chestnut_dump, self.species_dump], concept_scheme=ConceptScheme( uri='http://id.trees.org', labels=[ Label('Pythonic trees.', type='prefLabel', language='en'), Label('Pythonische bomen.', type='prefLabel', language=None) ], notes=[ Note('<p>Trees as used by Monthy Python.</p>', type='definition', language='en', markup='HTML') ])) self.tree_provider2 = DictionaryProvider( {'id': 'TREE'}, [self.oak_dump, self.chestnut_dump, self.species_dump]) self.world_provider = DictionaryProvider({'id': 'WORLD'}, [self.world_dump]) # Set up rdf_provider self.rdf_products_provider = RDFProvider( { 'id': 'PRODUCTS', 'conceptscheme_id': 1 }, self.graph)
False } trees = DictionaryProvider( { 'id': 'TREES', 'default_language': 'nl', 'subject': ['biology'], 'dataset': { 'uri': 'http://id.trees.org/dataset' } }, [larch, chestnut, species], concept_scheme=ConceptScheme('http://id.trees.org', labels=[{ 'type': 'prefLabel', 'language': 'nl', 'label': 'Soorten' }], languages=['nl', 'en'])) world = { 'id': '1', 'uri': None, 'labels': [{ 'type': 'prefLabel', 'language': 'en', 'label': 'World' }], 'narrower': [2, 3] }
def testEmptyUri(self): with pytest.raises(ValueError): cs = ConceptScheme(uri=None)
def testLanguages(self): labels = self._get_labels() cs = ConceptScheme(uri='urn:x-skosprovider:gemeenten', labels=labels, languages=['nl', 'en', 'und']) self.assertEquals(cs.languages, ['nl', 'en', 'und'])
def testRepr(self): cs = ConceptScheme( uri='urn:x-skosprovider:gemeenten' ) self.assertEqual("ConceptScheme('urn:x-skosprovider:gemeenten')", cs.__repr__())
from skosprovider.skos import ConceptScheme, Label, Note, Source from skosprovider_rdf.utils import rdf_dumper ifile = open(os.path.join(os.path.dirname(__file__), 'data', 'menu.csv'), "r") reader = csv.reader(ifile) csvprovider = SimpleCsvProvider( {'id': 'MENU'}, reader, uri_generator=UriPatternGenerator('http://id.python.org/menu/%s'), concept_scheme=ConceptScheme( uri='http://id.python.org/menu', labels=[ Label(type='prefLabel', language='en', label='A pythonesque menu.') ], notes=[ Note( type='changeNote', language='en', note= "<strong>We didn't need no change notes when I was younger.</strong>", markup='HTML') ], sources=[Source("Monthy Python's Flying Circus, 1970. Spam.")])) graph = rdf_dumper(csvprovider) print graph.serialize(format='n3')
def testRepr(self): cs = ConceptScheme(uri='urn:x-skosprovider:gemeenten') self.assertEquals("ConceptScheme('urn:x-skosprovider:gemeenten')", cs.__repr__())
'language': 'en', 'note': 'As seen in <em>How to Recognise Different Types of Trees from Quite a Long Way Away</em>.', 'markup': 'HTML' } ] } provider = DictionaryProvider( { 'id': 'TREES', 'default_language': 'nl', 'subject': ['biology'] }, [larch, chestnut, species], uri_generator=UriPatternGenerator('http://id.trees.org/types/%s'), concept_scheme=ConceptScheme('http://id.trees.org') ) # Get a concept or collection by id print(provider.get_by_id(1).label().label) # Get a concept or collection by uri print(provider.get_by_uri('http://id.trees.org/types/1')) # Get all concepts and collections in a provider # If possible, show a Dutch(as spoken in Belgium) label # Order them ascending by label print(provider.get_all(language='nl-BE', sort='label', sort_order='asc')) # Get the top concepts in a provider print(provider.get_top_concepts())