当前位置: 首页>>代码示例>>Python>>正文


Python ConjunctiveGraph.preferredLabel方法代码示例

本文整理汇总了Python中rdflib.ConjunctiveGraph.preferredLabel方法的典型用法代码示例。如果您正苦于以下问题:Python ConjunctiveGraph.preferredLabel方法的具体用法?Python ConjunctiveGraph.preferredLabel怎么用?Python ConjunctiveGraph.preferredLabel使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在rdflib.ConjunctiveGraph的用法示例。


在下文中一共展示了ConjunctiveGraph.preferredLabel方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestPrefLabel

# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import preferredLabel [as 别名]
class TestPrefLabel(unittest.TestCase):

    def setUp(self):
        self.g = ConjunctiveGraph()
        self.u = URIRef('http://example.com/foo')
        self.g.add([self.u, RDFS.label, Literal('foo')])
        self.g.add([self.u, RDFS.label, Literal('bar')])

    def test_default_label_sorting(self):
        res = sorted(self.g.preferredLabel(self.u))
        tgt = [(rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#label'),
                rdflib.term.Literal(u'bar')),
               (rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#label'),
                rdflib.term.Literal(u'foo'))]
        self.assertEqual(res, tgt)

    def test_default_preflabel_sorting(self):
        self.g.add([self.u, SKOS.prefLabel, Literal('bla')])
        res = self.g.preferredLabel(self.u)
        tgt = [(rdflib.term.URIRef('http://www.w3.org/2004/02/skos/core#prefLabel'),
                rdflib.term.Literal(u'bla'))]
        self.assertEqual(res, tgt)

    def test_preflabel_lang_sorting_no_lang_attr(self):
        self.g.add([self.u, SKOS.prefLabel, Literal('bla')])
        self.g.add([self.u, SKOS.prefLabel, Literal('blubb', lang='en')])
        res = sorted(self.g.preferredLabel(self.u))
        tgt = [(rdflib.term.URIRef('http://www.w3.org/2004/02/skos/core#prefLabel'),
                rdflib.term.Literal(u'bla')),
               (rdflib.term.URIRef('http://www.w3.org/2004/02/skos/core#prefLabel'),
                rdflib.term.Literal(u'blubb', lang='en'))]

        self.assertEqual(res, tgt)

    def test_preflabel_lang_sorting_empty_lang_attr(self):
        self.g.add([self.u, SKOS.prefLabel, Literal('bla')])
        self.g.add([self.u, SKOS.prefLabel, Literal('blubb', lang='en')])
        res = self.g.preferredLabel(self.u, lang='')
        tgt = [(rdflib.term.URIRef('http://www.w3.org/2004/02/skos/core#prefLabel'),
                rdflib.term.Literal(u'bla'))]
        self.assertEqual(res, tgt)

    def test_preflabel_lang_sorting_en_lang_attr(self):
        self.g.add([self.u, SKOS.prefLabel, Literal('blubb', lang='en')])
        res = self.g.preferredLabel(self.u, lang='en')
        tgt = [(rdflib.term.URIRef('http://www.w3.org/2004/02/skos/core#prefLabel'),
                rdflib.term.Literal(u'blubb', lang='en'))]
        self.assertEqual(res, tgt)
开发者ID:RDFLib,项目名称:rdflib,代码行数:50,代码来源:test_preflabel.py

示例2: handle

# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import preferredLabel [as 别名]
  def handle(self, *args, **options):
    es = settings.ELASTIC_SEARCH_URL
    db = os.path.join(settings.BASE_DIR, "db")
    print(es)

    graph = ConjunctiveGraph('Sleepycat')
    graph.open(db, create=False)
    graph.bind('skos', SKOS)

    EU = Namespace('http://eurovoc.europa.eu/schema#')
    UNBIST = Namespace('http://unontologies.s3-website-us-east-1.amazonaws.com/unbist#')

    querystring = "select ?uri where { ?uri rdf:type skos:Concept filter not exists { ?uri rdf:type unbist:PlaceName } . }"

    index = 1
    
    # make the index:
    thes_index = {
      "mappings": {
        "terms": {
          "properties": {
            "scope_notes": {
              "type": "string"
            },
            "uri": {
              "type": "string"
            },
            "alt_labels": {
              "type": "string"
            },
            "alt_labels_orig": {
              "type": "string",
              "index": "not_analyzed"
            },
            "labels": {
              "type": "string"
            },
            "labels_orig": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        }
      }
    }
    
    r = requests.put(es + 'thesaurus/', data=json.dumps(thes_index))
    

    for uri in graph.query(querystring):
      this_uri = uri[0]
      doc = { 
        "uri": this_uri
      }
      pref_labels = []
      labels_orig_lc = []
      print("Getting preferred labels")
      for label in graph.preferredLabel(URIRef(this_uri)):
        pref_labels.append(label[1])
        if label[1].language in ['en','fr','es']:
          labels_orig_lc.append(label[1].lower())
  
      doc.update({"labels": pref_labels})
      doc.update({"labels_orig": pref_labels + labels_orig_lc})
      
      alt_labels = []
      alt_labels_orig_lc = []
      print("Getting alternate labels")
      for label in graph.objects(URIRef(this_uri), SKOS.altLabel):
        alt_labels.append(label)
        if label.language in ['en','fr','es']:
          alt_labels_orig_lc.append(label.lower())

      doc.update({"alt_labels": alt_labels})
      doc.update({"alt_labels_orig": alt_labels + alt_labels_orig_lc})
        
      scope_notes = []
      print("Getting scope notes")
      for sn in graph.objects(URIRef(this_uri), SKOS.scopeNote):
        scope_notes.append(sn)
      
      doc.update({"scope_notes": scope_notes})
      
      payload = json.dumps(doc)
      
      r = requests.put(es + 'thesaurus/terms/' + str(index), data=payload)
      index += 1
开发者ID:dag-hammarskjold-library,项目名称:unlod,代码行数:89,代码来源:riverrun.py


注:本文中的rdflib.ConjunctiveGraph.preferredLabel方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。