Permalink
Browse files

added example service and sample data

  • Loading branch information...
1 parent 07ba030 commit 6edda797ec390d39f1f973ec254c1750ff5e87b2 @adorsk-whoi adorsk-whoi committed Dec 12, 2011
Showing with 105 additions and 0 deletions.
  1. +5 −0 example_composers.csv
  2. +100 −0 example_composers_reconciliation_service.py
  3. BIN flask/__init__.pyc
  4. BIN flask/app.pyc
  5. BIN flask/blueprints.pyc
  6. BIN flask/config.pyc
  7. BIN flask/ctx.pyc
  8. BIN flask/debughelpers.pyc
  9. BIN flask/globals.pyc
  10. BIN flask/helpers.pyc
  11. BIN flask/module.pyc
  12. BIN flask/sessions.pyc
  13. BIN flask/signals.pyc
  14. BIN flask/templating.pyc
  15. BIN flask/wrappers.pyc
  16. BIN fuzzywuzzy/__init__.pyc
  17. BIN fuzzywuzzy/fuzz.pyc
  18. BIN fuzzywuzzy/utils.pyc
  19. BIN jinja2/__init__.pyc
  20. BIN jinja2/bccache.pyc
  21. BIN jinja2/compiler.pyc
  22. BIN jinja2/defaults.pyc
  23. BIN jinja2/environment.pyc
  24. BIN jinja2/exceptions.pyc
  25. BIN jinja2/filters.pyc
  26. BIN jinja2/lexer.pyc
  27. BIN jinja2/loaders.pyc
  28. BIN jinja2/nodes.pyc
  29. BIN jinja2/optimizer.pyc
  30. BIN jinja2/parser.pyc
  31. BIN jinja2/runtime.pyc
  32. BIN jinja2/tests.pyc
  33. BIN jinja2/utils.pyc
  34. BIN jinja2/visitor.pyc
  35. BIN simplejson/__init__.pyc
  36. BIN simplejson/decoder.pyc
  37. BIN simplejson/encoder.pyc
  38. BIN simplejson/ordered_dict.pyc
  39. BIN simplejson/scanner.pyc
  40. BIN werkzeug/__init__.pyc
  41. BIN werkzeug/_internal.pyc
  42. BIN werkzeug/contrib/__init__.pyc
  43. BIN werkzeug/contrib/securecookie.pyc
  44. BIN werkzeug/contrib/sessions.pyc
  45. BIN werkzeug/datastructures.pyc
  46. BIN werkzeug/debug/__init__.pyc
  47. BIN werkzeug/debug/console.pyc
  48. BIN werkzeug/debug/repr.pyc
  49. BIN werkzeug/debug/tbtools.pyc
  50. BIN werkzeug/exceptions.pyc
  51. BIN werkzeug/formparser.pyc
  52. BIN werkzeug/http.pyc
  53. BIN werkzeug/local.pyc
  54. BIN werkzeug/posixemulation.pyc
  55. BIN werkzeug/routing.pyc
  56. BIN werkzeug/security.pyc
  57. BIN werkzeug/serving.pyc
  58. BIN werkzeug/urls.pyc
  59. BIN werkzeug/utils.pyc
  60. BIN werkzeug/wrappers.pyc
  61. BIN werkzeug/wsgi.pyc
View
@@ -0,0 +1,5 @@
+"label","id"
+"Ludwig van Beethoven","http://en.wikipedia.org/wiki/Ludwig_van_Beethoven"
+"Johann Sebastian Bach","http://en.wikipedia.org/wiki/Johann_Sebastian_Bach"
+"Wolfgang Amadeus Mozart","http://en.wikipedia.org/wiki/Wolfgang_Amadeus_Mozart"
+"George Frideric Handel","http://en.wikipedia.org/wiki/George_Frideric_Handel"
@@ -0,0 +1,100 @@
+#!/usr/bin/env python
+
+"""
+An example reconciliation service API for Google Refine 2.0.
+
+See http://code.google.com/p/google-refine/wiki/ReconciliationServiceApi.
+"""
+
+import csv
+from fuzzywuzzy import fuzz
+import re
+import sys
+import simplejson as json
+from flask import Flask, request, jsonify
+app = Flask(__name__)
+
+
+# Matching threshold.
+match_threshold = 70
+
+# Basic service metadata. There are a number of other documented options
+# but this is all we need for a simple service.
+metadata = {
+ "name": "Person Reconciliation Service",
+ "defaultTypes": [{"id": "/people/person", "name" : "Person"}]
+ }
+
+# Read in person records from csv file.
+reader = csv.DictReader(open('example_composers.csv', 'rb'))
+records = [r for r in reader]
+
+
+def search(query):
+
+ # Initialize matches.
+ matches = []
+
+ # Search person records for matches.
+ for r in records:
+ score = fuzz.token_set_ratio(query, r['label'])
+
+ if score > match_threshold:
+ matches.append({
+ "id": r['id'],
+ "name": r['label'],
+ "score": score,
+ "match": query == r['label'],
+ "type": [{"id": "/people/person", "name": "Person"}]
+ })
+
+ print >> sys.stderr, matches
+ return matches
+
+
+def jsonpify(obj):
+ """
+ Like jsonify but wraps result in a JSONP callback if a 'callback'
+ query param is supplied.
+ """
+ try:
+ callback = request.args['callback']
+ response = app.make_response("%s(%s)" % (callback, json.dumps(obj)))
+ response.mimetype = "text/javascript"
+ return response
+ except KeyError:
+ return jsonify(obj)
+
+
+@app.route("/reconcile", methods=['POST', 'GET'])
+
+def reconcile():
+ # If a single 'query' is provided do a straightforward search.
+ query = request.form.get('query')
+ if query:
+ # If the 'query' param starts with a "{" then it is a JSON object
+ # with the search string as the 'query' member. Otherwise,
+ # the 'query' param is the search string itself.
+ if query.startswith("{"):
+ query = json.loads(query)['query']
+ results = search(query)
+ return jsonpify({"result": results})
+
+ # If a 'queries' parameter is supplied then it is a dictionary
+ # of (key, query) pairs representing a batch of queries. We
+ # should return a dictionary of (key, results) pairs.
+ queries = request.form.get('queries')
+ if queries:
+ queries = json.loads(queries)
+ results = {}
+ for (key, query) in queries.items():
+ results[key] = {"result": search(query['query'])}
+ return jsonpify(results)
+
+ # If neither a 'query' nor 'queries' parameter is supplied then
+ # we should return the service metadata.
+ return jsonpify(metadata)
+
+if __name__ == '__main__':
+ #print search("Doe, John")
+ app.run(debug=True)
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.

0 comments on commit 6edda79

Please sign in to comment.