This might be similar to Java : Cartesian Product of a List of Lists but not answering my question.
I created the following
TreeMap<String, Set<String>> aMapOfSet
aMapOfSet
represents different words in a sentence and Set<String>
will contain all variations to the words if there is no variation then set will be empty/null for that word key.
I want to write a method that would take aMapOfSet and returns a Set of all possible sentences.
For instance, the original sentence could be:
tss xes wxy xyz
suppose there are total of 3 variations for the word "wxy" and total of 2 variations for the word "xyz"
Then aMapOfSet would look like this
tss
xes
wxy -> [wxys,wxyes]
xyz -> [xyzs]
The answer would be 6 sentences in resultSet
tss xes wxy xyz
tss xes wxys xyz
tss xes wxyes xyz
tss xes wxy xyzs
tss xes wxys xyzs
tss xes wxyes xyzs
I used treeMap to preserve the sequence of words.
Here is my work in progress code:
Set<String> getCartesianProduct(TreeMap<String, Set<String>> wordVariationSet)
{
Set<String> resultSet =new HashSet<String>();// to store answer
for(String theOriginalWord: wordVariationSet.keySet())
{
for(String word:wordVariationSet.get(theOriginalWord))
{
// TODO create a sentence with 1 space between words and add to resultSet
}
}
return resultSet;
}
I will update the code as I make more progress.
What is the best way to iterate through all variations, so to get all 6 sentences.