Browse Source

[fix] merge infoboxes based on weight

marc 8 years ago
parent
commit
ad58b14be7
4 changed files with 50 additions and 16 deletions
  1. 19
    5
      searx/engines/wikidata.py
  2. 17
    1
      searx/results.py
  3. 2
    0
      searx/settings.yml
  4. 12
    10
      tests/unit/engines/test_wikidata.py

+ 19
- 5
searx/engines/wikidata.py View File

@@ -35,7 +35,7 @@ url_detail = wikidata_api\
35 35
 
36 36
 url_map = 'https://www.openstreetmap.org/'\
37 37
     + '?lat={latitude}&lon={longitude}&zoom={zoom}&layers=M'
38
-url_image = 'https://commons.wikimedia.org/wiki/Special:FilePath/{filename}?width=500'
38
+url_image = 'https://commons.wikimedia.org/wiki/Special:FilePath/{filename}?width=500&height=400'
39 39
 
40 40
 # xpaths
41 41
 wikidata_ids_xpath = '//div/ul[@class="wikibase-disambiguation"]/li/a/@title'
@@ -162,6 +162,7 @@ def getDetail(jsonresponse, wikidata_id, language, locale):
162 162
 
163 163
     # INFOBOX ATTRIBUTES (ROWS)
164 164
 
165
+    # DATES
165 166
     # inception date
166 167
     add_attribute(attributes, result, 'P571', date=True)
167 168
     # dissolution date
@@ -170,11 +171,14 @@ def getDetail(jsonresponse, wikidata_id, language, locale):
170 171
     add_attribute(attributes, result, 'P580', date=True)
171 172
     # end date
172 173
     add_attribute(attributes, result, 'P582', date=True)
173
-
174 174
     # date of birth
175 175
     add_attribute(attributes, result, 'P569', date=True)
176 176
     # date of death
177 177
     add_attribute(attributes, result, 'P570', date=True)
178
+    # date of spacecraft launch
179
+    add_attribute(attributes, result, 'P619', date=True)
180
+    # date of spacecraft landing
181
+    add_attribute(attributes, result, 'P620', date=True)
178 182
 
179 183
     # nationality
180 184
     add_attribute(attributes, result, 'P27')
@@ -201,7 +205,7 @@ def getDetail(jsonresponse, wikidata_id, language, locale):
201 205
     # area
202 206
     add_attribute(attributes, result, 'P2046')
203 207
     # currency
204
-    add_attribute(attributes, result, 'P38')
208
+    add_attribute(attributes, result, 'P38', trim=True)
205 209
     # heigth (building)
206 210
     add_attribute(attributes, result, 'P2048')
207 211
 
@@ -230,6 +234,10 @@ def getDetail(jsonresponse, wikidata_id, language, locale):
230 234
     add_attribute(attributes, result, 'P264')
231 235
     # publisher
232 236
     add_attribute(attributes, result, 'P123')
237
+    # original network
238
+    add_attribute(attributes, result, 'P449')
239
+    # distributor
240
+    add_attribute(attributes, result, 'P750')
233 241
     # composer
234 242
     add_attribute(attributes, result, 'P86')
235 243
     # publication date
@@ -266,6 +274,10 @@ def getDetail(jsonresponse, wikidata_id, language, locale):
266 274
     add_attribute(attributes, result, 'P112')
267 275
     # legal form (company/organization)
268 276
     add_attribute(attributes, result, 'P1454')
277
+    # operator
278
+    add_attribute(attributes, result, 'P137')
279
+    # crew members (tripulation)
280
+    add_attribute(attributes, result, 'P1029')
269 281
     # taxon
270 282
     add_attribute(attributes, result, 'P225')
271 283
     # chemical formula
@@ -300,8 +312,8 @@ def getDetail(jsonresponse, wikidata_id, language, locale):
300 312
 
301 313
 # only returns first match
302 314
 def add_image(result):
303
-    # P18: image, P154: logo, P242: map, P41: flag, P2716: collage, P2910: icon
304
-    property_ids = ['P18', 'P154', 'P242', 'P41', 'P2716', 'P2910']
315
+    # P15: route map, P242: locator map, P154: logo, P18: image, P242: map, P41: flag, P2716: collage, P2910: icon
316
+    property_ids = ['P15', 'P242', 'P154', 'P18', 'P242', 'P41', 'P2716', 'P2910']
305 317
 
306 318
     for property_id in property_ids:
307 319
         image = result.xpath(property_xpath.replace('{propertyid}', property_id))
@@ -320,6 +332,7 @@ def add_attribute(attributes, result, property_id, default_label=None, date=Fals
320 332
             label = default_label
321 333
         else:
322 334
             label = extract_text(attribute[0].xpath(label_xpath))
335
+            label = label[0].upper() + label[1:]
323 336
 
324 337
         if date:
325 338
             trim = True
@@ -369,6 +382,7 @@ def add_url(urls, result, property_id=None, default_label=None, url_prefix=None,
369 382
             dom_element = dom_element[0]
370 383
             if not default_label:
371 384
                 label = extract_text(dom_element.xpath(label_xpath))
385
+                label = label[0].upper() + label[1:]
372 386
 
373 387
             if link_type == 'geo':
374 388
                 links.append(get_geolink(dom_element))

+ 17
- 1
searx/results.py View File

@@ -43,6 +43,19 @@ def compare_urls(url_a, url_b):
43 43
 
44 44
 
45 45
 def merge_two_infoboxes(infobox1, infobox2):
46
+    # get engines weights
47
+    if hasattr(engines[infobox1['engine']], 'weight'):
48
+        weight1 = engines[infobox1['engine']].weight
49
+    else:
50
+        weight1 = 1
51
+    if hasattr(engines[infobox2['engine']], 'weight'):
52
+        weight2 = engines[infobox2['engine']].weight
53
+    else:
54
+        weight2 = 1
55
+
56
+    if weight2 > weight1:
57
+        infobox1['engine'] = infobox2['engine']
58
+
46 59
     if 'urls' in infobox2:
47 60
         urls1 = infobox1.get('urls', None)
48 61
         if urls1 is None:
@@ -64,6 +77,8 @@ def merge_two_infoboxes(infobox1, infobox2):
64 77
         img2 = infobox2.get('img_src')
65 78
         if img1 is None:
66 79
             infobox1['img_src'] = img2
80
+        elif weight2 > weight1:
81
+            infobox1['img_src'] = img2
67 82
 
68 83
     if 'attributes' in infobox2:
69 84
         attributes1 = infobox1.get('attributes', None)
@@ -77,7 +92,8 @@ def merge_two_infoboxes(infobox1, infobox2):
77 92
                 attributeSet.add(attribute.get('label', None))
78 93
 
79 94
         for attribute in infobox2.get('attributes', []):
80
-            attributes1.append(attribute)
95
+            if attribute.get('label', None) not in attributeSet:
96
+                attributes1.append(attribute)
81 97
 
82 98
     if 'content' in infobox2:
83 99
         content1 = infobox1.get('content', None)

+ 2
- 0
searx/settings.yml View File

@@ -105,6 +105,7 @@ engines:
105 105
   - name : ddg definitions
106 106
     engine : duckduckgo_definitions
107 107
     shortcut : ddd
108
+    weight : 2
108 109
     disabled : True
109 110
 
110 111
   - name : digg
@@ -127,6 +128,7 @@ engines:
127 128
   - name : wikidata
128 129
     engine : wikidata
129 130
     shortcut : wd
131
+    weight : 2
130 132
 
131 133
   - name : duckduckgo
132 134
     engine : duckduckgo

+ 12
- 10
tests/unit/engines/test_wikidata.py View File

@@ -95,14 +95,14 @@ class TestWikidataEngine(SearxTestCase):
95 95
 
96 96
         results = wikidata.getDetail(response, "Q123", "yua", "yua_MX")
97 97
         self.assertEqual(len(results), 2)
98
-        self.assertEqual(results[0]['title'], 'official website')
98
+        self.assertEqual(results[0]['title'], 'Official website')
99 99
         self.assertEqual(results[0]['url'], 'https://officialsite.com')
100 100
 
101 101
         self.assertEqual(results[1]['infobox'], 'Test')
102 102
         self.assertEqual(results[1]['id'], None)
103 103
         self.assertEqual(results[1]['content'], 'Description')
104 104
         self.assertEqual(results[1]['attributes'], [])
105
-        self.assertEqual(results[1]['urls'][0]['title'], 'official website')
105
+        self.assertEqual(results[1]['urls'][0]['title'], 'Official website')
106 106
         self.assertEqual(results[1]['urls'][0]['url'], 'https://officialsite.com')
107 107
         self.assertEqual(results[1]['urls'][1]['title'], 'Wikipedia (en)')
108 108
         self.assertEqual(results[1]['urls'][1]['url'], 'https://en.wikipedia.org/wiki/Test')
@@ -141,7 +141,8 @@ class TestWikidataEngine(SearxTestCase):
141 141
         html_etree = fromstring(html)
142 142
 
143 143
         image_src = wikidata.add_image(html_etree)
144
-        self.assertEqual(image_src, "https://commons.wikimedia.org/wiki/Special:FilePath/image.png?width=500")
144
+        self.assertEqual(image_src,
145
+                         "https://commons.wikimedia.org/wiki/Special:FilePath/image.png?width=500&height=400")
145 146
 
146 147
         html = u"""
147 148
         <div>
@@ -196,7 +197,8 @@ class TestWikidataEngine(SearxTestCase):
196 197
         html_etree = fromstring(html)
197 198
 
198 199
         image_src = wikidata.add_image(html_etree)
199
-        self.assertEqual(image_src, "https://commons.wikimedia.org/wiki/Special:FilePath/logo.png?width=500")
200
+        self.assertEqual(image_src,
201
+                         "https://commons.wikimedia.org/wiki/Special:FilePath/logo.png?width=500&height=400")
200 202
 
201 203
     def test_add_attribute(self):
202 204
         html = u"""
@@ -234,7 +236,7 @@ class TestWikidataEngine(SearxTestCase):
234 236
 
235 237
         wikidata.add_attribute(attributes, html_etree, "P27")
236 238
         self.assertEqual(len(attributes), 1)
237
-        self.assertEqual(attributes[0]["label"], "country of citizenship")
239
+        self.assertEqual(attributes[0]["label"], "Country of citizenship")
238 240
         self.assertEqual(attributes[0]["value"], "United Kingdom")
239 241
 
240 242
         html = u"""
@@ -269,7 +271,7 @@ class TestWikidataEngine(SearxTestCase):
269 271
         html_etree = fromstring(html)
270 272
         wikidata.add_attribute(attributes, html_etree, "P569", date=True)
271 273
         self.assertEqual(len(attributes), 1)
272
-        self.assertEqual(attributes[0]["label"], "date of birth")
274
+        self.assertEqual(attributes[0]["label"], "Date of birth")
273 275
         self.assertEqual(attributes[0]["value"], "27 January 1832")
274 276
 
275 277
         html = u"""
@@ -317,7 +319,7 @@ class TestWikidataEngine(SearxTestCase):
317 319
         html_etree = fromstring(html)
318 320
         wikidata.add_attribute(attributes, html_etree, "P6")
319 321
         self.assertEqual(len(attributes), 1)
320
-        self.assertEqual(attributes[0]["label"], "head of government")
322
+        self.assertEqual(attributes[0]["label"], "Head of government")
321 323
         self.assertEqual(attributes[0]["value"], "Old Prime Minister, Actual Prime Minister")
322 324
 
323 325
         attributes = []
@@ -355,7 +357,7 @@ class TestWikidataEngine(SearxTestCase):
355 357
         html_etree = fromstring(html)
356 358
         wikidata.add_url(urls, html_etree, 'P856')
357 359
         self.assertEquals(len(urls), 1)
358
-        self.assertIn({'title': 'official website', 'url': 'https://searx.me/'}, urls)
360
+        self.assertIn({'title': 'Official website', 'url': 'https://searx.me/'}, urls)
359 361
         urls = []
360 362
         results = []
361 363
         wikidata.add_url(urls, html_etree, 'P856', 'custom label', results=results)
@@ -403,8 +405,8 @@ class TestWikidataEngine(SearxTestCase):
403 405
         html_etree = fromstring(html)
404 406
         wikidata.add_url(urls, html_etree, 'P856')
405 407
         self.assertEquals(len(urls), 2)
406
-        self.assertIn({'title': 'official website', 'url': 'http://www.worldofwarcraft.com'}, urls)
407
-        self.assertIn({'title': 'official website', 'url': 'http://eu.battle.net/wow/en/'}, urls)
408
+        self.assertIn({'title': 'Official website', 'url': 'http://www.worldofwarcraft.com'}, urls)
409
+        self.assertIn({'title': 'Official website', 'url': 'http://eu.battle.net/wow/en/'}, urls)
408 410
 
409 411
     def test_get_imdblink(self):
410 412
         html = u"""