Improving Wolfram Alpha search hit content

Making WA search hits contain
- the (parsed) input inside the "title" instead of just "Wolfram|Alpha", to better match other hit titles and to confirm correct parsing of input to the user
- the first output field that contains any text (skipping ones that are only pictures; this is usually the most meaningful "result" field) instead of the raw input as the "content", making it additionally possible to obtain WA computations from JSON API calls
This commit is contained in:
Lorenzo J. Lucchini 2016-07-07 00:33:03 +02:00 committed by firebovine
parent 09ee2aa69d
commit a8907224a1

View File

@ -18,7 +18,6 @@ api_key = '' # defined in settings.yml
# xpath variables # xpath variables
failure_xpath = '/queryresult[attribute::success="false"]' failure_xpath = '/queryresult[attribute::success="false"]'
answer_xpath = '//pod[attribute::primary="true"]/subpod/plaintext'
input_xpath = '//pod[starts-with(attribute::id, "Input")]/subpod/plaintext' input_xpath = '//pod[starts-with(attribute::id, "Input")]/subpod/plaintext'
pods_xpath = '//pod' pods_xpath = '//pod'
subpods_xpath = './subpod' subpods_xpath = './subpod'
@ -76,11 +75,11 @@ def response(resp):
try: try:
infobox_title = search_results.xpath(input_xpath)[0].text infobox_title = search_results.xpath(input_xpath)[0].text
except: except:
infobox_title = None infobox_title = ""
pods = search_results.xpath(pods_xpath) pods = search_results.xpath(pods_xpath)
result = ""
result_chunks = [] result_chunks = []
result_content = ""
for pod in pods: for pod in pods:
pod_id = pod.xpath(pod_id_xpath)[0] pod_id = pod.xpath(pod_id_xpath)[0]
pod_title = pod.xpath(pod_title_xpath)[0] pod_title = pod.xpath(pod_title_xpath)[0]
@ -97,8 +96,9 @@ def response(resp):
if content and pod_id not in image_pods: if content and pod_id not in image_pods:
if pod_is_result: if pod_is_result or not result_content:
result = content if pod_id != "Input":
result_content = "%s: %s" % (pod_title, content)
# if no input pod was found, title is first plaintext pod # if no input pod was found, title is first plaintext pod
if not infobox_title: if not infobox_title:
@ -115,6 +115,8 @@ def response(resp):
if not result_chunks: if not result_chunks:
return [] return []
title = "Wolfram|Alpha (%s)" % infobox_title
# append infobox # append infobox
results.append({'infobox': infobox_title, results.append({'infobox': infobox_title,
'attributes': result_chunks, 'attributes': result_chunks,
@ -122,7 +124,7 @@ def response(resp):
# append link to site # append link to site
results.append({'url': resp.request.headers['Referer'].decode('utf8'), results.append({'url': resp.request.headers['Referer'].decode('utf8'),
'title': infobox_title + ' - Wolfram|Alpha', 'title': title,
'content': result}) 'content': result_content})
return results return results