add sparql request for languages and countries labels
authorcavaliet
Fri, 30 Aug 2013 13:08:29 +0200
changeset 10 c4e7d66b7dc2
parent 9 3bc55f57b2b1
child 11 a88010423961
add sparql request for languages and countries labels
src/p4l/management/commands/get_country_label.py
src/p4l/management/commands/get_language_label.py
src/p4l/migrations/0001_initial.py
src/p4l/models/data.py
src/p4l/templates/p4l/p4l_record_view.html
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/p4l/management/commands/get_country_label.py	Fri Aug 30 13:08:29 2013 +0200
@@ -0,0 +1,69 @@
+# -*- coding: utf-8 -*-
+
+from django.conf import settings
+from django.core.management import BaseCommand
+from p4l.models import Country
+from p4l.utils import show_progress
+import logging
+import requests
+
+logger = logging.getLogger(__name__)
+
+class Command(BaseCommand):
+    
+    query = """
+PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
+PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+PREFIX owl:<http://www.w3.org/2002/07/owl#>
+PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
+SELECT ?uri ?label 
+WHERE {
+    ?uri skos:inScheme <http://skos.um.es/unescothes/CS000/Countries> .
+    ?uri skos:prefLabel|skos:label ?label .
+    FILTER (?uri = $root)
+}
+"""
+    
+    def fill_label(self):
+        # Loads Countries label from sparkl query
+        countries = Country.objects.filter(label=None)
+        total_countries = len(countries)
+        writer = None
+        i = 0
+        found = 0
+        for c in countries:
+            i += 1
+            res = requests.get(
+                settings.SPARQL_QUERY_ENDPOINT,
+                params={'query':self.query, 'timeout':10, '$root' : "<"+c.country+">"},
+                headers={'accept':'application/sparql-results+json'},
+            )
+            if not res.ok:
+                continue
+            elif res.text:
+                json_res = res.json()
+                if 'results' in json_res and 'bindings' in json_res['results'] and len(json_res['results']['bindings'])>0:
+                    # json_res['results']['bindings'] has several languages. If we find french, we save the french label.
+                    # If not, we save the first one.
+                    tmp_dict = {}
+                    first_label = None
+                    # We create a temporary dict with the lang code and the label
+                    for b in json_res['results']['bindings']:
+                        if 'label' in b and 'value' in b['label'] and 'xml:lang' in b['label']:
+                            tmp_dict[b['label']['xml:lang']] = b['label']['value']
+                            if not first_label:
+                                first_label = b['label']['value']
+                    if 'fr' in tmp_dict or first_label:
+                        if 'fr' in tmp_dict:
+                            c.label = tmp_dict['fr']
+                        else:
+                            c.label = first_label
+                        l = c.label
+                        c.save()
+                        found += 1
+                        writer = show_progress(i, total_countries, l, 50, writer=writer)
+        print("Processing Subjects Sparql Done. %d found on %d" % (found, total_countries))
+
+    def handle(self, *args, **options):
+        self.fill_label()
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/p4l/management/commands/get_language_label.py	Fri Aug 30 13:08:29 2013 +0200
@@ -0,0 +1,73 @@
+# -*- coding: utf-8 -*-
+
+from django.conf import settings
+from django.core.management import BaseCommand
+from p4l.models import Language
+from p4l.utils import show_progress
+import logging
+import requests
+
+logger = logging.getLogger(__name__)
+
+class Command(BaseCommand):
+    
+    query = """
+PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
+PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+PREFIX owl:<http://www.w3.org/2002/07/owl#>
+PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
+SELECT ?uri ?label 
+WHERE {
+    ?uri skos:inScheme <http://www.iiep.unesco.org/plan4learning/scheme/Languages> .
+    ?uri skos:prefLabel|skos:label ?label .
+    FILTER (?uri = $root)
+}
+"""
+    
+    def fill_label(self):
+        # Loads Subjects label from sparkl query
+        langs = Language.objects.filter(label=None)
+        total_langs = len(langs)
+        writer = None
+        i = 0
+        found = 0
+        for l in langs:
+            i += 1
+            logger.debug("1")
+            logger.debug(l)
+            logger.debug("2")
+            logger.debug(l.language)
+            res = requests.get(
+                settings.SPARQL_QUERY_ENDPOINT,
+                params={'query':self.query, 'timeout':10, '$root' : "<"+l.language+">"},
+                headers={'accept':'application/sparql-results+json'},
+            )
+            if not res.ok:
+                continue
+            elif res.text:
+                json_res = res.json()
+                if 'results' in json_res and 'bindings' in json_res['results'] and len(json_res['results']['bindings'])>0:
+                    # json_res['results']['bindings'] has several languages. If we find french, we save the french label.
+                    # If not, we save the first one.
+                    tmp_dict = {}
+                    first_label = None
+                    # We create a temporary dict with the lang code and the label
+                    for b in json_res['results']['bindings']:
+                        if 'label' in b and 'value' in b['label'] and 'xml:lang' in b['label']:
+                            tmp_dict[b['label']['xml:lang']] = b['label']['value']
+                            if not first_label:
+                                first_label = b['label']['value']
+                    if 'fr' in tmp_dict or first_label:
+                        if 'fr' in tmp_dict:
+                            l.label = tmp_dict['fr']
+                        else:
+                            l.label = first_label
+                        lab = l.label
+                        l.save()
+                        found += 1
+                        writer = show_progress(i, total_langs, lab, 50, writer=writer)
+        print("Processing Subjects Sparql Done. %d found on %d" % (found, total_langs))
+
+    def handle(self, *args, **options):
+        self.fill_label()
+
--- a/src/p4l/migrations/0001_initial.py	Fri Aug 30 12:40:01 2013 +0200
+++ b/src/p4l/migrations/0001_initial.py	Fri Aug 30 13:08:29 2013 +0200
@@ -94,6 +94,7 @@
         db.create_table(u'p4l_country', (
             (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
             ('country', self.gf('django.db.models.fields.URLField')(unique=True, max_length=2048, db_index=True)),
+            ('label', self.gf('django.db.models.fields.CharField')(db_index=True, max_length=2048, null=True, blank=True)),
         ))
         db.send_create_signal('p4l', ['Country'])
 
@@ -128,6 +129,7 @@
         db.create_table(u'p4l_language', (
             (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
             ('language', self.gf('django.db.models.fields.URLField')(unique=True, max_length=2048, db_index=True)),
+            ('label', self.gf('django.db.models.fields.CharField')(db_index=True, max_length=2048, null=True, blank=True)),
         ))
         db.send_create_signal('p4l', ['Language'])
 
--- a/src/p4l/models/data.py	Fri Aug 30 12:40:01 2013 +0200
+++ b/src/p4l/models/data.py	Fri Aug 30 13:08:29 2013 +0200
@@ -57,6 +57,7 @@
 
 class Country(P4lModel):
     country = models.URLField(max_length=2048, unique=True, db_index=True)
+    label = models.CharField(max_length=2048, blank=True, null=True, db_index=True) # filled from sparql request after import
 
 
 class Isbn(P4lModelLang):
@@ -74,6 +75,7 @@
 
 class Language(P4lModel):
     language = models.URLField(max_length=2048, unique=True, db_index=True)
+    label = models.CharField(max_length=2048, blank=True, null=True, db_index=True) # filled from sparql request after import
 
 
 
--- a/src/p4l/templates/p4l/p4l_record_view.html	Fri Aug 30 12:40:01 2013 +0200
+++ b/src/p4l/templates/p4l/p4l_record_view.html	Fri Aug 30 13:08:29 2013 +0200
@@ -25,7 +25,7 @@
     </tr>
     <tr>
       <td>{% trans 'countries' %}</td>
-      <td><ul>{% for i in record.countries.all %}<li>{{ i.country|default:'' }}</li>{% endfor %}</ul></td>
+      <td><ul>{% for i in record.countries.all %}<li>{{ i.label|default:'' }} <small class="text-muted">({{ i.country|default:'' }})</small></li>{% endfor %}</ul></td>
     </tr>
     <tr>
       <td>{% trans 'identifier' %}</td>
@@ -49,7 +49,7 @@
     </tr>
     <tr>
       <td>{% trans 'language' %}</td>
-      <td>{{ record.language.language|default:'' }}</td>
+      <td>{{ record.language.label|default:'' }} <small class="text-muted">({{ record.language.language|default:'' }})</small></td>
     </tr>
     <tr>
       <td>{% trans 'other languages' %}</td>