convert MultiLabelDataset to Guava Multima

2020-04-28 08:29发布

I'm working with the following program:

public static void main(String[] args) throws Exception
{
    String input = args[0];

    InputStream is = new BufferedInputStream( new FileInputStream( input ) );

    toMultiLabelDataset(is);
    is.close();
}

public static MultiLabelDataset<String, String> toMultiLabelDataset(InputStream is) throws IOException
{
    List<RelationAndMentions> relations = toRelations(is, true);
    MultiLabelDataset<String, String> dataset = toDataset(relations);

    return dataset;
}

As you can see it utilizes a data structure of the form MultiLabelDataset<String, String>, I want to print the contents of that beast in a human intelligabe form. According to my previous searching I can likely use apache string utils or google guava library, is that right? How would I go about going that?

My guess would be that theres an equivalent data struc in those libraries, one that takes <String, String>, I just need to determine what that is, copy the contents of MultiLabelDataset<String, String> and then print? Is that reasonable?

EDIT


    Multimap<String, String> result = HashMultimap.create();
    for (String key : ((MultiLabelDataset) dataset).items) 
    {
        for (String value : dataset.getLabels()) 
        {
            result.put(key, value);
        }
    }

^not like that.

1条回答
▲ chillily
2楼-- · 2020-04-28 08:31

Assuming it's this MultiLabelDataset, then it's similar to Guava's Multimap and you can convert it easily. However, this conversion will be neither simpler nor smarter than a direct conversion to a String. The only advantage would be that thereafter you can work with a sane Java class having many useful method and working well together with other Java classes.

The whole conversion goes like always, but you need something to iterate the keys. Assuming the only implementation is LabeledLDADataset, it's easy:

Multimap<String, String> toMultimap(MultiLabelDataset<String, String> dataset) {
    Multimap<String, String> result = HashMultimap.create();
    for (Item key : ((LabeledLDADataset) dataset).items()) {
        result.putAll(key.toString(), dataset.getLabels(key));
    }
    return result;
}
查看更多
登录 后发表回答