Created
March 7, 2016 16:25
-
-
Save willkg/e827829d3d8065a0c5bc to your computer and use it in GitHub Desktop.
Run a search and compare against previous search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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