| author | ymh <ymh.work@gmail.com> |
| Mon, 09 Jan 2012 03:19:43 +0100 | |
| changeset 108 | 4b73a767a6c0 |
| parent 84 | 84dc6383323d |
| child 111 | ceb381f5b0c7 |
| permissions | -rw-r--r-- |
| 24 | 1 |
# -*- coding: utf-8 -*- |
|
42
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
2 |
from django.conf import settings |
| 72 | 3 |
from django.utils.http import urlquote |
4 |
from haystack.constants import DJANGO_ID |
|
5 |
from haystack.query import SearchQuerySet |
|
| 66 | 6 |
from hdabo.models import Tag, TaggedSheet |
|
42
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
7 |
from wikitools import api, wiki |
| 72 | 8 |
|
| 47 | 9 |
|
10 |
def normalize_tag(tag): |
|
11 |
if len(tag) == 0: |
|
12 |
return tag |
|
13 |
tag = tag.strip() |
|
14 |
tag = tag.replace("_", " ") |
|
15 |
tag = " ".join(tag.split()) |
|
16 |
tag = tag[0].upper() + tag[1:] |
|
17 |
return tag |
|
18 |
||
| 66 | 19 |
def urlize_for_wikipedia(label): |
| 72 | 20 |
return urlquote(label.replace(" ", "_")) |
| 47 | 21 |
|
| 24 | 22 |
|
23 |
def __is_homonymie(page_dict): |
|
24 |
for cat in page_dict.get(u"categories", []): |
|
25 |
if u'Catégorie:Homonymie' in cat.get(u"title", u"") or u'Category:Disambiguation pages' in cat.get(u"title", u""): |
|
26 |
return True |
|
27 |
return False |
|
28 |
||
| 66 | 29 |
def query_wikipedia_title(site, label=None, pageid=None): |
| 24 | 30 |
|
| 66 | 31 |
params = {'action':'query', 'prop':'info|categories|langlinks', 'inprop':'url', 'lllimit':'500', 'cllimit':'500', 'rvprop':'ids'} |
32 |
||
33 |
if label: |
|
34 |
params['titles'] = label |
|
35 |
else: |
|
36 |
params['pageids'] = pageid |
|
| 24 | 37 |
wpquery = api.APIRequest(site, params) #@UndefinedVariable |
38 |
||
| 47 | 39 |
response = wpquery.query() |
40 |
original_response = response |
|
| 66 | 41 |
def return_null_result(): |
| 72 | 42 |
return { 'new_label': None, 'status': Tag.TAG_URL_STATUS_DICT["null_result"], 'wikipedia_url': None, 'pageid': None, 'dbpedia_uri': None, 'revision_id': None, 'response': response } |
| 66 | 43 |
|
| 24 | 44 |
|
45 |
query_dict = response['query'] |
|
46 |
# get page if multiple pages or none -> return Tag.null_result |
|
47 |
pages = query_dict.get("pages", {}) |
|
48 |
if len(pages) > 1 or len(pages) == 0: |
|
| 66 | 49 |
return return_null_result() |
| 24 | 50 |
|
51 |
page = pages.values()[0] |
|
52 |
||
53 |
if u"invalid" in page or u"missing" in page: |
|
| 66 | 54 |
return return_null_result() |
| 24 | 55 |
|
56 |
url = page.get(u'fullurl', None) |
|
57 |
pageid = page.get(u'pageid', None) |
|
58 |
new_label = page[u'title'] |
|
|
108
4b73a767a6c0
backport changes made on model for hdabo_sf
ymh <ymh.work@gmail.com>
parents:
84
diff
changeset
|
59 |
alternative_label = None |
|
4b73a767a6c0
backport changes made on model for hdabo_sf
ymh <ymh.work@gmail.com>
parents:
84
diff
changeset
|
60 |
alternative_url = None |
|
4b73a767a6c0
backport changes made on model for hdabo_sf
ymh <ymh.work@gmail.com>
parents:
84
diff
changeset
|
61 |
alternative_pageid = None |
| 24 | 62 |
|
63 |
if __is_homonymie(page): |
|
64 |
status = Tag.TAG_URL_STATUS_DICT["homonyme"] |
|
65 |
elif u"redirect" in page: |
|
66 |
status = Tag.TAG_URL_STATUS_DICT["redirection"] |
|
67 |
else: |
|
68 |
status = Tag.TAG_URL_STATUS_DICT["match"] |
|
| 47 | 69 |
|
70 |
if status == Tag.TAG_URL_STATUS_DICT["redirection"]: |
|
| 66 | 71 |
params['redirects'] = True |
| 47 | 72 |
wpquery = api.APIRequest(site, params) #@UndefinedVariable |
73 |
response = wpquery.query() |
|
74 |
query_dict = response['query'] |
|
75 |
pages = query_dict.get("pages", {}) |
|
76 |
#we know that we have at least one answer |
|
77 |
if len(pages) > 1 or len(pages) == 0: |
|
| 66 | 78 |
return return_null_result() |
| 47 | 79 |
page = pages.values()[0] |
|
108
4b73a767a6c0
backport changes made on model for hdabo_sf
ymh <ymh.work@gmail.com>
parents:
84
diff
changeset
|
80 |
alternative_label = page.get('title', None) |
|
4b73a767a6c0
backport changes made on model for hdabo_sf
ymh <ymh.work@gmail.com>
parents:
84
diff
changeset
|
81 |
alternative_url = page.get('fullurl', None) |
|
4b73a767a6c0
backport changes made on model for hdabo_sf
ymh <ymh.work@gmail.com>
parents:
84
diff
changeset
|
82 |
alternative_pageid = page.get('pageid',None) |
| 47 | 83 |
|
| 24 | 84 |
|
| 66 | 85 |
revision_id = page.get('lastrevid', None) |
| 47 | 86 |
|
87 |
#process language to extract the english label |
|
88 |
english_label = None |
|
89 |
||
90 |
if status == Tag.TAG_URL_STATUS_DICT['match'] or status == Tag.TAG_URL_STATUS_DICT['redirection']: |
|
91 |
lang_links = page.get('langlinks', []) |
|
92 |
for lang_info_dict in lang_links: |
|
93 |
if lang_info_dict['lang'] == "en": |
|
94 |
english_label = lang_info_dict["*"] |
|
95 |
break |
|
96 |
||
97 |
if english_label and "#" not in english_label: |
|
| 66 | 98 |
dbpedia_uri = settings.DBPEDIA_URI_TEMPLATE % (urlize_for_wikipedia(english_label)) |
| 47 | 99 |
else: |
100 |
dbpedia_uri = None |
|
| 66 | 101 |
|
| 47 | 102 |
|
|
108
4b73a767a6c0
backport changes made on model for hdabo_sf
ymh <ymh.work@gmail.com>
parents:
84
diff
changeset
|
103 |
return { 'new_label': new_label, 'alternative_label': alternative_label, 'status': status, 'wikipedia_url': url, 'pageid': pageid, 'alternative_wikipedia_url': alternative_url, 'alternative_pageid': alternative_pageid, 'dbpedia_uri': dbpedia_uri, 'revision_id': revision_id, 'response': original_response } |
| 47 | 104 |
|
|
42
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
105 |
|
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
106 |
|
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
107 |
def get_or_create_tag(tag_label): |
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
108 |
|
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
109 |
tag_label_normalized = normalize_tag(tag_label) |
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
110 |
# We get the wikipedia references for the tag_label |
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
111 |
# We get or create the tag object |
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
112 |
|
| 69 | 113 |
tag = None |
114 |
for t in Tag.objects.filter(label__iexact=tag_label_normalized): |
|
115 |
if tag is None or t.url_status != Tag.TAG_URL_STATUS_DICT['null_result']: |
|
116 |
tag = t |
|
117 |
if tag.url_status != Tag.TAG_URL_STATUS_DICT['null_result']: |
|
118 |
break |
|
119 |
||
120 |
if tag is None: |
|
121 |
tag = Tag(label=tag_label_normalized, original_label=tag_label) |
|
122 |
created = True |
|
123 |
else: |
|
124 |
created = False |
|
125 |
||
| 66 | 126 |
site = wiki.Wiki(settings.WIKIPEDIA_API_URL) #@UndefinedVariable |
127 |
||
|
42
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
128 |
if created: |
| 66 | 129 |
wp_res = query_wikipedia_title(site, label=tag_label_normalized) |
|
108
4b73a767a6c0
backport changes made on model for hdabo_sf
ymh <ymh.work@gmail.com>
parents:
84
diff
changeset
|
130 |
new_label = wp_res['new_label'] |
|
4b73a767a6c0
backport changes made on model for hdabo_sf
ymh <ymh.work@gmail.com>
parents:
84
diff
changeset
|
131 |
alternative_label= wp_res['alternative_label'] |
|
4b73a767a6c0
backport changes made on model for hdabo_sf
ymh <ymh.work@gmail.com>
parents:
84
diff
changeset
|
132 |
status = wp_res['status'] |
|
4b73a767a6c0
backport changes made on model for hdabo_sf
ymh <ymh.work@gmail.com>
parents:
84
diff
changeset
|
133 |
url = wp_res['wikipedia_url'] |
|
4b73a767a6c0
backport changes made on model for hdabo_sf
ymh <ymh.work@gmail.com>
parents:
84
diff
changeset
|
134 |
alternative_url = wp_res['alternative_wikipedia_url'] |
|
4b73a767a6c0
backport changes made on model for hdabo_sf
ymh <ymh.work@gmail.com>
parents:
84
diff
changeset
|
135 |
pageid = wp_res['pageid'] |
|
4b73a767a6c0
backport changes made on model for hdabo_sf
ymh <ymh.work@gmail.com>
parents:
84
diff
changeset
|
136 |
alternative_pageid = wp_res['alternative_pageid'] |
|
4b73a767a6c0
backport changes made on model for hdabo_sf
ymh <ymh.work@gmail.com>
parents:
84
diff
changeset
|
137 |
dbpedia_uri = wp_res["dbpedia_uri"] |
|
4b73a767a6c0
backport changes made on model for hdabo_sf
ymh <ymh.work@gmail.com>
parents:
84
diff
changeset
|
138 |
wikipedia_revision_id = wp_res['revision_id'] |
| 47 | 139 |
|
|
42
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
140 |
|
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
141 |
# We save the datas |
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
142 |
if new_label is not None: |
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
143 |
tag.label = new_label |
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
144 |
if status is not None: |
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
145 |
tag.url_status = status |
|
108
4b73a767a6c0
backport changes made on model for hdabo_sf
ymh <ymh.work@gmail.com>
parents:
84
diff
changeset
|
146 |
tag.alternative_label = alternative_label |
|
4b73a767a6c0
backport changes made on model for hdabo_sf
ymh <ymh.work@gmail.com>
parents:
84
diff
changeset
|
147 |
tag.alternative_wikipedia_url = alternative_url |
|
4b73a767a6c0
backport changes made on model for hdabo_sf
ymh <ymh.work@gmail.com>
parents:
84
diff
changeset
|
148 |
tag.alternative_wikipedia_pageid = alternative_pageid |
| 47 | 149 |
tag.wikipedia_url = url |
150 |
tag.wikipedia_pageid = pageid |
|
151 |
tag.dbpedia_uri = dbpedia_uri |
|
|
42
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
152 |
|
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
153 |
tag.save() |
|
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
154 |
|
| 66 | 155 |
elif tag.wikipedia_pageid: |
156 |
wp_res = query_wikipedia_title(site, pageid=tag.wikipedia_pageid) |
|
157 |
wikipedia_revision_id = wp_res['revision_id'] |
|
158 |
else: |
|
159 |
wikipedia_revision_id = None |
|
160 |
||
161 |
||
162 |
return tag, wikipedia_revision_id, created |
|
|
42
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
163 |
|
| 66 | 164 |
def process_tag(site, tag, verbosity=0): |
|
42
861a78f74a37
modify behavior for tag modification on the datasheet
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
165 |
|
| 66 | 166 |
wp_res = query_wikipedia_title(site, label=tag.label) |
167 |
new_label, status, url, pageid, response, dbpedia_uri, revision_id = wp_res['new_label'], wp_res['status'], wp_res['wikipedia_url'], wp_res['pageid'], wp_res['response'], wp_res["dbpedia_uri"], wp_res["revision_id"] |
|
| 47 | 168 |
|
169 |
if verbosity >= 2 : |
|
170 |
print "response from query to %s with parameters %s :" % (site.apibase, repr(new_label)) |
|
171 |
print repr(response) |
|
172 |
||
| 66 | 173 |
prev_wikipedia_pageid = tag.wikipedia_pageid |
174 |
||
| 47 | 175 |
if new_label is not None: |
176 |
tag.label = new_label |
|
177 |
if status is not None: |
|
178 |
tag.url_status = status |
|
179 |
tag.wikipedia_url = url |
|
180 |
tag.wikipedia_pageid = pageid |
|
181 |
tag.dbpedia_uri = dbpedia_uri |
|
182 |
||
183 |
tag.save() |
|
| 66 | 184 |
|
185 |
if prev_wikipedia_pageid != pageid: |
|
186 |
TaggedSheet.objects.filter(tag=tag).update(wikipedia_revision_id=revision_id) |
|
| 72 | 187 |
|
188 |
||
189 |
def reorder_datasheet_tags(ds): |
|
190 |
ts_list = [] |
|
191 |
for ts in ds.taggedsheet_set.all(): |
|
|
83
1c4729b3dac1
Correction bug #20. The solution is mainly to make sure that the index is recalculated
ymh <ymh.work@gmail.com>
parents:
72
diff
changeset
|
192 |
ts.index_note = 0 |
| 72 | 193 |
kwargs = {DJANGO_ID + "__exact": unicode(ds.pk)} |
|
83
1c4729b3dac1
Correction bug #20. The solution is mainly to make sure that the index is recalculated
ymh <ymh.work@gmail.com>
parents:
72
diff
changeset
|
194 |
|
| 72 | 195 |
results = SearchQuerySet().filter(title=ts.tag.label).filter_or(description=ts.tag.label).filter(**kwargs) |
196 |
if len(results) > 0: |
|
|
83
1c4729b3dac1
Correction bug #20. The solution is mainly to make sure that the index is recalculated
ymh <ymh.work@gmail.com>
parents:
72
diff
changeset
|
197 |
ts.index_note += results[0].score |
|
84
84dc6383323d
Correction bug #20. The solution is mainly to make sure that the index is recalculated - small optimization
ymh <ymh.work@gmail.com>
parents:
83
diff
changeset
|
198 |
ts.save() |
|
83
1c4729b3dac1
Correction bug #20. The solution is mainly to make sure that the index is recalculated
ymh <ymh.work@gmail.com>
parents:
72
diff
changeset
|
199 |
|
| 72 | 200 |
ts_list.append(ts) |
201 |
ts_list.sort(key=lambda t: (-t.index_note, t.order)) |
|
202 |
for k, ts in enumerate(ts_list): |
|
203 |
ts.order = k + 1 |
|
204 |
ts.save() |
|
205 |
if ds.manual_order: |
|
206 |
ds.manual_order = False |
|
207 |
ds.save() |
|
| 47 | 208 |
|
209 |