refine-ruby
is a Ruby Gem client library for OpenRefine. It was written for Google Refine 2.x, but should work with OpenRefine as well.
If you want to port this to another language, check out the Refine API documentation.
NOTE: The Refine client/server protocol is an internal API which is subject to change, so use at your own risk (although it has stayed relatively stable for the last few versions).
Install
gem install refine-ruby
Example
Given that you have the following raw data:
Date
7 December 2001
July 1 2002
10/20/10
Google Refine lets you clean up the data and export your operation history as a JSON instruction set. Here is an example that extracts the year from the above dates:
[
{
"op": "core/text-transform",
"description": "Text transform on cells in column Date using expression grel:value.toDate()",
"engineConfig": {
"facets": [],
"mode": "row-based"
},
"columnName": "Date",
"expression": "grel:value.toDate()",
"onError": "set-to-blank",
"repeat": false,
"repeatCount": 10
},
{
"op": "core/text-transform",
"description": "Text transform on cells in column Date using expression grel:value.datePart(\"year\")+1",
"engineConfig": {
"facets": [],
"mode": "row-based"
},
"columnName": "Date",
"expression": "grel:value.datePart(\"year\")",
"onError": "set-to-blank",
"repeat": false,
"repeatCount": 10
}
]
You can use this gem to apply the operation set to the raw data from ruby. You will need to have Google Refine running on your local computer, or specify an external address (see source):
require 'refine'
prj = Refine.new('project_name' => 'date cleanup', 'file_name' => 'dates.txt')
prj.apply_operations('operations.json')
puts prj.export_rows('csv')
prj.delete_project
Which outputs:
Date
2001
2002
2010
Copyright
Copyright © 2018 David Huynh, Max Ogden & Distill Analytics Inc.