Exemplo n.º 1
0
 def testConstructorInvalidLanguage(self):
     with self.assertRaises(ValueError):
         n = Note('Een gemeente in West-Vlaanderen.',
                  type="note",
                  language='nederlands')
     n = Note('Een gemeente in West-Vlaanderen.',
              type="note",
              language=None)
     assert n.language == 'und'
Exemplo n.º 2
0
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
Exemplo n.º 3
0
 def testConstructor(self):
     n = Note('Een gemeente in West-Vlaanderen.',
              type="note",
              language='nl-BE')
     self.assertEqual('Een gemeente in West-Vlaanderen.', n.note)
     self.assertEqual('note', n.type)
     self.assertEqual('nl-BE', n.language)
Exemplo n.º 4
0
    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])
Exemplo n.º 6
0
 def testDictInequality(self):
     n1 = Note('A note.')
     n2 = {
         'note': 'A note.',
         'type': 'definition',
         'language': 'und',
         'markup': None
     }
     self.assertNotEqual(n1, n2)
Exemplo n.º 7
0
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,
        )
Exemplo n.º 8
0
 def testConstructorWithHTML(self):
     n = Note('<p>Een gemeente in <em>West-Vlaanderen</em>.</p>',
              type="note",
              language='nl-BE',
              markup='HTML')
     self.assertEqual('<p>Een gemeente in <em>West-Vlaanderen</em>.</p>',
                      n.note)
     self.assertEqual('note', n.type)
     self.assertEqual('nl-BE', n.language)
     self.assertEqual('HTML', n.markup)
Exemplo n.º 9
0
 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
Exemplo n.º 10
0
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):
    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
Exemplo n.º 12
0
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
Exemplo n.º 13
0
 def testEquality(self):
     n1 = Note('A note.')
     n2 = Note('A note.', 'note', 'und')
     self.assertEqual(n1, n2)
Exemplo n.º 14
0
 def testConstructorInvalidMarkup(self):
     with self.assertRaises(ValueError):
         n = Note('Een gemeente in West-Vlaanderen.',
                  type="note",
                  language='nl',
                  markup='markdown')
Exemplo n.º 15
0
    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)
Exemplo n.º 16
0
 def testIsValidMarkup(self):
     self.assertTrue(Note.is_valid_markup('HTML'))
     self.assertFalse(Note.is_valid_markup('markdown'))
     n = Note('A community in West-Flanders.', 'definition', 'en', None)
     self.assertTrue(n.is_valid_markup(None))
Exemplo n.º 17
0
 def testDictToNodeWithNote(self):
     d = dict_to_note(Note('A note.', 'note'))
     self.assertEqual('A note.', d.note)
     self.assertEqual('note', d.type)
     self.assertEqual('und', d.language)
Exemplo n.º 18
0
 def testIsValidMarkup(self):
     self.assertTrue(Note.is_valid_markup('HTML'))
     self.assertFalse(Note.is_valid_markup('markdown'))
     n = Note('A community in West-Flanders.', 'definition', 'en', None)
     self.assertTrue(n.is_valid_markup(None))
Exemplo n.º 19
0
 def testIsValidType(self):
     self.assertTrue(Note.is_valid_type('note'))
     self.assertFalse(Note.is_valid_type('notitie'))
     n = Note('A community in West-Flanders.', 'definition', 'en')
     self.assertTrue(n.is_valid_type('definition'))
Exemplo n.º 20
0
def _create_note(literal, type):
    if not Note.is_valid_type(type):
        raise ValueError('Type of Note is not valid.')

    return Note(text_(literal.value, encoding="utf-8"), type,
                _get_language_from_literal(literal))
Exemplo n.º 21
0
 def testDictEquality(self):
     n1 = Note('A note.')
     n2 = {'note': 'A note.', 'type': 'note', 'language': 'und'}
     self.assertEqual(n1, n2)
Exemplo n.º 22
0
 def testInEquality(self):
     n1 = Note('A note.')
     n2 = Note('A note.', 'definition', 'und')
     self.assertNotEqual(n1, n2)
Exemplo n.º 23
0
 def _create_note(self, literal, type):
     if not Note.is_valid_type(type):
         raise ValueError('Type of Note is not valid.')
     l = self._read_markupped_literal(literal)
     return Note(self.to_text(l[0]), type, l[1], l[2])
    def _from_thing(self, thing):
        '''
        Load one concept or collection from the database.

        :param :class:`skosprovider_sqlalchemy.models.Thing` thing: Thing
            to load.
        '''
        if thing.type and thing.type == 'collection':
            return Collection(
                id=thing.concept_id,
                uri=thing.uri
                if thing.uri is not None else self.uri_generator.generate(
                    type='collection', id=thing.concept_id),
                concept_scheme=self.concept_scheme,
                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, n.markup)
                    for n in thing.notes
                ],
                sources=[Source(s.citation, s.markup) for s in thing.sources],
                members=[member.concept_id for member in thing.members]
                if hasattr(thing, 'members') else [],
                member_of=[
                    member_of.concept_id for member_of 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 not key in matches:
                    matches[key] = []
                matches[key].append(m.uri)
            return Concept(
                id=thing.concept_id,
                uri=thing.uri
                if thing.uri is not None else self.uri_generator.generate(
                    type='concept', id=thing.concept_id),
                concept_scheme=self.concept_scheme,
                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, n.markup)
                    for n in thing.notes
                ],
                sources=[Source(s.citation, s.markup) 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=[
                    member_of.concept_id for member_of in thing.member_of
                ],
                subordinate_arrays=[
                    narrower_collection.concept_id
                    for narrower_collection in thing.narrower_collections
                ],
                matches=matches)
def _create_note(literal, type):
    if not Note.is_valid_type(type):
        raise ValueError('Type of Note is not valid.')

    return Note(text_(literal.value, encoding="utf-8"), type, _get_language_from_literal(literal))
Exemplo n.º 26
0
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')
Exemplo n.º 27
0
 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'))