| author | ymh <ymh.work@gmail.com> |
| Sun, 16 Nov 2014 18:09:42 +0100 | |
| changeset 359 | 46ad324f6fe4 |
| parent 284 | f01235a1dcc2 |
| child 360 | 6129e7390abe |
| permissions | -rw-r--r-- |
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1 |
# -*- coding: utf-8 -*- |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2 |
''' |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
3 |
Created on Jan 30, 2012 |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
4 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
5 |
@author: ymh |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
6 |
''' |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
7 |
|
|
359
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
8 |
from hdabo.models import Tag |
|
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
9 |
from hdabo.utils import show_progress |
|
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
10 |
from hdalab.models import DbpediaFields, TagLinks |
|
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
11 |
from hdalab.models.dataviz import DbpediaFieldsTranslation |
|
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
12 |
import logging |
|
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
13 |
from optparse import make_option |
|
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
14 |
import sys |
|
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
15 |
import traceback |
|
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
16 |
|
|
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
17 |
from SPARQLWrapper import SPARQLWrapper, N3 |
|
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
18 |
from django import db |
|
281
bc0f26b1acc2
Hdalab : commands now work after update. Requests update with a dbpedia url from settings.
cavaliet
parents:
279
diff
changeset
|
19 |
from django.conf import settings |
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
20 |
from django.core.management.base import NoArgsCommand |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
21 |
from django.core.management.color import no_style |
|
359
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
22 |
from django.db import transaction |
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
23 |
from django.db.models import Count |
|
359
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
24 |
from rdflib import URIRef, Graph |
|
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
25 |
|
|
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
26 |
|
|
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
27 |
logger = logging.getLogger(__name__) |
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
28 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
29 |
class Command(NoArgsCommand): |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
30 |
''' |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
31 |
query and update wikipedia for tag title. |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
32 |
''' |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
33 |
options = '' |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
34 |
help = """query and update wikipedia for tag title.""" |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
35 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
36 |
option_list = NoArgsCommand.option_list + ( |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
37 |
make_option('--all', |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
38 |
action='store_true', |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
39 |
dest='all', |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
40 |
default=False, |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
41 |
help='force all tags to be updated, not only those not yet processed'), |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
42 |
make_option('--force', |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
43 |
action='store_true', |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
44 |
dest='force', |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
45 |
default=False, |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
46 |
help='ask no questions'), |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
47 |
make_option('--random', |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
48 |
action='store_true', |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
49 |
dest='random', |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
50 |
default=False, |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
51 |
help='randomize query on tags'), |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
52 |
make_option('--limit', |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
53 |
action='store', |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
54 |
type='int', |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
55 |
dest='limit', |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
56 |
default= -1, |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
57 |
help='number of tag to process'), |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
58 |
make_option('--start', |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
59 |
action='store', |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
60 |
type='int', |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
61 |
dest='start', |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
62 |
default=0, |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
63 |
help='number of tag to ignore'), |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
64 |
make_option('--tag', |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
65 |
action='append', |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
66 |
dest='tags', |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
67 |
type='string', |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
68 |
default=[], |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
69 |
help='the tag to query'), |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
70 |
) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
71 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
72 |
def handle_noargs(self, **options): |
| 135 | 73 |
|
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
74 |
self.style = no_style() |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
75 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
76 |
self.interactive = options.get('interactive', True) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
77 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
78 |
self.verbosity = int(options.get('verbosity', '1')) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
79 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
80 |
self.force = options.get('force', False) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
81 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
82 |
self.limit = options.get("limit", -1) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
83 |
self.start = options.get("start", 0) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
84 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
85 |
self.random = options.get('random', False) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
86 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
87 |
if self.verbosity > 2: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
88 |
print "option passed : " + repr(options) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
89 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
90 |
self.tag_list = options.get("tags", []); |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
91 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
92 |
queryset = Tag.objects.exclude(dbpedia_uri= None) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
93 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
94 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
95 |
if self.tag_list: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
96 |
queryset = queryset.filter(label__in=self.tag_list) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
97 |
elif not options.get('all',False): |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
98 |
queryset = queryset.annotate(dbfc=Count('dbpedia_fields')).filter(dbfc = 0) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
99 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
100 |
if self.random: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
101 |
queryset = queryset.order_by("?") |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
102 |
else: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
103 |
queryset = queryset.order_by("label") |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
104 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
105 |
if self.limit >= 0: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
106 |
queryset = queryset[self.start:self.limit] |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
107 |
elif self.start > 0: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
108 |
queryset = queryset[self.start:] |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
109 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
110 |
if self.verbosity > 2 : |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
111 |
print "Tag Query is %s" % (queryset.query) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
112 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
113 |
count = queryset.count() |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
114 |
|
| 135 | 115 |
if count == 0: |
116 |
print "No tag to query : exit." |
|
117 |
return |
|
118 |
||
119 |
||
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
120 |
if not self.force and self.interactive: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
121 |
confirm = raw_input("You have requested to query and replace the dbpedia information for %d tags.\n Are you sure you want to do this? \nType 'yes' to continue, or 'no' to cancel: " % (count)) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
122 |
else: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
123 |
confirm = 'yes' |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
124 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
125 |
if confirm != "yes": |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
126 |
print "dbpedia query cancelled" |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
127 |
return |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
128 |
|
|
281
bc0f26b1acc2
Hdalab : commands now work after update. Requests update with a dbpedia url from settings.
cavaliet
parents:
279
diff
changeset
|
129 |
#endpoint = SPARQLWrapper("http://dbpedia.org/sparql", returnFormat=RDF) |
|
359
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
130 |
endpoint = SPARQLWrapper(settings.DBPEDIA_URI_TEMPLATE % ( 'sparql', '' ), returnFormat=N3) |
|
240
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
131 |
|
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
132 |
writer = None |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
133 |
for i,tag in enumerate(queryset): |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
134 |
writer = show_progress(i+1, count, tag.label, 50, writer) |
| 135 | 135 |
db.reset_queries() |
|
240
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
136 |
|
|
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
137 |
#abstract query |
|
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
138 |
#"select ?y |
|
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
139 |
# where {<%s> <http://dbpedia.org/ontology/abstract> ?y}" % (tag.dbpedia_uri) |
|
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
140 |
|
|
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
141 |
#rdf_uri = re.sub('\/resource\/', "/data/", tag.dbpedia_uri) + ".n3" |
|
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
142 |
#g = Graph() |
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
143 |
try : |
|
240
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
144 |
abstracts = {} |
|
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
145 |
labels = {} |
|
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
146 |
thumbnail = None |
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
147 |
with transaction.commit_on_success(): |
| 284 | 148 |
endpoint.setQuery("select distinct ?y where {<%s> <http://dbpedia.org/ontology/abstract> ?y}" % (tag.dbpedia_uri)) |
|
359
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
149 |
res_abstracts = Graph().parse(data=endpoint.queryAndConvert(), format='n3') |
|
240
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
150 |
for _,_,o in res_abstracts.triples((None, URIRef('http://www.w3.org/2005/sparql-results#value'), None)): |
|
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
151 |
abstracts[o.language] = (unicode(o), True) |
|
359
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
152 |
logger.debug("Abstracts: %r" % abstracts) |
|
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
153 |
|
|
240
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
154 |
endpoint.setQuery("select distinct ?y where {<%s> <http://www.w3.org/2000/01/rdf-schema#label> ?y}" % (tag.dbpedia_uri)) |
|
359
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
155 |
#res_labels = endpoint.queryAndConvert() |
|
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
156 |
res_labels = Graph().parse(data=endpoint.queryAndConvert(), format='n3') |
|
240
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
157 |
for _,_,o in res_labels.triples((None, URIRef('http://www.w3.org/2005/sparql-results#value'), None)): |
|
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
158 |
labels[o.language] = (unicode(o), True) |
|
359
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
159 |
logger.debug("Labels: %r" % labels) |
|
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
160 |
|
| 284 | 161 |
endpoint.setQuery("select distinct ?y where {<%s> <http://dbpedia.org/ontology/thumbnail> ?y} limit 1" % (tag.dbpedia_uri)) |
|
359
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
162 |
res_thumbnails = Graph().parse(data=endpoint.queryAndConvert(), format='n3') |
|
240
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
163 |
for _,_,o in res_thumbnails.triples((None, URIRef('http://www.w3.org/2005/sparql-results#value'), None)): |
|
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
164 |
thumbnail = unicode(o) |
|
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
165 |
|
|
281
bc0f26b1acc2
Hdalab : commands now work after update. Requests update with a dbpedia url from settings.
cavaliet
parents:
279
diff
changeset
|
166 |
#endpoint.setQuery('select distinct ?y where { <%s> ?p ?y . FILTER regex(?y, "^http://dbpedia.org/resource")}' % (tag.dbpedia_uri)) |
|
bc0f26b1acc2
Hdalab : commands now work after update. Requests update with a dbpedia url from settings.
cavaliet
parents:
279
diff
changeset
|
167 |
endpoint.setQuery('select distinct ?y where { <%s> ?p ?y . FILTER regex(?y, "^%s")}' % (tag.dbpedia_uri, settings.DBPEDIA_URI_TEMPLATE % ( 'resource', '' ))) |
|
359
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
168 |
res_links = Graph().parse(data=endpoint.queryAndConvert(), format='n3') |
|
240
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
169 |
for _,_,o in res_links.triples((None, URIRef('http://www.w3.org/2005/sparql-results#value'), None)): |
|
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
170 |
tagqs = Tag.objects.filter(dbpedia_uri=unicode(o)) |
|
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
171 |
if tagqs: |
|
359
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
172 |
TagLinks.objects.get_or_create(subject=tag, object=tagqs[0]) |
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
173 |
|
|
240
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
174 |
ref_label_lang, (ref_label, _) = ('fr',labels['fr']) if 'fr' in labels else ('en',labels['en']) if 'en' in labels else labels.items()[0] if len(labels) > 0 else ('fr',(tag.label, True)) |
|
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
175 |
ref_abstract_lang, (ref_abstract, _) = ('fr',abstracts['fr']) if 'fr' in abstracts else ('en',abstracts['en']) if 'en' in abstracts else abstracts.items()[0] if len(abstracts) > 0 else ('fr',(None, 'True')) |
|
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
176 |
|
| 135 | 177 |
for lang in settings.LANGUAGES: |
178 |
if lang[0] not in labels: |
|
|
240
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
179 |
labels[lang[0]]= (ref_label, False) |
| 135 | 180 |
if lang[0] not in abstracts: |
|
240
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
181 |
abstracts[lang[0]] = (ref_abstract, False) |
| 135 | 182 |
|
183 |
dbfield , created = DbpediaFields.objects.get_or_create(dbpedia_uri=tag.dbpedia_uri,tag=tag, defaults={'abstract':ref_abstract, 'thumbnail':thumbnail, 'label':ref_label}) #@UndefinedVariable |
|
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
184 |
if not created: |
| 135 | 185 |
dbfield.abstract = ref_abstract |
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
186 |
dbfield.thumbnail = thumbnail |
| 135 | 187 |
dbfield.label = ref_label |
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
188 |
dbfield.save() |
| 135 | 189 |
DbpediaFieldsTranslation.objects.filter(master=dbfield).delete() |
190 |
||
191 |
consolidated_trans = {} |
|
192 |
for lang,label in labels.iteritems(): |
|
|
240
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
193 |
consolidated_trans[lang] = [label,(ref_abstract, lang==ref_abstract_lang)] |
| 135 | 194 |
for lang,abstract in abstracts.iteritems(): |
195 |
if lang in consolidated_trans: |
|
196 |
consolidated_trans[lang][1] = abstract |
|
197 |
else: |
|
|
240
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
198 |
consolidated_trans[lang] = [(ref_label, lang==ref_label_lang), abstract] |
| 135 | 199 |
|
200 |
for lang, trans in consolidated_trans.iteritems(): |
|
201 |
label, abstract = tuple(trans) |
|
|
240
c8627191f2d7
add indication that the labels has been translated, and use sparql requests instead of full n3 download
ymh <ymh.work@gmail.com>
parents:
135
diff
changeset
|
202 |
DbpediaFieldsTranslation.objects.create(master=dbfield, language_code=lang, label=label[0], is_label_translated=label[1], abstract=abstract[0], is_abstract_translated=abstract[1]) |
| 135 | 203 |
|
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
204 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
205 |
except Exception as e: |
|
279
177b508612f4
add, configure and correct hdalab to installed apps
cavaliet
parents:
272
diff
changeset
|
206 |
if tag.dbpedia_uri: |
|
177b508612f4
add, configure and correct hdalab to installed apps
cavaliet
parents:
272
diff
changeset
|
207 |
print "\nError processing resource %s : %s" %(tag.dbpedia_uri,unicode(e)) |
|
177b508612f4
add, configure and correct hdalab to installed apps
cavaliet
parents:
272
diff
changeset
|
208 |
else: |
|
177b508612f4
add, configure and correct hdalab to installed apps
cavaliet
parents:
272
diff
changeset
|
209 |
print "\nError processing resource %s" % unicode(e) |
| 135 | 210 |
traceback.print_exception(type(e), e, sys.exc_info()[2]) |
211 |
||
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
212 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
213 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
214 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
215 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
216 |