Skip to content

Instantly share code, notes, and snippets.

@willkg
Created March 7, 2016 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willkg/e827829d3d8065a0c5bc to your computer and use it in GitHub Desktop.
Save willkg/e827829d3d8065a0c5bc to your computer and use it in GitHub Desktop.
Run a search and compare against previous search
import json
import os
try:
import requests
from tabulate import tabulate
except ImportError:
print 'Please install "requests" and "tabulate" packages.'
URL = 'https://developer-local.allizom.org/en-US/search?q=bind&format=json&per_page=20'
resp = requests.get(URL, verify=False)
data = json.loads(resp.content)
if not os.path.exists('original.json'):
orig_data = json.loads(resp.content)
with open('original.json', 'w') as fp:
json.dump(orig_data, fp)
else:
with open('original.json', 'r') as fp:
orig_data = json.load(fp)
def get_index_and_score(id_, orig_data):
for i, doc in enumerate(orig_data['documents']):
if doc['id'] == id_:
return (i, doc['score'])
return (None, None)
print 'URL:', URL
print 'Count:', data['count']
print 'Query:', data['query']
table = []
for i, doc in enumerate(data['documents']):
orig_i, orig_score = get_index_and_score(doc['id'], orig_data)
table.append((i, orig_i, doc['score'], orig_score, doc['id'], doc['tags'], doc['title']))
print tabulate(table, headers=['i', 'orig i', 'score', 'orig score', 'id', 'tags', 'title'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment