Recursive Loop

All loops we have looked at in this course so far, process a subset of the input data in each iteration. The recursive loop however, passes the updated data back to the input for the next iteration. Let’s see how we can build a recursive loop in KNIME Analytics Platform.

 

Exercise: Recursive Replacement of Strings

Here is a German sentence containing alphabet characters with umlaute (ä, ö and ü), which are some special non ASCII characters specific to the German language:

Ich wünsche Dir schöne Ferien in Ägypten.

The goal is to replace all of them with their ASCII representation, according to the following look up table.

Umlaut ASCII
Ä Ae
ö oe
ü ue

If you copy this table into a Table Creator node, please make sure that no additional spaces are added to the end of each value.

In each iteration, one replacement should be applied and the updated text of the last iteration should be used as input in the next iteration. The loop should stop once all replacements are done.

Therefore, first create two tables with the Table Creator node. One with the German sentence from above and one with the lookup table.

Then, build a recursive loop using the recursive loop nodes with ports for two updated tables, and define an appropriate stopping criterion and output handling.

Hints:

  • The two tables are the initial input tables for the Recursive Loop Start (2 Ports) node.
  • In each iteration, the loop body should extract one row from the look up table with the Row Splitter node. It should then convert the “umlaut” and the ASCII representation into Flow Variables with the Table Row to Variable node and use them in the String Manipulation node to make the replacement. For example:
KNIME Recursive Loop
  • The modified output of the String Manipulation node is the input for the first two input ports of the Recursive Loop End (2 Ports) node.
    • First input port, because in the last iteration the output of the String Manipulation node is the text after all replacements, which should be passed to the output port.
    • Second input port, because we want to use the updated text in the next iteration.
  • Use the remaining rows of the lookup table (second output port of the Row Splitter node) as the input into the last input port of the loop end node, as the rows are used in the following iterations.
  • The loop is supposed to stop if the lookup table has less than one row. This means all replacements are done. Therefore, the stopping criterion in the configuration dialog of the loop end node should be set to “minimal number of rows” being equal to 1. In addition, the maximum number of iterations should be set to 100 to avoid an endless loop - just in case there is an error in the loop body.
  • In order to take care of the output data handling, activate the checkbox “collect from last iteration only” if you are not interested in the intermediate results from the loop.

Solution:

A possible solution can be found inside the workflow on the KNIME Workflow Hub and on the EXAMPLES Server: EXAMPLES/06_Control_Structures/04_Loops/08_Example_for_Recursive_Replacement_of_Strings

LinkedInTwitterShare