user4506542
user4506542

Reputation:

generate and output all possible combinations, from three List<String> only once per unique item

I'm trying to generate all possible combinations of sentences. As variables I have two Strings, one String will be the subject, e.g. health and one will be an object, such as fruit, but then I will have a List<String> of values associated with one "head" word, so in keeping with the two components just mentioned, they would be associated with the list [improve, change, alter, modify]. I want to generate all possible combinations of these sentences and add each one to the List<Sentences> with something like:

Sentence example_sentence = new Sentence(verb, object, subject);
sentences.add(example_sentence);

Right now the larger function in which this is taking place looks like this:

public Sentence dataPreprocessing(String raw_subject, String raw_object, String raw_verb, List<Sentence> sentences) throws IOException {
    WordNet wordnet = new WordNet();
    String verb = wordnet.getStem(raw_verb);
    String object = wordnet.getStem(raw_object);        
    String subject = wordnet.getStem(raw_subject);
    List<String> verb_hypernym_container = new ArrayList<>();       
    verb_hypernym_container = wordnet.getHypernyms(verb, POS.VERB);
    //wordnet.getHypernyms(this.object, POS.NOUN);  
    //wordnet.getHypernyms(this.subject, POS.NOUN); 
    Sentence return_sentence = new Sentence( verb, object, subject );
    return return_sentence;
}

How can I most effectively achieve this goal of generating all possible sentences?

Upvotes: 1

Views: 389

Answers (2)

sprinter
sprinter

Reputation: 27966

Once you have a list of nouns and verbs you could use streams to return a list of sentences. This also gives you a chance to remove any duplicates, sort or anything else you need to do to the stream.

List<Sentence> sentences = subjectList.stream()
        .flatMap(object -> verbList.stream()
            .flatMap(verb -> objectList.stream()
                .map(subject -> new Sentence(object, verb, subject))))
        .distinct()
        .collect(Collectors.toList());

Upvotes: 1

Since you have a fixed number of lists, the simplest way is to just use nested loops:

List<Sentence> sentences = new ArrayList<>();

for(String verb_hypernym : wordnet.getHypernyms(verb, POS.VERB))
    for(String object_hypernym : wordnet.getHypernyms(object, POS.NOUN))
        for(String subject_hypernym : wordnet.getHypernyms(subject, POS.NOUN))
            sentences.add(new Sentence(verb_hypernym, object_hypernym, subject_hypernym));

return sentences;

Or, to avoid calling getHypernyms more often than necessary:

List<String> verb_hypernyms = wordnet.getHypernyms(verb, POS.VERB);
List<String> object_hypernyms = wordnet.getHypernyms(object, POS.NOUN);
List<String> subject_hypernyms = wordnet.getHypernyms(subject, POS.NOUN);


for(String verb_hypernym : verb_hypernyms)
    for(String object_hypernym : object_hypernyms)
        for(String subject_hypernym : subject_hypernyms)
            sentences.add(new Sentence(verb_hypernym, object_hypernym, subject_hypernym));

return sentences;

Upvotes: 2

Related Questions