edu.cmu.sv.utils.Combination Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yoda Show documentation
Show all versions of yoda Show documentation
A library that allows rapid prototyping of dialog systems (language understanding, discourse modelling, dialog management, language generation).
package edu.cmu.sv.utils;
import org.apache.commons.math3.random.RandomData;
import org.apache.commons.math3.random.RandomDataImpl;
import java.util.*;
import java.util.stream.Collectors;
/**
* Created by David Cohen on 9/2/14.
*/
public class Combination {
static RandomData randomData = new RandomDataImpl();
public static Set randomSubset(Collection items, int k){
if (items.size() > k){
return Arrays.asList(randomData.nextSample(
items, k)).stream().
map(x -> (T)x).
collect(Collectors.toSet());
}
else
return new HashSet<>(items);
}
public static Set> combinations(Set items){
Set> ans = new HashSet<>();
if (items.size()==1) {
ans.add(new LinkedList(items));
return ans;
} else if (items.size()==0){
return ans;
}
T item = new LinkedList<>(items).get(0);
items.remove(item);
for (List subList : combinations(items)){
subList.add(item);
ans.add(subList);
}
return ans;
}
public static Set