Create all Permutations of strings

Hello!

I want to create permutations of values which are separated by a space in one cell.
Example:
“My house is beautiful”

Desired Result:
My house is beautiful
beautiful is house my
is beautiful my house
is beautiful house my
…and so on!

Could you please help me?
Is there a solution with basic elements in knime?

Best,
Andreas

Hi @Andromir

Is there a max on the number of separated values you expect?

Hi @HansS,

the maximum is around 5.
But it is different , there can be values with 2 or values with 5 word.

You should write it in a Java Snippet node; there should be many Google-hits for permutation algorithms running ~O(n!). The Java Snippet node’s output column should be a String array type which you’d then feed in to an Ungroup to make one row per array item.

Hi @Andromir

I created a workflow that answers your question, but it’s not completely done in KNIME :see_no_evil: I used some python code in a Python node. It takes a string (multiple) as an input and returns a column with all possible combinations of words from this string (the number of permutations) will rapidly grow as the length of the sting increases. It will run for strings with more than 5 words


Hopefully is running Python on your machine not a problem for you :hear_no_evil:
See the workflow: permutations2.knwf (29.4 KB)

gr. Hans

3 Likes

Actually I was doing it with Column Expressions using JavaScript syntax. :sweat_smile:

I won’t give up. :sunglasses:

2 Likes

I tested several different codes in Column Expressions and this one is the finest by far but still I get error.
Can anyone help me with this?

I used a Cell Splitter node to convert the string in column1 “My house is beautiful” to list [My, house, is, beautiful]. Which is column("column1_SplitResultList") in the code.

var arrCol = column("column1_SplitResultList")
var arrLength = column("column1_SplitResultList").length
var arrStr = ""
var arrStrMas = ""
var arrMas = []
var temp = ""
function toStringArrMas(arr){
    for (i=0; i<arr.length; i++){
        if(i==0)arrStrMas = arr[i]
        else arrStrMas = arrStrMas+", "+arr[i]
    }
    return arrStrMas
}
function toStringArr(arr){
    for (i=0; i<arr.length; i++){
        if(i==0)arrStr = arr[i]
        else arrStr = arrStr+" "+arr[i]
    }
    return arrStr
}
function permute(arr, len, n){
    if(len==1) arrMas= arrMas.push(toStringArr(arr))
    for (i=0; i<len; i++){
        permute(arr, len-1, n)
        if (len % 2 == 1) { 
                temp = arr[0]
                arr[0] = arr[len-1]
                arr[len-1] = temp
            } 
            else { 
                temp = arr[i]
                arr[i] = arr[len-1]
                arr[len-1] = temp
            } 
    }
}
var finalArr = permute(arrCol, arrLength, arrLength)
toStringArrMas(finalArr)

The toStringArr function is to transform each permutation inside the permute function to string (the separator is space). The output of the permute function is array.
The toStringArrMas function is to transform the output of permute function to string (the separator is comma).

The source of the algorithm:

:blush:

2 Likes

Here ya go - armin’s permute.knwf (7.8 KB)

1 Like

Many many thanks @quaeler. Really fantastic! :+1::+1:

Here is the final workflow which transforms each sentence to several permutations in rows (or as a list) by using Column Expressions:
permute.knwf (21.5 KB)

Again many thanks to you @quaeler. Would you please give me a feedback of the code I was using. I want to know my mistakes.

:blush:

2 Likes

My pleasure. Things i noticed about your code above:

  • toStringArrMas and toStringArr did the same thing, only using a different delimiter; it made sense to abstract to a single method
  • those two methods affected global variables - it made more sense to affect a local variable and return that
  • you were treating the permute(... method like it returned something, but it did not
1 Like

Thank you so much for taking the time to make it clear to me.:+1:
:blush:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.