forked from catalyst-fp7/ontology
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonto2po.py
56 lines (50 loc) · 1.87 KB
/
onto2po.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from builtins import str
import sys
import os
from os.path import exists, join
from itertools import chain
from collections import defaultdict
import simplejson as json
import rdflib
from rdflib.namespace import RDFS, DCTERMS
def extract_labels(graph, prop):
labelsBySubject = defaultdict(dict)
for s,p,o in c.triples((None, prop, None)):
labelsBySubject[s][o.language] = o
return labelsBySubject
LANGS = ['fr']
if __name__ == '__main__':
c = rdflib.ConjunctiveGraph()
for f in sys.argv[1:]:
c.parse(open(f), format='turtle')
labelsBySubject = extract_labels(c, RDFS.label)
descriptionsBySubject = extract_labels(c, DCTERMS.description)
for lang in LANGS:
if not exists(lang):
os.mkdir(lang)
dirname = join(lang, 'LC_MESSAGES')
if not exists(dirname):
os.mkdir(dirname)
with open(join(dirname, 'ontology.po'), 'w') as f:
for labels in chain(
iter(labelsBySubject.values()),
iter(descriptionsBySubject.values())):
if 'en' in labels and lang in labels:
f.write((u'msgid "%s"\nmsgstr"%s"\n\n' % (
labels['en'], labels[lang])).encode('utf-8'))
c2 = rdflib.Graph()
subjects = set(chain(iter(labelsBySubject.keys()),
iter(descriptionsBySubject.keys())))
for s in subjects:
label = labelsBySubject[s].get('en', None)
if label is not None:
c2.add((s, RDFS.label, label))
desc = descriptionsBySubject[s].get('en', None)
if desc is not None:
c2.add((s, DCTERMS.description, desc))
with open('labels.jsonld', 'w') as f:
f.write(c2.serialize(format="json-ld", context={
"@language": "en",
"label": str(RDFS.label),
"description": str(DCTERMS.description)
}))