Example #1
0
def test_list():
    c1 = Concept('_666')
    c2 = Concept('_999')
    l1 = Label('foo', 'en')
    l2 = Label('bar', 'de')

    lines = txt.list_concepts([c1, c2])
    lines = ''.join(lines)
    assert lines == '_666\n_999\n'

    c1.pref_labels.append(l1)

    lines = txt.list_concepts([c1, c2])
    lines = ''.join(lines)
    assert lines == 'foo\n_999\n'

    c1.alt_labels.append(l2)

    lines = txt.list_concepts([c1, c2])
    lines = ''.join(lines)
    assert lines == 'foo\n_999\n'

    c2.alt_labels.append(l1)

    lines = txt.list_concepts([c1, c2])
    lines = ''.join(lines)
    assert lines == 'foo\n_999\n'
Example #2
0
    def test_build_df(self):
        concepts_list = [
            Concept(0, 'title0', ['a', 'b', 'c']),
            Concept(1, 'title1', ['b', 'c']),
            Concept(2, 'title2', ['x', 'c']),
        ]
        index_by_word = {'a': 0, 'b': 1, 'c': 2, 'x': 3}

        expected = [1, 2, 3, 1]
        actual = dbb.build_df(index_by_word, concepts_list)
        self.assertEqual(expected, actual)
Example #3
0
    def test_bulid_word_index(self):
        concepts_list = [
            Concept(0, 'title0', ['a', 'b', 'c']),
            Concept(1, 'title1', ['b', 'c']),
            Concept(2, 'title2', ['x', 'c']),
        ]

        expected = ['a', 'b', 'c', 'x']
        actual = dbb.build_word_index(concepts_list)

        self.assertEqual(set(expected), set(actual))
Example #4
0
def json_to_concept(json_data):
    """Convert a JSON dict into a Concept object."""
    concept_obj = Concept(fact=json_to_fact(json_data['fact']),
                          user_id=json_data['userId'],
                          concept_id=json_data['id'],
                          creation_timestamp=json_data['createdAt'],
                          update_timestamp=json_data['updatedAt'])

    return concept_obj
Example #5
0
def get_creator(environ, start_response):
    response_headers = [('Content-Type', 'text/html')]
    start_response(HTTP['200'], response_headers)
    template = ENV.get_template('edit_concept.html')
    return template.generate(
        title='Concept Creator',
        concept=Concept(),
        root_uri='/',  # XXX: root_uri hardcoded; breaks encapsulation
        form_uri=environ['SCRIPT_NAME'])  # XXX: SCRIPT_NAME correct?
Example #6
0
def _seed():
    import json

    print 'INFO: seeding database'  # TODO: use logger
    with open('data.json') as fp:
        for doc in json.load(fp):
            concept = Concept().from_document(doc)
            STORE.add(concept)
            print concept._id, concept
Example #7
0
    def retrieve(self, collection, *args, **kwargs):
        """
        collection argument is a string, remaining arguments are passed through
        to PyMongo's `find` method
        """
        if collection not in self.collections.values():
            raise ValueError('invalid collection')

        # XXX: hard-coding concepts for now
        # XXX: association with models is dangerous also because we might query only for a limited selection of fields
        return (Concept().from_document(doc)
                for doc in getattr(self.db, collection).find(*args, **kwargs))
Example #8
0
    def __init__(self, front, back, user_id, card_id=None):
        """Initialize a new instance of the Card class."""
        self.id = card_id if card_id else str(uuid4())
        self.user_id = user_id

        # While Tinycards originally uses a (2 element) tuple to
        # represent both sides, we chose to go with a more
        # semantic naming here.
        if type(front) is Side:
            self.front = front
        elif type(front) is str:
            self.front = Side(concepts=Concept(Fact(front), self.user_id),
                              user_id=self.user_id)
        else:
            raise ValueError("Front property can only be of type Side")
        if type(back) is Side:
            self.back = back
        elif type(back) is str:
            self.back = Side(concepts=Concept(Fact(back), self.user_id),
                             user_id=self.user_id)
        else:
            raise ValueError("Back property can only be of type Side")
Example #9
0
def test_show():
    c0 = Concept('_666')
    l1 = Label('foo', 'en')
    l2 = Label('bar', 'de')

    lines = txt.show_concept(c0)
    lines = ''.join(lines)
    assert lines == '_666\n\nPREFERRED LABELS\n\nALTERNATIVE LABELS\n'

    c0.pref_labels.append(l1)

    lines = txt.show_concept(c0)
    lines = ''.join(lines)
    assert lines == ('_666\n\n'
                     'PREFERRED LABELS\n    [en] foo\n\n'
                     'ALTERNATIVE LABELS\n')

    c0.pref_labels.append(l2)

    lines = txt.show_concept(c0)
    lines = ''.join(lines)
    assert lines == ('_666\n\n'
                     'PREFERRED LABELS\n    [en] foo\n    [de] bar\n\n'
                     'ALTERNATIVE LABELS\n')

    c0.alt_labels.append(l2)

    lines = txt.show_concept(c0)
    lines = ''.join(lines)
    assert lines == ('_666\n\n'
                     'PREFERRED LABELS\n    [en] foo\n    [de] bar\n\n'
                     'ALTERNATIVE LABELS\n    [de] bar\n')

    c0.alt_labels.append(l1)

    lines = txt.show_concept(c0)
    lines = ''.join(lines)
    assert lines == ('_666\n\nPREFERRED LABELS\n    [en] foo\n    [de] bar\n\n'
                     'ALTERNATIVE LABELS\n    [de] bar\n    [en] foo\n')
Example #10
0
def post_creator(environ, start_response):
    content_type = environ.get('CONTENT_TYPE', '')
    if not content_type == 'application/x-www-form-urlencoded':  # XXX: TiddlyWeb uses startswith here!?
        raise HTTP415

    # TODO: this might be encapsulated in middleware (cf. tiddlyweb.web.query)
    content_length = int(environ['CONTENT_LENGTH'] or 0)
    content = environ['wsgi.input'].read(content_length)
    data = parse_qs(content, keep_blank_values=True)

    concept = Concept()
    # TODO: label language (input/selection currently missing from HTML template)
    for label_type in ['pref', 'alt']:
        key = '%s_labels' % label_type
        for name in data[key]:
            if name:
                label = Label(name, lang=None)
                getattr(concept, key).append(label)

    _id = STORE.add(concept)

    response_headers = [('Location', '/concepts/%s' % concept._id)]
    start_response(HTTP['302'], response_headers)
    return ['']
Example #11
0
    def get_concept(self):
        uri = self.request.values.get("uri")
        q = """PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
            PREFIX dct: <http://purl.org/dc/terms/>
            SELECT *
            WHERE {{
              <{0}> skos:prefLabel ?pl .
              OPTIONAL {{ <{0}> skos:definition ?d }}
              OPTIONAL {{ <{0}> dct:created ?created }}
              OPTIONAL {{ <{0}> dct:modified ?modified }}
            }}""".format(uri)
        self.s = VocBench("x", self.request)._authed_request_object()
        r = self.s.post(
            config.VB_ENDPOINT + "/SPARQL/evaluateQuery",
            data={
                "query": q,
                "ctx_project": self.vocab_uri
            },
        )
        metadata = json.loads(r.content.decode(
            "utf-8"))["result"]["sparql"]["results"]["bindings"][0]

        # get the concept's altLabels
        q = """PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
            SELECT *
            WHERE {{
              <{}> skos:altLabel ?al .
            }}""".format(uri)
        r = self.s.post(
            config.VB_ENDPOINT + "/SPARQL/evaluateQuery",
            data={
                "query": q,
                "ctx_project": self.vocab_uri
            },
        )
        altLabels = json.loads(r.content.decode(
            "utf-8"))["result"]["sparql"]["results"]["bindings"]

        # get the concept's hiddenLabels
        q = """PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
            SELECT *
            WHERE {{
              <{}> skos:hiddenLabel ?hl .
            }}""".format(uri)
        r = self.s.post(
            config.VB_ENDPOINT + "/SPARQL/evaluateQuery",
            data={
                "query": q,
                "ctx_project": self.vocab_uri
            },
        )
        hiddenLabels = json.loads(r.content.decode(
            "utf-8"))["result"]["sparql"]["results"]["bindings"]

        # get the concept's broaders
        q = """ PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
            SELECT *
            WHERE {{
              <{}> skos:broader ?b .
              ?b skos:prefLabel ?pl .
            }}""".format(uri)
        r = self.s.post(
            config.VB_ENDPOINT + "/SPARQL/evaluateQuery",
            data={
                "query": q,
                "ctx_project": self.vocab_uri
            },
        )
        broaders = json.loads(r.content.decode(
            "utf-8"))["result"]["sparql"]["results"]["bindings"]

        # get the concept's narrowers
        q = """PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
            SELECT *
            WHERE {{
              <{}> skos:narrower ?n .
              ?n skos:prefLabel ?pl .
            }}""".format(uri)
        r = self.s.post(
            config.VB_ENDPOINT + "/SPARQL/evaluateQuery",
            data={
                "query": q,
                "ctx_project": self.vocab_uri
            },
        )
        narrowers = json.loads(r.content.decode(
            "utf-8"))["result"]["sparql"]["results"]["bindings"]

        # get exactMatch
        q = """PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
            SELECT *
            WHERE {{
                <{}> skos:exactMatch ?s .
            }}""".format(uri)
        r = self.s.post(
            config.VB_ENDPOINT + "/SPARQL/evaluateQuery",
            data={
                "query": q,
                "ctx_project": self.vocab_uri
            },
        )
        exactMatches = json.loads(r.content.decode(
            "utf-8"))["result"]["sparql"]["results"]["bindings"]

        # get closeMatch
        q = """PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
                    SELECT *
                    WHERE {{
                        <{}> skos:closeMatch ?s .
                    }}""".format(uri)
        r = self.s.post(
            config.VB_ENDPOINT + "/SPARQL/evaluateQuery",
            data={
                "query": q,
                "ctx_project": self.vocab_uri
            },
        )
        closeMatches = json.loads(r.content.decode(
            "utf-8"))["result"]["sparql"]["results"]["bindings"]

        # get broadMatch
        q = """PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
                            SELECT *
                            WHERE {{
                                <{}> skos:broadMatch ?s .
                            }}""".format(uri)
        r = self.s.post(
            config.VB_ENDPOINT + "/SPARQL/evaluateQuery",
            data={
                "query": q,
                "ctx_project": self.vocab_uri
            },
        )
        broadMatches = json.loads(r.content.decode(
            "utf-8"))["result"]["sparql"]["results"]["bindings"]

        # get narrowMatch
        q = """PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
                                    SELECT *
                                    WHERE {{
                                        <{}> skos:narrowMatch ?s .
                                    }}""".format(uri)
        r = self.s.post(
            config.VB_ENDPOINT + "/SPARQL/evaluateQuery",
            data={
                "query": q,
                "ctx_project": self.vocab_uri
            },
        )
        narrowMatches = json.loads(r.content.decode(
            "utf-8"))["result"]["sparql"]["results"]["bindings"]

        # get relatedMatch
        q = """PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
                                            SELECT *
                                            WHERE {{
                                                <{}> skos:relatedMatch ?s .
                                            }}""".format(uri)
        r = self.s.post(
            config.VB_ENDPOINT + "/SPARQL/evaluateQuery",
            data={
                "query": q,
                "ctx_project": self.vocab_uri
            },
        )
        relatedMatches = json.loads(r.content.decode(
            "utf-8"))["result"]["sparql"]["results"]["bindings"]

        from model.concept import Concept

        return Concept(
            self.vocab_uri,
            uri,
            metadata["pl"]["value"],
            metadata.get("d").get("value")
            if metadata.get("d") is not None else None,
            [x.get("al").get("value") for x in altLabels],
            [x.get("hl").get("value") for x in hiddenLabels],
            metadata.get("sc").get("value")
            if metadata.get("sc") is not None else None,
            metadata.get("cn").get("value")
            if metadata.get("cn") is not None else None,
            [{
                "uri": x.get("b").get("value"),
                "prefLabel": x.get("pl").get("value")
            } for x in broaders],
            [{
                "uri": x.get("n").get("value"),
                "prefLabel": x.get("pl").get("value")
            } for x in narrowers],
            [x["s"]["value"] for x in exactMatches],
            [x["s"]["value"] for x in closeMatches],
            [x["s"]["value"] for x in broadMatches],
            [x["s"]["value"] for x in narrowMatches],
            [x["s"]["value"] for x in relatedMatches],
            None,  # TODO: replace Sem Properties sub,
            metadata.get("created").get("value")[:10]
            if metadata.get("created") else None,
            metadata.get("modified").get("value")[:10]
            if metadata.get("modified") else None,
            source=self,
        )
Example #12
0
    def get_concept(self):
        concept_uri = self.request.values.get("uri")
        q = """
            PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
            PREFIX dct: <http://purl.org/dc/terms/>
            
            SELECT 
                ?predicate      # 0
                ?object         # 1
                ?predicateLabel # 2
                ?objectLabel    # 3
                ?prefLabel      # 4
                ?lang           # 5
            WHERE {{
                <{concept_uri}> ?predicate ?object .
                OPTIONAL {{
                    ?predicate rdfs:label ?predicateLabel .
                    FILTER(lang(?predicateLabel) = "{language}" || lang(?predicateLabel) = "")
                    BIND(lang(?predicateLabel) AS ?lang)
                }}
                OPTIONAL {{
                    ?object skos:prefLabel|rdfs:label ?objectLabel .
                    # Don't filter prefLabel language
                    FILTER(?prefLabel = skos:prefLabel || lang(?objectLabel) = "{language}" || lang(?objectLabel) = "") 
                    BIND(lang(?objectLabel) AS ?lang)
                }}
            }}
            """.format(concept_uri=concept_uri, language=self.language)

        result = self.gr.query(q)

        assert result, "FILE source is unable to query concepts for {}".format(
            self.request.values.get("uri"))

        prefLabel = None

        related_objects = {}

        for r in result:
            predicateUri = str(r[0])

            # Special case for prefLabels
            if predicateUri == "http://www.w3.org/2004/02/skos/core#prefLabel":
                predicateLabel = "Multilingual Labels"
                preflabel_lang = r[1].language if hasattr(r[1],
                                                          "language") else "en"

                # Use default language or no language prefLabel as primary
                if (not prefLabel
                        and not preflabel_lang) or (preflabel_lang
                                                    == self.language):
                    prefLabel = str(r[1])  # Set current language prefLabel

                # # Omit current language string from list (remove this if we want to show all)
                # if preflabel_lang in ['', self.language]:
                #     continue

                # Apend language code to prefLabel literal
                related_object = "{} ({})".format(str(r[1]), preflabel_lang)
                related_objectLabel = None
            else:
                predicateLabel = (str(r[2]) if r[2] is not None else
                                  h.make_title(str(r[0])))

                if not str(r[1]).startswith("http"):
                    related_object = str(r[1])
                    related_objectLabel = None
                else:
                    related_object = str(r[1])
                    related_objectLabel = (str(r[3]) if r[3] is not None else
                                           h.make_title(str(r[1])))

            relationship_dict = related_objects.get(predicateUri)
            if relationship_dict is None:
                relationship_dict = {"label": predicateLabel, "objects": {}}
                related_objects[predicateUri] = relationship_dict

            relationship_dict["objects"][related_object] = related_objectLabel

        related_objects = OrderedDict([(
            predicate,
            {
                "label":
                related_objects[predicate]["label"],
                "objects":
                OrderedDict([(key, related_objects[predicate]["objects"][key])
                             for key in sorted(related_objects[predicate]
                                               ["objects"].keys())]),
            },
        ) for predicate in sorted(related_objects.keys())])

        return Concept(
            vocab_id=self.vocab_id,
            uri=concept_uri,
            prefLabel=prefLabel,
            related_objects=related_objects,
            semantic_properties=None,
            source=self,
        )
Example #13
0
    def get_concept(self):
        concept_uri = self.request.values.get('uri')
        vocab = g.VOCABS[self.vocab_id]
        q = """PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX dct: <http://purl.org/dc/terms/>

select *

WHERE {{
    {{ GRAPH ?graph {{
        <{concept_uri}> ?predicate ?object .
        optional {{GRAPH ?predicateGraph {{?predicate rdfs:label ?predicateLabel .}} 
            FILTER(lang(?predicateLabel) = "{language}" || lang(?predicateLabel) = "")
            }}
        optional {{?object skos:prefLabel | rdfs:label ?objectLabel .
            FILTER(?prefLabel = skos:prefLabel || lang(?objectLabel) = "{language}" || lang(?objectLabel) = "") # Don't filter prefLabel language
        }}
    }} }}
    UNION
    {{
        <{concept_uri}> ?predicate ?object .
        optional {{GRAPH ?predicateGraph {{?predicate rdfs:label ?predicateLabel .}} 
            FILTER(lang(?predicateLabel) = "{language}" || lang(?predicateLabel) = "")
            }}
        optional {{?object skos:prefLabel | rdfs:label ?objectLabel .
            FILTER(?prefLabel = skos:prefLabel || lang(?objectLabel) = "{language}" || lang(?objectLabel) = "") # Don't filter prefLabel language
        }}
    }}
}}""".format(concept_uri=concept_uri, language=self.language)
        #print(q)
        result = Source.sparql_query(vocab.sparql_endpoint, q,
                                     vocab.sparql_username,
                                     vocab.sparql_password)

        assert result, 'Unable to query concepts for {}'.format(
            self.request.values.get('uri'))

        #print(str(result).encode('utf-8'))

        prefLabel = None

        related_objects = {}

        for row in result:
            predicateUri = row['predicate']['value']

            # Special case for prefLabels
            if predicateUri == 'http://www.w3.org/2004/02/skos/core#prefLabel':
                predicateLabel = 'Multilingual Labels'
                preflabel_lang = row['object'].get('xml:lang')

                # Use default language or no language prefLabel as primary
                if ((not prefLabel and not preflabel_lang)
                        or (preflabel_lang == self.language)):
                    prefLabel = row['object'][
                        'value']  # Set current language prefLabel

                # Omit current language string from list (remove this if we want to show all)
                if preflabel_lang in ['', self.language]:
                    continue

                # Apend language code to prefLabel literal
                related_object = '{} ({})'.format(row['object']['value'],
                                                  preflabel_lang)
                related_objectLabel = None
            else:
                predicateLabel = (row['predicateLabel']['value']
                                  if row.get('predicateLabel')
                                  and row['predicateLabel'].get('value') else
                                  make_title(row['predicate']['value']))

                if row['object']['type'] == 'literal':
                    related_object = row['object']['value']
                    related_objectLabel = None
                elif row['object']['type'] == 'uri':
                    related_object = row['object']['value']
                    related_objectLabel = (
                        row['objectLabel']['value'] if row.get('objectLabel')
                        and row['objectLabel'].get('value') else make_title(
                            row['object']['value']))

            relationship_dict = related_objects.get(predicateUri)
            if relationship_dict is None:
                relationship_dict = {'label': predicateLabel, 'objects': {}}
                related_objects[predicateUri] = relationship_dict

            relationship_dict['objects'][related_object] = related_objectLabel

        related_objects = OrderedDict([(predicate, {
            'label':
            related_objects[predicate]['label'],
            'objects':
            OrderedDict([
                (key, related_objects[predicate]['objects'][key])
                for key in sorted(related_objects[predicate]['objects'].keys())
            ])
        }) for predicate in sorted(related_objects.keys())])

        #print(repr(related_objects).encode('utf-8'))

        return Concept(
            vocab_id=self.vocab_id,
            uri=concept_uri,
            prefLabel=prefLabel,
            related_objects=related_objects,
            semantic_properties=None,
            source=self,
        )
    def get_concept(self, uri):
        q = '''PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
            PREFIX dct: <http://purl.org/dc/terms/>
            SELECT *
            WHERE {{
              <{0}> skos:prefLabel ?pl .
              OPTIONAL {{ <{0}> skos:definition ?d }}
              OPTIONAL {{ <{0}> dct:created ?created }}
              OPTIONAL {{ <{0}> dct:modified ?modified }}
            }}'''.format(uri)
        self.s = VOCBENCH('x', self.request)._authed_request_object()
        r = self.s.post(config.VB_ENDPOINT + '/SPARQL/evaluateQuery',
                        data={
                            'query': q,
                            'ctx_project': self.vocab_id
                        })
        metadata = json.loads(r.content.decode(
            'utf-8'))['result']['sparql']['results']['bindings'][0]

        # get the concept's altLabels
        q = '''PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
            SELECT *
            WHERE {{
              <{}> skos:altLabel ?al .
            }}'''.format(uri)
        r = self.s.post(config.VB_ENDPOINT + '/SPARQL/evaluateQuery',
                        data={
                            'query': q,
                            'ctx_project': self.vocab_id
                        })
        altLabels = json.loads(r.content.decode(
            'utf-8'))['result']['sparql']['results']['bindings']

        # get the concept's hiddenLabels
        q = '''PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
            SELECT *
            WHERE {{
              <{}> skos:hiddenLabel ?hl .
            }}'''.format(uri)
        r = self.s.post(config.VB_ENDPOINT + '/SPARQL/evaluateQuery',
                        data={
                            'query': q,
                            'ctx_project': self.vocab_id
                        })
        hiddenLabels = json.loads(r.content.decode(
            'utf-8'))['result']['sparql']['results']['bindings']

        # get the concept's broaders
        q = ''' PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
            SELECT *
            WHERE {{
              <{}> skos:broader ?b .
              ?b skos:prefLabel ?pl .
            }}'''.format(uri)
        r = self.s.post(config.VB_ENDPOINT + '/SPARQL/evaluateQuery',
                        data={
                            'query': q,
                            'ctx_project': self.vocab_id
                        })
        broaders = json.loads(r.content.decode(
            'utf-8'))['result']['sparql']['results']['bindings']

        # get the concept's narrowers
        q = '''PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
            SELECT *
            WHERE {{
              <{}> skos:narrower ?n .
              ?n skos:prefLabel ?pl .
            }}'''.format(uri)
        r = self.s.post(config.VB_ENDPOINT + '/SPARQL/evaluateQuery',
                        data={
                            'query': q,
                            'ctx_project': self.vocab_id
                        })
        narrowers = json.loads(r.content.decode(
            'utf-8'))['result']['sparql']['results']['bindings']

        # get exactMatch
        q = """PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
            SELECT *
            WHERE {{
                <{}> skos:exactMatch ?s .
            }}""".format(uri)
        r = self.s.post(config.VB_ENDPOINT + '/SPARQL/evaluateQuery',
                        data={
                            'query': q,
                            'ctx_project': self.vocab_id
                        })
        exactMatches = json.loads(r.content.decode(
            'utf-8'))['result']['sparql']['results']['bindings']

        # get closeMatch
        q = """PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
                    SELECT *
                    WHERE {{
                        <{}> skos:closeMatch ?s .
                    }}""".format(uri)
        r = self.s.post(config.VB_ENDPOINT + '/SPARQL/evaluateQuery',
                        data={
                            'query': q,
                            'ctx_project': self.vocab_id
                        })
        closeMatches = json.loads(r.content.decode(
            'utf-8'))['result']['sparql']['results']['bindings']

        # get broadMatch
        q = """PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
                            SELECT *
                            WHERE {{
                                <{}> skos:broadMatch ?s .
                            }}""".format(uri)
        r = self.s.post(config.VB_ENDPOINT + '/SPARQL/evaluateQuery',
                        data={
                            'query': q,
                            'ctx_project': self.vocab_id
                        })
        broadMatches = json.loads(r.content.decode(
            'utf-8'))['result']['sparql']['results']['bindings']

        # get narrowMatch
        q = """PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
                                    SELECT *
                                    WHERE {{
                                        <{}> skos:narrowMatch ?s .
                                    }}""".format(uri)
        r = self.s.post(config.VB_ENDPOINT + '/SPARQL/evaluateQuery',
                        data={
                            'query': q,
                            'ctx_project': self.vocab_id
                        })
        narrowMatches = json.loads(r.content.decode(
            'utf-8'))['result']['sparql']['results']['bindings']

        # get relatedMatch
        q = """PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
                                            SELECT *
                                            WHERE {{
                                                <{}> skos:relatedMatch ?s .
                                            }}""".format(uri)
        r = self.s.post(config.VB_ENDPOINT + '/SPARQL/evaluateQuery',
                        data={
                            'query': q,
                            'ctx_project': self.vocab_id
                        })
        relatedMatches = json.loads(r.content.decode(
            'utf-8'))['result']['sparql']['results']['bindings']

        from model.concept import Concept
        return Concept(
            self.vocab_id,
            uri,
            metadata['pl']['value'],
            metadata.get('d').get('value')
            if metadata.get('d') is not None else None,
            [x.get('al').get('value') for x in altLabels],
            [x.get('hl').get('value') for x in hiddenLabels],
            metadata.get('sc').get('value')
            if metadata.get('sc') is not None else None,
            metadata.get('cn').get('value')
            if metadata.get('cn') is not None else None,
            [{
                'uri': x.get('b').get('value'),
                'prefLabel': x.get('pl').get('value')
            } for x in broaders],
            [{
                'uri': x.get('n').get('value'),
                'prefLabel': x.get('pl').get('value')
            } for x in narrowers],
            [x['s']['value'] for x in exactMatches],
            [x['s']['value'] for x in closeMatches],
            [x['s']['value'] for x in broadMatches],
            [x['s']['value'] for x in narrowMatches],
            [x['s']['value'] for x in relatedMatches],
            None,  # TODO: replace Sem Properties sub,
            metadata.get('created').get('value')[:10]
            if metadata.get('created') else None,
            metadata.get('modified').get('value')[:10]
            if metadata.get('modified') else None,
        )
    def get_concept(self):
        vocab = g.VOCABS[self.vocab_id]
        q = """
            PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
            PREFIX dct: <http://purl.org/dc/terms/>
            PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            PREFIX dc: <http://purl.org/dc/elements/1.1/>
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            SELECT DISTINCT *
            WHERE  {{ GRAPH ?g {{
                {{ <{concept_uri}> skos:prefLabel ?prefLabel . # ?s skos:prefLabel|dct:title|rdfs:label ?prefLabel .
                    # FILTER(lang(?prefLabel) = "{language}" || lang(?prefLabel) = "") 
                    }}
                OPTIONAL {{ <{concept_uri}> skos:definition ?definition .
                    FILTER(lang(?definition) = "{language}" || lang(?definition) = "") }}
                OPTIONAL {{ <{concept_uri}> skos:altLabel ?altLabel .
                    FILTER(lang(?altLabel) = "{language}" || lang(?altLabel) = "") }}
                OPTIONAL {{ <{concept_uri}> skos:hiddenLabel ?hiddenLabel .
                    FILTER(lang(?hiddenLabel) = "{language}" || lang(?hiddenLabel) = "") }}
                OPTIONAL {{ <{concept_uri}> dct:source ?source .
                    FILTER(lang(?source) = "{language}" || lang(?source) = "") }}
                OPTIONAL {{ <{concept_uri}> dct:contributor ?contributor .
                    FILTER(lang(?contributor) = "{language}" || lang(?contributor) = "") }}
                OPTIONAL {{ <{concept_uri}> skos:broader ?broader .
                    OPTIONAL {{ ?broader skos:prefLabel ?broaderLabel .
                        FILTER(lang(?broaderLabel) = "{language}" || lang(?broaderLabel) = "") }} 
                    }}
                OPTIONAL {{ <{concept_uri}> skos:narrower ?narrower .
                    OPTIONAL {{ ?narrower skos:prefLabel ?narrowerLabel .
                        FILTER(lang(?narrowerLabel) = "{language}" || lang(?narrowerLabel) = "") }} 
                    }}
                OPTIONAL {{ <{concept_uri}> skos:exactMatch ?exactMatch .
                    OPTIONAL {{ ?exactMatch skos:prefLabel ?exactMatchLabel .
                        FILTER(lang(?exactMatchLabel) = "{language}" || lang(?exactMatchLabel) = "") }}
                    }}
                OPTIONAL {{ <{concept_uri}> skos:closeMatch ?closeMatch .
                    OPTIONAL {{ ?closeMatch skos:prefLabel ?closeMatchLabel .
                        FILTER(lang(?closeMatchLabel) = "{language}" || lang(?closeMatchLabel) = "") }}
                    }}
                OPTIONAL {{ <{concept_uri}> skos:broadMatch ?broadMatch .
                    OPTIONAL {{ ?broadMatch skos:prefLabel ?broadMatchLabel .
                        FILTER(lang(?broadMatchLabel) = "{language}" || lang(?broadMatchLabel) = "") }}
                    }}
                OPTIONAL {{ <{concept_uri}> skos:narrowMatch ?narrowMatch .
                    OPTIONAL {{ ?narrowMatch skos:prefLabel ?narrowMatchLabel .
                        FILTER(lang(?narrowMatchLabel) = "{language}" || lang(?narrowMatchLabel) = "") }}
                    }}
                OPTIONAL {{ <{concept_uri}> skos:relatedMatch ?relatedMatch .                
                    OPTIONAL {{ ?relatedMatch skos:prefLabel ?relatedMatchLabel .
                        FILTER(lang(?relatedMatchLabel) = "{language}" || lang(?relatedMatchLabel) = "") }}
                    }}
                OPTIONAL {{ <{concept_uri}> dct:created ?created }}
                OPTIONAL {{ <{concept_uri}> dct:modified ?modified }}
            }} }}""".format(concept_uri=self.request.values.get('uri'), 
                            language=self.language)
            
        result = Source.sparql_query(vocab.sparql_endpoint, q, vocab.sparql_username, vocab.sparql_password)
        
        assert result, 'Unable to query concepts for {}'.format(self.request.values.get('uri'))

        prefLabel = None
        definition = None
        lang_prefLabels = {}
        altLabels = []
        hiddenLabels = []
        source = None
        contributors = []
        
        concept_relationships = {        
            'broader': {},
            'narrower': {},
            'exactMatch': {},
            'closeMatch': {},
            'broadMatch': {},
            'narrowMatch': {},
            'relatedMatch': {},
            }
        
        for row in result:
            preflabel_lang = row['prefLabel'].get('xml:lang') or ''
            # Use default language or no language prefLabel as primary
            if ((not prefLabel and preflabel_lang == '') or 
                (preflabel_lang == self.language)
                ):
                prefLabel = row['prefLabel']['value']
                
            if preflabel_lang not in ['', self.language]:
                lang_prefLabels[preflabel_lang] = row['prefLabel']['value']

            if row.get('definition'):
                definition = row['definition']['value']
            else:
                definition = None

            if row.get('altLabel'):
                if row['altLabel']['value'] is not None and row['altLabel']['value'] not in altLabels:
                    altLabels.append(row['altLabel']['value'])

            if row.get('hiddenLabel'):
                if row['hiddenLabel']['value'] is not None and row['hiddenLabel']['value'] not in hiddenLabels:
                    hiddenLabels.append(row['hiddenLabel']['value'])

            if row.get('source'):
                source = row['source']['value']

            if row.get('contributor'):
                if row['contributor']['value'] is not None and row['contributor']['value'] not in contributors:
                    contributors.append(row['contributor']['value'])

            for relationship, related_concepts in concept_relationships.items():
                if row.get(relationship) and row[relationship].get('value'):
                    relatedConceptLabel = (row[relationship+'Label']['value'] if row.get(relationship+'Label') and row[relationship+'Label'].get('value') 
                                           else make_title(row[relationship]['value'])) # No prefLabel
                    related_concepts[row[relationship]['value']] = relatedConceptLabel

        lang_prefLabels = OrderedDict([(key, lang_prefLabels[key]) 
                                       for key in sorted(lang_prefLabels.keys())])
        altLabels.sort()
        hiddenLabels.sort()
        contributors.sort()
        
        for relationship, related_concepts in concept_relationships.items():
            concept_relationships[relationship] = OrderedDict([(key, related_concepts[key]) 
                                                               for key in sorted(related_concepts.keys())])
            
        return Concept(
            vocab_id=self.vocab_id,
            uri=vocab.uri,
            prefLabel=prefLabel,
            definition=definition,
            altLabels=altLabels,
            hiddenLabels=hiddenLabels,
            source=source,
            contributors=contributors,
            concept_relationships=concept_relationships,
            semantic_properties=None,
            created=None,
            modified=None,
            lang_prefLabels=lang_prefLabels
        )
Example #16
0
    def get_concept(self):
        concept_uri = self.request.values.get("uri")
        vocab = g.VOCABS[self.vocab_id]
        q = """PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX dct: <http://purl.org/dc/terms/>

select *

WHERE {{
    {{ GRAPH ?graph {{
        <{concept_uri}> ?predicate ?object .
        optional {{GRAPH ?predicateGraph {{?predicate rdfs:label ?predicateLabel .}} 
            FILTER(lang(?predicateLabel) = "{language}" || lang(?predicateLabel) = "")
            }}
        optional {{?object skos:prefLabel | rdfs:label ?objectLabel .
            FILTER(?prefLabel = skos:prefLabel || lang(?objectLabel) = "{language}" || lang(?objectLabel) = "") # Don't filter prefLabel language
        }}
    }} }}
    UNION
    {{
        <{concept_uri}> ?predicate ?object .
        optional {{GRAPH ?predicateGraph {{?predicate rdfs:label ?predicateLabel .}} 
            FILTER(lang(?predicateLabel) = "{language}" || lang(?predicateLabel) = "")
            }}
        optional {{?object skos:prefLabel | rdfs:label ?objectLabel .
            FILTER(?prefLabel = skos:prefLabel || lang(?objectLabel) = "{language}" || lang(?objectLabel) = "") # Don't filter prefLabel language
        }}
    }}
}}""".format(concept_uri=concept_uri, language=self.language)
        result = Source.sparql_query(vocab.sparql_endpoint, q,
                                     vocab.sparql_username,
                                     vocab.sparql_password)

        assert result, "Unable to query concepts for {}".format(
            self.request.values.get("uri"))

        prefLabel = None

        related_objects = {}

        for row in result:
            predicateUri = row["predicate"]["value"]

            # Special case for prefLabels
            if predicateUri == "http://www.w3.org/2004/02/skos/core#prefLabel":
                predicateLabel = "Multilingual Labels"
                preflabel_lang = row["object"].get("xml:lang")

                # Use default language or no language prefLabel as primary
                if (not prefLabel
                        and not preflabel_lang) or (preflabel_lang
                                                    == self.language):
                    prefLabel = row["object"][
                        "value"]  # Set current language prefLabel

                # Omit current language string from list (remove this if we want to show all)
                if preflabel_lang in ["", self.language]:
                    continue

                # Apend language code to prefLabel literal
                related_object = "{} ({})".format(row["object"]["value"],
                                                  preflabel_lang)
                related_objectLabel = None
            else:
                predicateLabel = (row["predicateLabel"]["value"]
                                  if row.get("predicateLabel")
                                  and row["predicateLabel"].get("value") else
                                  make_title(row["predicate"]["value"]))

                if row["object"]["type"] == "literal":
                    related_object = row["object"]["value"]
                    related_objectLabel = None
                elif row["object"]["type"] == "uri":
                    related_object = row["object"]["value"]
                    related_objectLabel = (
                        row["objectLabel"]["value"] if row.get("objectLabel")
                        and row["objectLabel"].get("value") else make_title(
                            row["object"]["value"]))

            relationship_dict = related_objects.get(predicateUri)
            if relationship_dict is None:
                relationship_dict = {"label": predicateLabel, "objects": {}}
                related_objects[predicateUri] = relationship_dict

            relationship_dict["objects"][related_object] = related_objectLabel

        related_objects = OrderedDict([(
            predicate,
            {
                "label":
                related_objects[predicate]["label"],
                "objects":
                OrderedDict([(key, related_objects[predicate]["objects"][key])
                             for key in sorted(related_objects[predicate]
                                               ["objects"].keys())]),
            },
        ) for predicate in sorted(related_objects.keys())])

        return Concept(
            vocab_id=self.vocab_id,
            uri=concept_uri,
            prefLabel=prefLabel,
            related_objects=related_objects,
            semantic_properties=None,
            source=self,
        )
Example #17
0
from model.concept import Concept
import unittest
from test import test_utils
from model import db_builder
from model import stemmers
from model.stemmers import StopWordsStemmer
from model.semantic_interpreter import SemanticComparer


#those concepts have distinct set of words
distinct_concepts = [
      
    Concept(1, "Technology", 
    [
         'computer', 'technology', 'system',
         'service',  'site',       'phone', 
         'internet', 'machine' 
    ]),
           
    Concept(2, "Business",
    [
        'store', 'product', 'sales', 'sell',
        'business', 'advertising','market', 
        'consumer'
    ]),
    
    Concept(3, "Entertainment",
    [            
        'play', 'film', 'production', 
        'movie', 'theater', 'stage'
        'star', 'director',