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.
I created a workflow that answers your question, but it’s not completely done in KNIME 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
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).
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.