本文整理汇总了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)
示例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