I had a set of data where I needed to only process sentences that were more than a certain number of words. Fortunately, doing so is relatively easy.
First of all, the words in a sentence can be split up into an array using value.split(" ").
Secondly, the length() function in Open Refine returns the length, or total number of elements in an array. Since each element is a word, put these two together and the number of words will be calculated:
Monday, July 31, 2017
Using splitByCharType to extract years of coverage in an 856 field
GREL's splitByCharType() is actually quite a useful function. It takes a string, splits it up, and then groups consecutive like characters together in an array. Some of the more common like characters are all lowercase letters, all uppercase letters, all numbers, and all spaces. Special characters and punctuation are only grouped together with the exact same character (e.g. $$ would be grouped together, but $! would not.)
For example, for the string: $a300 pages ;$c20 cm, it'll fill the array this way:
Index Element
0 $
1 a
2 300
3 <space>
4 pages
5 <space>
6 ;
7 $
8 c
9 20
10 <space>
11 cm
For example, for the string: $a300 pages ;$c20 cm, it'll fill the array this way:
Index Element
0 $
1 a
2 300
3 <space>
4 pages
5 <space>
6 ;
7 $
8 c
9 20
10 <space>
11 cm
Friday, July 28, 2017
Custom faceting using booleans
So, here's some sample data:
For the editing I want to do, I would like only the lines with v.<number>(<year>)-v<number>(<year>)
Since all of the notations vary, I tried the custom facet:
value.startsWith("v")
The problem is that lines containing data like this weren't excluded:
v.34(2008)-;v.1(1974)-v.26(2000)
Using the Marcedit capturing group Find/Replace in Open Refine, pt.2 : putting it into an if function.
This is a continuation from this post about how to mimic Marcedit capturing group Find/Replace in Open Refine.
The one problem with mimicking the Marcedit Find/Replace is that it will wipe out the data you don't want to alter unless you facet it out first. This is kind of cumbersome and still doesn't have the convenience of Marcedit's Find/Replace.
However, the if function takes care of that problem. I'm going to post the recipe here at the top and then go into detail further down. So, for your capturing group find of value.match(/<capturing group regex>/), do a transform cells, and drop it into this statement:
if(isNotNull(value.match(/<your find>/)), <your replace>, value)
The one problem with mimicking the Marcedit Find/Replace is that it will wipe out the data you don't want to alter unless you facet it out first. This is kind of cumbersome and still doesn't have the convenience of Marcedit's Find/Replace.
However, the if function takes care of that problem. I'm going to post the recipe here at the top and then go into detail further down. So, for your capturing group find of value.match(/<capturing group regex>/), do a transform cells, and drop it into this statement:
if(isNotNull(value.match(/<your find>/)), <your replace>, value)
Unpacking value.match with reg ex and how to use it in boolean statements
The value.match documentation on the Open Refine github wiki is a bit daunting, so I'm going to try to break it down in this post.
Some programming speak first: GREL functions usually yield some sort of data after they're done doing their thing. That data is called a return value. So, that's what I'm referring to whenever I make statements like, "match returns <whatever>".
Anyway, the biggest hurdle to my understanding was figuring out how match and regular expressions worked together. Here's what I've determined:
Unlike Marcedit, you have to account for the whole line if you're using regex in match. Thursday, July 27, 2017
How to do a Marcedit Find/Replace with capturing groups in Open Refine
One of the things that's really frustrated me with Open Refine is that there didn't seem to be a way to mimic the capturing group rearranging from the Find/Replace function in Marcedit.
For example, in this import of Marcedit records, some of the subfield a's have an indicator in front of them:
In Marcedit, I'd normally do this:
But I couldn't figure out how to do the equivalent in Open Refine. value.match seemed like a contender, but I couldn't figure out how to access the array elements. I finally figured it out today --
For example, in this import of Marcedit records, some of the subfield a's have an indicator in front of them:
![]() |
| Click on image to enlarge |
In Marcedit, I'd normally do this:
| click on image to enlarge |
But I couldn't figure out how to do the equivalent in Open Refine. value.match seemed like a contender, but I couldn't figure out how to access the array elements. I finally figured it out today --
Comments are now enabled and a label widget has been added.
I didn't realize that comments were disabled for non-Google users. I've changed that, so comment away if you need clarification on my posts or if you would like me to cover something.
I also didn't realize that a label widget wasn't displaying. So I fixed that.
I also didn't realize that a label widget wasn't displaying. So I fixed that.
Subscribe to:
Posts (Atom)
