Tuesday, July 18, 2017

If structure cheat sheet


Updated 8/24/17

If structure cheat sheet
<operation> can be any function – reprinting a cell value, concatenating cells values, any string processing function listed in the wiki, and even a foreach, with, or another if function.


Do <operation> only if test condition is false
if (test condition, value, <operation>)
Do <operation> only if test condition is true
if (test condition, <operation>, value)
Do <operation1> if test condition is true, otherwise do <operation 2>
if (test condition, <operation 1>, <operation 2>)
Do a Marcedit Find/Replace on string1 string2 string3 to transform it into string2; string3; string1
if (isNotNull(value.match(/(regex for string1)(regex for string2)(regex for string3)/)), value.match(/(regex for string1)(regex for string2)(regex for string3)/)[1] + “;” + value.match(/(regex for string1)(regex for string2)(regex for string3)/)[2] + “;” + value.match(/(regex for string1)(regex for string2)(regex for string3)/)[0], value)

Test Condition Cheat Sheet

Example data to be used is
auto
autos
auto mechanics
automechanics
auto mechanics and aviation
planes
trains
automobiles
aviation
aviation and automechanics

NOTE : Test conditions can also be used for custom text faceting

Test Condition
Code
String must exactly match auto
value == “auto”
String can be auto, autos, auto mechanics, automechanics, or auto mechanics and aviation
value.contains("auto")
String can be auto mechanics, automechanics, auto  mechanics, or auto mechanics and aviation.
isNotNull(value.match(/(.*auto.*mechanics.*)/))

String can be auto, autos, planes, trains, or automobiles.
isNull(value.match(/(.*auto.*mechanics.*)/))
String must match auto, autos, or planes. or(isNotNull(value.match(/(^auto[s]/))), value == "planes")
String must start with some variation of auto.
value.startsWith("auto")
Find all of the variations of auto mechanics and aviation
and(isNotNull(value.match(/(.*auto\s*mechanics.*)/)), value.contains("aviation"))
All strings that begin with "a"
value.startsWith("a")
All strings that end with "s"
value.endsWith("s")
String must be less than 4 words
length (split(value, “ “)) < 4
String must contain any substring with variable spacing of “auto mechanics” and must be less than 4 words
and(isNotNull(value.match(/.*auto.*mechanics.*/)), length(split(value, " ")) < 4)
Cell is completely blank
isBlank(value)
Cell is not blank
isNonBlank(value)

Test conditions for dates
Test string is a single range of years, such as 1938-1968. No spaces surrounding the hyphen.

First year must be greater than 1938
toNumber(substring(value, 0,4)) > 1938
First year must equal 1938
toNumber(substring(value, 0,4)) == 1938
First year must be less than 1938
toNumber(substring(value, 0,4)) < 1938
Second year must be less than 1968
toNumber(substring(value,5,9)) < 1968
Second year must be equal 1968
toNumber(substring(value,5,9)) == 1968
Second year must be less greater than 1968
toNumber(substring(value,5,9)) > 1968
Second year is greater than the first
toNumber(substring(value,5,9)) > toNumber(substring(value,0,4))

Recipe: Joining two lists and interweaving the terms

Say, in Column 1, you have :
Santa Cruz; Santa Cruz Beach Boardwalk
And in Column 2, you have:
California; Surfing
And you want to join them like so:
Santa Cruz; California; Santa Cruz Beach Boardwalk; Surfing
You can do it using this Jython code (keep the carriage returns): 

first = cells['column1']['value']
split1 = first.split(';')
second = cells['column2']['value']
split2 = second.split(';')
split3 = map('; '.join, zip(split1, split2))
return str.join("; ", split3)

This only works if you have equal amounts of terms in both columns.


Monday, July 17, 2017

Walkthrough on writing a complex if function

Say that from this sample data of a 538 field from video game records, you're only supposed to remove "System requirements" from the entries with a video game console:

System requirements: Nintendo 64
System requirements: Nintendo 64
System requirements: Nintendo 64; designed for N64 Rumble Pak
System requirements: PlayStation 2
System requirements: PlayStation 2
System requirements: Windows 95/98; 133MHz Pentium or faster processor; Windows 95/98; 64MB RAM; Microsoft DirectX 6.0 (included); quad speed CD-ROM drive; PCI or AGP graphics card; 4MB 3D accelerator for 3D graphics support (optional); 16-bit sound card; keyboard and mouse; joystick (optional); gamepad (optional)
System requirements: PlayStation 2
System requirements: PlayStation 2, memory card (for PS2) 94 KB
System requirements: PlayStation 2
System requirements: Nintendo 64. Designed for N64 Rumble Pak
System requirements: Nintendo Entertainment System
System requirements: Windows 98SW/ME/2000/XP; Pentium III 800 MHz or faster; 256 MB RAM; ; 2.5 GB free hard disk space; 32 MB hardware T&L compatible video card; Windows compatible sound card; DirectX version 9.0c (included) or higher;  8x or faster CD-ROM drive
System requirements: PlayStation 2, memory card (for PS2) 177 KB

Recipes : remove the nth word in a delimited string, remove any term between two parens




Click to expand this image.

Thursday, July 6, 2017

Tutorial on objects and the cell.recon object


What's an object?
You've probably seen this wiki page when trying to access the deeper reconciliation fields in Open Refine:

https://github.com/OpenRefine/OpenRefine/wiki/Variables

This is a pretty confusing page, so let me give a little background about objects. An object is a programming language structure that's basically a container for a bunch of other things, including other objects. The object itself cannot be manipulated, but everything contained in that object can.
Open Refine has a master object and then either objects or fields contained within it.

A field will either yield a string, a boolean, a null, a number, or it will have deeper fields.

Monday, July 3, 2017

Introduction to loops and forRange tutorial with variable data

Problem : I have a column of cells with variable date ranges:
1907-1913
1910-1911
1908-1911
etc.

The cells have to be converted to:
1907; 1908; 1909; 1910; 1911; 1912; 1913
1910; 1911
1908; 1909; 1910; 1911

etc.

Open Refine can be used to convert this. Using add column based on this column, you would use the following GREL command:

forRange(toNumber(substring(value, 0,4)), toNumber(substring(value, 5,9)) + 1, 1, currentYear, currentYear).join("; ")


Yes, it looks daunting. But let's break it down, starting with the forRange command:

Introduction to Arrays

Update 7/28/17: I've fixed up this post because when I first wrote it, I didn't realize what Open Refine's convention was for naming arrays.

You've probably heard a lot about arrays when working with Open Refine, but what exactly is it?

First of all, I need to explain how software computing works before I can explain the array functionality. When any sort of software needs to process data, it has to break it down into chunks that it can understand. The chunks are in the computer's equivalent of headspace -- much like when you're trying to figure things out in your head.

However, a computer can only process data using certain structures. There are several of these data structures, but I'm just going to cover the array.