示例#1
0
    def __init__(self, name):
        """
        Initialize a concept by a name, which can be
        an alias (it does not have to be the canonical name).
        This specified name will be saved as an Alias.

        A canonical name will be looked for; if one is found
        it will be used as the slug for this Concept.
        """
        self.aliases.append(Alias(name))

        # Try to get a canonical URI
        # and derive the slug from that.
        self.uri = knowledge.uri_for_name(name)
        if self.uri:
            self.slug = self.uri.split('/')[-1]
            k = knowledge.knowledge_for(uri=self.uri, fallback=True)
            self.commonness = knowledge.commonness_for_uri(self.uri)

            self.summary = k['summary']
            self.name = k['name']

            # Download the image.
            if k['image'] is not None:
                ext = splitext(k['image'])[-1].lower()
                self.image = storage.save_from_url(
                    k['image'], '{0}{1}'.format(hash(self.slug), ext))

        # If no URI was found,
        # generate our own slug.
        # Note: A problem here is that it assumes that
        # this particular name is the canonical one,
        # and that we don't collect any information for it.
        else:
            self.slug = slugify(name)
示例#2
0
    def __init__(self, name):
        """
        Initialize a concept by a name, which can be
        an alias (it does not have to be the canonical name).
        This specified name will be saved as an Alias.

        A canonical name will be looked for; if one is found
        it will be used as the slug for this Concept.
        """
        self.aliases.append(Alias(name))

        # Try to get a canonical URI
        # and derive the slug from that.
        self.uri = knowledge.uri_for_name(name)
        if self.uri:
            self.slug = self.uri.split('/')[-1]
            k = knowledge.knowledge_for(uri=self.uri, fallback=True)
            self.commonness = knowledge.commonness_for_uri(self.uri)

            self.summary = k['summary']
            self.name = k['name']

            # Download the image.
            if k['image'] is not None:
                ext = splitext(k['image'])[-1].lower()
                self.image = storage.save_from_url(k['image'], '{0}{1}'.format(hash(self.slug), ext))

        # If no URI was found,
        # generate our own slug.
        # Note: A problem here is that it assumes that
        # this particular name is the canonical one,
        # and that we don't collect any information for it.
        else:
            self.slug = slugify(name)
示例#3
0
 def test_commonness_for_uri(self):
     self.mock_resp.read.return_value = b'''
         {
             "head": {
                 "vars": [ "from" ]
             } ,
             "results": {
                 "bindings": [
                     {
                         "from": { "type": "uri" , "value": "http://dbpedia.org/resource/Taiping_Rebellion" }
                     } ,
                     {
                         "from": { "type": "uri" , "value": "http://dbpedia.org/resource/Taiping_Heavenly_Kingdom" }
                     }
                 ]
             }
         }
     '''
     commonness = knowledge.commonness_for_uri('http://dbpedia.org/resource/Zeng_Guofan')
     self.assertEqual(commonness, 2)
示例#4
0
 def test_commonness_for_uri(self):
     self.mock_resp.read.return_value = b'''
         {
             "head": {
                 "vars": [ "from" ]
             } ,
             "results": {
                 "bindings": [
                     {
                         "from": { "type": "uri" , "value": "http://dbpedia.org/resource/Taiping_Rebellion" }
                     } ,
                     {
                         "from": { "type": "uri" , "value": "http://dbpedia.org/resource/Taiping_Heavenly_Kingdom" }
                     }
                 ]
             }
         }
     '''
     commonness = knowledge.commonness_for_uri(
         'http://dbpedia.org/resource/Zeng_Guofan')
     self.assertEqual(commonness, 2)