GREL - Case or Switch Statement?

300 views
Skip to first unread message

MRB

unread,
May 28, 2019, 9:40:13 AM5/28/19
to OpenRefine
Greetings,
I didn't see any mention but was wondering if they exist in GREL.  I need to create a new column that consolidates 20+ status indicators into 2 values and was looking for a better way to it consolidate than using a very deep set of nested if statements. 
I don't know Python/Jython/Cloture - but if can be done that way, I would appreciate a sample of how to do it.

Thanks,
Mark


Owen Stephens

unread,
May 28, 2019, 10:05:12 AM5/28/19
to OpenRefine
Hi Mark,

Can you give any more information on exactly how the 20+ status indicators are consolidated into the 2 values?

There isn't a case or switch statement in GREL, but it might be possible to use a `filter` or `forEach` (or one of the forEach variants) to achieve the outcome you want in GREL while avoiding heavily nested `if` statements.

Python/Jython also lacks a case/switch statement, although you can write code that uses if ... elif ... elif ... else as a simple way of achieving a similar outcome OR you can approach the problem in a different way, such as using a Python dictionary (more on the lack of case/switch in Python at https://www.pydanny.com/why-doesnt-python-have-switch-case.html). But again having an example of the type of data you are working with would give an idea of the best approach in Python

I don't know Clojure although case statements in Clojure look pretty straightforward based on https://clojuredocs.org/clojure.core/case

Owen

MRB

unread,
May 28, 2019, 10:14:44 AM5/28/19
to OpenRefine
Hi Owen,

I have a column named Status, with text values such as 'FIXED', 'Closed', 'Done', and 'COMPLETED' that I want to consolidate into a new status field NewStatus  as 'Closed'.  
There are eleven other values such as 'Open', 'Awaiting Dev', 'Awaiting UAT', etc that I want to consolidate into the NewStatus field as 'Open'

This would let me quickly separate out open vs closed tickets.

Thanks,
Mark

Owen Stephens

unread,
May 28, 2019, 10:25:01 AM5/28/19
to OpenRefine
So there are a few ways of approaching this in OpenRefine :)

If you want to do it in one GREL expression I'd use `contains` with a regular expression like:

if(value.contains(/(FIXED|Closed|Done|COMPLETED)/),"Closed","Open")

Just add to the list of terms in the brackets separating each "Closed" value with the pipe | character

An alternative approach using Facets is:

* Create a Text Facet on the column
* Select all the "Closed" statuses in the Facet
* Do a GREL transform on the rows to put the value "Closed" in all these rows
* Invert the selection to have all the "Open" status rows
* Do a GREL transform on the rows to put the value "Open" in all these rows

While this is a little clunky the first time, once you've done this in one project, you can apply the same changes from the History to any other projects you have easily - so you only have to do it once.

Hope all of that makes sense - let me know if you have any questions

Owen

MRB

unread,
May 28, 2019, 10:27:57 AM5/28/19
to OpenRefine
Thank you Owen, I will try that approach!
-Mark


On Tuesday, May 28, 2019 at 9:40:13 AM UTC-4, MRB wrote:

Owen Stephens

unread,
May 28, 2019, 10:29:09 AM5/28/19
to OpenRefine
Note that rather than creating a new column for the 'NewStatus' value, you can use a Custom Text facet with an expression like:

if(value.contains(/(FIXED|Closed|Done|COMPLETED)/),"Closed","Open")

This would give you the ability to easily filter Open vs Closed without creating an additional column.

It doesn't really matter, but just highlighting this - I use Custom Text facets a lot for this type of task

Owen

Mark Bustin

unread,
May 28, 2019, 3:35:40 PM5/28/19
to openr...@googlegroups.com
Owen


On Tuesday, May 28, 2019, Owen Stephens <ow...@ostephens.com> wrote:
Note that rather than creating a new column for the 'NewStatus' value, you can use a Custom Text facet with an expression like:

if(value.contains(/(FIXED|Closed|Done|COMPLETED)/),"Closed","Open")

This would give you the ability to easily filter Open vs Closed without creating an additional column.

It doesn't really matter, but just highlighting this - I use Custom Text facets a lot for this type of task

Owen

On Tuesday, May 28, 2019 at 3:25:01 PM UTC+1, Owen Stephens wrote:
So there are a few ways of approaching this in OpenRefine :)

If you want to do it in one GREL expression I'd use `contains` with a regular expression like:

if(value.contains(/(FIXED|Closed|Done|COMPLETED)/),"Closed","Open")

Just add to the list of terms in the brackets separating each "Closed" value with the pipe | character

An alternative approach using Facets is:

* Create a Text Facet on the column
* Select all the "Closed" statuses in the Facet
* Do a GREL transform on the rows to put the value "Closed" in all these rows
* Invert the selection to have all the "Open" status rows
* Do a GREL transform on the rows to put the value "Open" in all these rows

While this is a little clunky the first time, once you've done this in one project, you can apply the same changes from the History to any other projects you have easily - so you only have to do it once.

Hope all of that makes sense - let me know if you have any questions

Owen,
If you create a custom facet - how do you invoke it?
 

On Tuesday, May 28, 2019 at 3:14:44 PM UTC+1, MRB wrote:
Hi Owen,

I have a column named Status, with text values such as 'FIXED', 'Closed', 'Done', and 'COMPLETED' that I want to consolidate into a new status field NewStatus  as 'Closed'.  
There are eleven other values such as 'Open', 'Awaiting Dev', 'Awaiting UAT', etc that I want to consolidate into the NewStatus field as 'Open'

This would let me quickly separate out open vs closed tickets.

Thanks,
Mark



On Tuesday, May 28, 2019 at 9:40:13 AM UTC-4, MRB wrote:
Greetings,
I didn't see any mention but was wondering if they exist in GREL.  I need to create a new column that consolidates 20+ status indicators into 2 values and was looking for a better way to it consolidate than using a very deep set of nested if statements. 
I don't know Python/Jython/Cloture - but if can be done that way, I would appreciate a sample of how to do it.

Thanks,
Mark


--
You received this message because you are subscribed to the Google Groups "OpenRefine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openrefine+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/openrefine/702e1bae-9f37-4c62-928d-317e1e749518%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Owen Stephens

unread,
May 30, 2019, 8:37:18 AM5/30/19
to OpenRefine
Hi Mark

Just go into the Facet menu in a column and you should see the option to create a Custom Text Facet

Owen

Reply all
Reply to author
Forward
0 new messages