| author | cavaliet |
| Fri, 20 Sep 2013 11:27:44 +0200 | |
| changeset 109 | 9328fa268f45 |
| parent 102 | 53c9233a7684 |
| child 113 | c05567404888 |
| permissions | -rw-r--r-- |
| 0 | 1 |
# -*- coding: utf-8 -*- |
2 |
import codecs #@UnresolvedImport |
|
|
101
71532a54d1c4
update virtualenv + implement record serialization
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
3 |
import logging |
| 0 | 4 |
import math |
|
101
71532a54d1c4
update virtualenv + implement record serialization
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
5 |
import sys |
|
71532a54d1c4
update virtualenv + implement record serialization
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
6 |
|
|
71532a54d1c4
update virtualenv + implement record serialization
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
7 |
from django.conf import settings |
|
71532a54d1c4
update virtualenv + implement record serialization
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
8 |
from django.db.models.query import QuerySet |
|
71532a54d1c4
update virtualenv + implement record serialization
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
9 |
from django.db.models.query_utils import Q |
| 12 | 10 |
import requests #@UnresolvedImport |
| 109 | 11 |
from django.core.validators import URLValidator |
| 17 | 12 |
|
13 |
||
14 |
logger = logging.getLogger(__name__) |
|
| 0 | 15 |
|
16 |
def show_progress(current_line, total_line, label, width, writer=None): |
|
17 |
||
18 |
if writer is None: |
|
19 |
writer = sys.stdout |
|
20 |
if sys.stdout.encoding is not None: |
|
21 |
writer = codecs.getwriter(sys.stdout.encoding)(sys.stdout) |
|
22 |
||
23 |
percent = (float(current_line) / float(total_line)) * 100.0 |
|
24 |
||
25 |
marks = math.floor(width * (percent / 100.0)) #@UndefinedVariable |
|
26 |
spaces = math.floor(width - marks) #@UndefinedVariable |
|
27 |
||
28 |
loader = u'[' + (u'=' * int(marks)) + (u' ' * int(spaces)) + u']' |
|
29 |
||
30 |
s = u"%s %3d%% %*d/%d - %*s\r" % (loader, percent, len(str(total_line)), current_line, total_line, width, label[:width]) |
|
31 |
||
32 |
writer.write(s) #takes the header into account |
|
33 |
if percent >= 100: |
|
34 |
writer.write("\n") |
|
35 |
writer.flush() |
|
36 |
||
37 |
return writer |
|
38 |
||
39 |
LANGUAGE_NS = u"http://psi.oasis-open.org/iso/639/#" |
|
40 |
LANGUAGE_URI_MAP = {u'roh': u'rm', u'sco': u'sco', u'scn': u'scn', u'rom': u'rom', u'ron': u'ro', u'oss': u'os', u'ale': u'ale', u'mni': u'mni', u'nwc': u'nwc', u'osa': u'osa', u'mnc': u'mnc', u'mwr': u'mwr', u'ven': u'ven', u'uga': u'uga', u'mwl': u'mwl', u'fas': u'fa', u'fat': u'fat', u'fan': u'fan', u'fao': u'fo', u'din': u'din', u'hye': u'hy', u'dsb': u'dsb', u'srd': u'sc', u'iba': u'iba', u'div': u'div', u'tel': u'te', u'tem': u'tem', u'nbl': u'nr', u'ter': u'ter', u'tet': u'tet', u'sun': u'su', u'kut': u'kut', u'suk': u'suk', u'kur': u'ku', u'kum': u'kum', u'sus': u'sus', u'new': u'new', u'nep': u'ne', u'sux': u'sux', u'men': u'men', u'lez': u'lez', u'gla': u'gd', u'bos': u'bs', u'gle': u'ga', u'eka': u'eka', u'glg': u'gl', u'akk': u'akk', u'aka': u'aka', u'bod': u'bo', u'glv': u'gv', u'jrb': u'jrb', u'vie': u'vi', u'ipk': u'ik', u'uzb': u'uz', u'sga': u'sga', u'bre': u'br', u'bra': u'bra', u'aym': u'ay', u'cha': u'ch', u'chb': u'chb', u'che': u'ce', u'chg': u'chg', u'chk': u'chk', u'chm': u'chm', u'chn': u'chn', u'cho': u'cho', u'chp': u'chp', u'chr': u'chr', u'chu': u'cu', u'chv': u'cv', u'chy': u'chy', u'msa': u'ms', u'iii': u'ii', u'ndo': u'ng', u'ibo': u'ibo', u'car': u'car', u'xho': u'xh', u'deu': u'de', u'cat': u'ca', u'del': u'del', u'den': u'den', u'cad': u'cad', u'tat': u'tt', u'srn': u'srn', u'raj': u'raj', u'spa': u'es', u'tam': u'ta', u'tah': u'ty', u'afh': u'afh', u'eng': u'en', u'enm': u'enm', u'csb': u'csb', u'nyn': u'nyn', u'nyo': u'nyo', u'sid': u'sid', u'nya': u'ny', u'sin': u'si', u'afr': u'af', u'lam': u'lam', u'snd': u'sd', u'mar': u'mr', u'lah': u'lah', u'sna': u'sn', u'lad': u'lad', u'snk': u'snk', u'mad': u'mad', u'mag': u'mag', u'lat': u'la', u'mah': u'mh', u'mak': u'mak', u'mal': u'ml', u'man': u'man', u'egy': u'egy', u'znd': u'znd', u'zen': u'zen', u'kbd': u'kbd', u'ita': u'it', u'tsn': u'tn', u'tso': u'ts', u'tsi': u'tsi', u'byn': u'byn', u'fij': u'fj', u'fin': u'fi', u'eus': u'eu', u'non': u'non', u'ceb': u'ceb', u'dan': u'da', u'nym': u'nym', u'nob': u'nb', u'dak': u'dak', u'ces': u'cs', u'dar': u'dar', u'day': u'day', u'nor': u'no', u'kpe': u'kpe', u'guj': u'gu', u'mdf': u'mdf', u'mas': u'mas', u'lao': u'lo', u'mdr': u'mdr', u'gon': u'gon', u'goh': u'goh', u'sms': u'sms', u'smo': u'sm', u'smn': u'smn', u'smj': u'smj', u'got': u'got', u'sme': u'se', u'bla': u'bla', u'sma': u'sma', u'gor': u'gor', u'ast': u'ast', u'orm': u'om', u'que': u'qu', u'ori': u'or', u'bal': u'bal', u'asm': u'as', u'pus': u'ps', u'kik': u'ki', u'ltz': u'lb', u'wln': u'wa', u'isl': u'is', u'mai': u'mai', u'lav': u'lv', u'zap': u'zap', u'yid': u'yi', u'kok': u'kok', u'kom': u'kv', u'kon': u'kon', u'ukr': u'uk', u'ton': u'to', u'kos': u'kos', u'kor': u'ko', u'tog': u'tog', u'hun': u'hu', u'hup': u'hup', u'cym': u'cy', u'udm': u'udm', u'bej': u'bej', u'ben': u'bn', u'bel': u'be', u'bem': u'bem', u'aar': u'aa', u'nzi': u'nzi', u'sah': u'sah', u'san': u'sa', u'sam': u'sam', u'pro': u'pro', u'sag': u'sg', u'sad': u'sad', u'rar': u'rar', u'rap': u'rap', u'sas': u'sas', u'sat': u'sat', u'min': u'min', u'lim': u'li', u'lin': u'ln', u'lit': u'lt', u'efi': u'efi', u'btk': u'btk', u'kac': u'kac', u'kab': u'kab', u'kaa': u'kaa', u'kan': u'kn', u'kam': u'kam', u'kal': u'kl', u'kas': u'ks', u'kar': u'kar', u'kaw': u'kaw', u'kau': u'kau', u'kat': u'ka', u'kaz': u'kk', u'tyv': u'tyv', u'awa': u'awa', u'urd': u'ur', u'doi': u'doi', u'tpi': u'tpi', u'mri': u'mi', u'abk': u'ab', u'tkl': u'tkl', u'nld': u'nl', u'oji': u'oji', u'oci': u'oc', u'wol': u'wo', u'jav': u'jv', u'hrv': u'hr', u'mga': u'mga', u'hit': u'hit', u'gez': u'gez', u'ssw': u'ss', u'hil': u'hil', u'him': u'him', u'hin': u'hi', u'bas': u'bas', u'gba': u'gba', u'bad': u'bad', u'kua': u'kj', u'cre': u'cre', u'ban': u'ban', u'crh': u'crh', u'bam': u'bam', u'bak': u'ba', u'shn': u'shn', u'arp': u'arp', u'arw': u'arw', u'ara': u'ar', u'arc': u'arc', u'sel': u'sel', u'arn': u'arn', u'lus': u'lus', u'mus': u'mus', u'lua': u'lua', u'lub': u'lub', u'lug': u'lug', u'lui': u'lui', u'lun': u'lun', u'luo': u'luo', u'iku': u'iu', u'tur': u'tr', u'tuk': u'tk', u'tum': u'tum', u'mkd': u'mk', u'cop': u'cop', u'cos': u'co', u'ile': u'ie', u'ilo': u'ilo', u'gwi': u'gwi', u'und': u'und', u'tli': u'tli', u'tlh': u'tlh', u'por': u'pt', u'pon': u'pon', u'pol': u'pl', u'ang': u'ang', u'tgk': u'tg', u'tgl': u'tl', u'fra': u'fr', u'fre': u'fr', u'dum': u'dum', u'swa': u'sw', u'dua': u'dua', u'swe': u'sv', u'yap': u'yap', u'frm': u'frm', u'tiv': u'tiv', u'yao': u'yao', u'xal': u'xal', u'fry': u'fy', u'gay': u'gay', u'ota': u'ota', u'hmn': u'hmn', u'hmo': u'ho', u'an': u'arg', u'gaa': u'gaa', u'fur': u'fur', u'mlg': u'mg', u'slv': u'sl', u'fil': u'fil', u'mlt': u'mt', u'slk': u'sk', u'ful': u'ful', u'jpn': u'ja', u'vol': u'vo', u'vot': u'vot', u'ind': u'id', u'ave': u'ae', u'jpr': u'jpr', u'ava': u'ava', u'pap': u'pap', u'ewo': u'ewo', u'pau': u'pau', u'ewe': u'ewe', u'pag': u'pag', u'pal': u'pal', u'pam': u'pam', u'pan': u'pa', u'nog': u'nog', u'phn': u'phn', u'kir': u'ky', u'nia': u'nia', u'dgr': u'dgr', u'syr': u'syr', u'kin': u'rw', u'niu': u'niu', u'epo': u'eo', u'jbo': u'jbo', u'mic': u'mic', u'tha': u'th', u'hai': u'hai', u'gmh': u'gmh', u'ell': u'el', u'ady': u'ady', u'elx': u'elx', u'ada': u'ada', u'nav': u'nv', u'hat': u'ht', u'hau': u'ha', u'haw': u'haw', u'bin': u'bin', u'amh': u'am', u'bik': u'bik', u'bih': u'bh', u'mos': u'mos', u'moh': u'moh', u'mon': u'mn', u'bho': u'bho', u'mol': u'mo', u'bis': u'bi', u'tvl': u'tvl', u'ijo': u'ijo', u'est': u'et', u'kmb': u'kmb', u'peo': u'peo', u'umb': u'umb', u'tmh': u'tmh', u'fon': u'fon', u'hsb': u'hsb', u'run': u'rn', u'rus': u'ru', u'pli': u'pi', u'ace': u'ace', u'ach': u'ach', u'nde': u'nd', u'dzo': u'dz', u'kru': u'kru', u'srr': u'srr', u'ido': u'io', u'srp': u'sr', u'kro': u'kro', u'krc': u'krc', u'nds': u'nds', u'zun': u'zun', u'zul': u'zu', u'twi': u'tw', u'sog': u'sog', u'nso': u'nso', u'fro': u'fro', u'som': u'so', u'son': u'son', u'sot': u'st', u'vai': u'vai', u'her': u'hz', u'lol': u'lol', u'heb': u'he', u'loz': u'loz', u'gil': u'gil', u'was': u'was', u'war': u'war', u'bul': u'bg', u'wal': u'wal', u'bua': u'bua', u'bug': u'bug', u'aze': u'az', u'zha': u'za', u'zho': u'zh', u'nno': u'nn', u'uig': u'ug', u'myv': u'myv', u'inh': u'inh', u'khm': u'km', u'kho': u'kho', u'mya': u'my', u'kha': u'kha', u'ina': u'ia', u'nah': u'nah', u'tir': u'ti', u'nap': u'nap', u'grb': u'grb', u'grc': u'grc', u'nau': u'na', u'grn': u'gn', u'tig': u'tig', u'yor': u'yo', u'cor': u'kw', u'sqi': u'sq', u'dyu': u'dyu'} |
|
41 |
||
42 |
def get_code_from_language_uri(lang_uri): |
|
43 |
||
44 |
if not lang_uri: |
|
45 |
return None |
|
46 |
if lang_uri.startswith(LANGUAGE_NS): |
|
47 |
lang_uri = lang_uri[len(LANGUAGE_NS):] |
|
48 |
||
49 |
return LANGUAGE_URI_MAP.get(lang_uri, None) |
|
| 12 | 50 |
|
51 |
||
| 17 | 52 |
|
53 |
def get_labels_for_uris(uri_list, scheme_uri, lang, acronyms=False): |
|
54 |
query_without_acronym = """ |
|
55 |
PREFIX skos:<http://www.w3.org/2004/02/skos/core#> |
|
56 |
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
|
57 |
PREFIX owl:<http://www.w3.org/2002/07/owl#> |
|
58 |
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> |
|
59 |
SELECT ?uri ?label |
|
60 |
WHERE { |
|
61 |
?uri skos:inScheme <%s> . |
|
62 |
?uri skos:prefLabel|skos:label ?label . |
|
| 23 | 63 |
FILTER (%s) |
| 17 | 64 |
} |
65 |
""" |
|
66 |
query_with_acronym = """ |
|
67 |
PREFIX skos:<http://www.w3.org/2004/02/skos/core#> |
|
68 |
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
|
69 |
PREFIX owl:<http://www.w3.org/2002/07/owl#> |
|
70 |
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> |
|
71 |
SELECT ?uri ?label ?acro |
|
72 |
WHERE { |
|
73 |
?uri skos:inScheme <%s> . |
|
74 |
?uri skos:prefLabel|skos:label ?label . |
|
75 |
OPTIONAL { ?uri skos:altLabel ?acro } |
|
| 23 | 76 |
FILTER (%s) |
| 17 | 77 |
} |
78 |
""" |
|
79 |
if acronyms: |
|
80 |
query = query_with_acronym |
|
81 |
else: |
|
82 |
query = query_without_acronym |
|
83 |
res_dict = {} |
|
| 34 | 84 |
if not uri_list: |
85 |
return res_dict |
|
| 23 | 86 |
# We build the filter string |
87 |
filter_str = "" |
|
| 109 | 88 |
validate = URLValidator() |
| 23 | 89 |
for i,uri in enumerate(uri_list): |
| 17 | 90 |
res_dict[uri] = "" |
| 109 | 91 |
# We test if the uri is correct. If not, all the sparql request fails |
92 |
try: |
|
93 |
validate(uri) |
|
94 |
except: |
|
95 |
continue |
|
| 94 | 96 |
uri = uri.replace(" ", "") # avoid bug when only few urls are not good |
97 |
filter_str += (" || ?uri = <" + uri + ">") if i else ("?uri = <" + uri + ">") |
|
| 23 | 98 |
# We request the labels |
99 |
res = requests.get( |
|
100 |
settings.SPARQL_QUERY_ENDPOINT, |
|
101 |
params={'query':query % (scheme_uri, filter_str), 'timeout':10},#, '$root' : "<"+uri+">"}, |
|
102 |
headers={'accept':'application/sparql-results+json'}, |
|
103 |
) |
|
104 |
if res.ok and res.text: |
|
105 |
json_res = res.json() |
|
106 |
if 'results' in json_res and 'bindings' in json_res['results'] and len(json_res['results']['bindings'])>0: |
|
107 |
# json_res['results']['bindings'] has several languages. If we find french, we save the french label. |
|
108 |
# If not, we save the first one. |
|
109 |
tmp_dict = {} |
|
110 |
first_label = None |
|
111 |
# We create a temporary dict with the lang code and the label |
|
112 |
for b in json_res['results']['bindings']: |
|
113 |
if lang: |
|
114 |
if 'label' in b and 'value' in b['label'] and 'xml:lang' in b['label']: |
|
115 |
tmp_dict[b['label']['xml:lang']] = b['label']['value'] |
|
116 |
if not first_label: |
|
117 |
first_label = b['label']['value'] |
|
118 |
else: |
|
119 |
if 'acro' in b and 'value' in b['acro']: |
|
| 95 | 120 |
first_label = b['acro']['value'] + ". " + b['label']['value'] |
| 17 | 121 |
else: |
| 23 | 122 |
first_label = b['label']['value'] |
| 17 | 123 |
if lang in tmp_dict or first_label: |
124 |
if lang in tmp_dict: |
|
125 |
label = tmp_dict[lang] |
|
126 |
else: |
|
127 |
label = first_label |
|
| 23 | 128 |
res_dict[b['uri']['value']] = label |
| 17 | 129 |
return res_dict |
130 |
||
| 12 | 131 |
|
132 |
||
133 |
def fill_label_for_model(model, property_uri, scheme_uri): |
|
134 |
query = """ |
|
135 |
PREFIX skos:<http://www.w3.org/2004/02/skos/core#> |
|
136 |
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
|
137 |
PREFIX owl:<http://www.w3.org/2002/07/owl#> |
|
138 |
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> |
|
139 |
SELECT ?uri ?label |
|
140 |
WHERE { |
|
141 |
?uri skos:inScheme <%s> . |
|
142 |
?uri skos:prefLabel|skos:label ?label . |
|
143 |
FILTER (?uri = $root) |
|
144 |
} |
|
145 |
""" |
|
146 |
# Loads Models label from sparkl query |
|
147 |
objs = model.objects.filter(label=None) |
|
148 |
total_objs = len(objs) |
|
149 |
writer = None |
|
150 |
i = 0 |
|
151 |
found = 0 |
|
152 |
for o in objs: |
|
153 |
i += 1 |
|
154 |
res = requests.get( |
|
155 |
settings.SPARQL_QUERY_ENDPOINT, |
|
156 |
params={'query':query % scheme_uri, 'timeout':10, '$root' : "<"+getattr(o, property_uri)+">"}, |
|
157 |
headers={'accept':'application/sparql-results+json'}, |
|
158 |
) |
|
159 |
if not res.ok: |
|
160 |
continue |
|
161 |
elif res.text: |
|
162 |
json_res = res.json() |
|
163 |
if 'results' in json_res and 'bindings' in json_res['results'] and len(json_res['results']['bindings'])>0: |
|
164 |
# json_res['results']['bindings'] has several languages. If we find french, we save the french label. |
|
165 |
# If not, we save the first one. |
|
166 |
tmp_dict = {} |
|
167 |
first_label = None |
|
168 |
# We create a temporary dict with the lang code and the label |
|
169 |
for b in json_res['results']['bindings']: |
|
170 |
if 'label' in b and 'value' in b['label'] and 'xml:lang' in b['label']: |
|
171 |
tmp_dict[b['label']['xml:lang']] = b['label']['value'] |
|
172 |
if not first_label: |
|
173 |
first_label = b['label']['value'] |
|
174 |
if 'fr' in tmp_dict or first_label: |
|
175 |
if 'fr' in tmp_dict: |
|
176 |
o.label = tmp_dict['fr'] |
|
177 |
else: |
|
178 |
o.label = first_label |
|
179 |
l = o.label |
|
180 |
o.save() |
|
181 |
found += 1 |
|
182 |
writer = show_progress(i, total_objs, l, 50, writer=writer) |
|
|
101
71532a54d1c4
update virtualenv + implement record serialization
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
183 |
print("Processing Sparql Done. %d found on %d" % (found, total_objs)) |
|
71532a54d1c4
update virtualenv + implement record serialization
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
184 |