0

I would need a little help.

I have TreeMap inside TreeMap TreeMap<String,TreeMap<String,Integer>>

My text output looks like this (example of single line):

Phoneme "Y":     ["**"=46,15%] ["I"=30,77%] ["Y"=7,69%] ["i:"=15,38%] 

It means That phoneme Y has been in 31% recognized as I, in 7,7% as Y etc...

As a result I need a table (spreadsheet) which has on vertical (selected phoneme) and horizontal (prediction) lines every Phoneme and in cells the probability corresponding to selected line and row. This means that for example on line "Y" would be probabilities stated as above, everything else 0%

What would be the best solution for that? Should I programm it manually to get these information and save it sequentially in csv or is there some better way?

Lukadlo
  • 21
  • 2
  • I think you have to go with an API. See [here](http://stackoverflow.com/questions/3487123/creating-a-csv-file-in-java-from-a-hashmap) – fishi0x01 Jan 30 '15 at 18:11

2 Answers2

1

There's nothing wrong with using an API, especially if you're expecting your data to contain special characters. However here is a Java 8 one liner you could use and if you have a CSV API it would be even cleaner:

treeMap.entrySet().forEach((e)->{
    System.out.print("\""+e.getKey()+"\"");
    e.getValue().entrySet().forEach((m)->System.out.print(",\"" + m.getKey() + "\"," + m.getValue()));
    System.out.println();
});

"Y","**",46.15,"I",30.77,"Y",7.69,"i",15.38

JimW
  • 186
  • 8
0

Here's an example of creating tabular CSV output using traditional Java with no external libraries.

static String toCSV(Map<String, Map<String, Integer>> phonemeStats)
{
    StringBuilder csv = new StringBuilder(512);

    // get ordered set of all statistic names
    Set<String> statNames = new TreeSet<String>();
    for (Map<String, Integer> stats: phonemeStats.values()) {
        statNames.addAll(stats.keySet());
    }

    // write column headings line
    csv.append("phoneme");
    for (String name: statNames) {
        csv.append(",").append(name);
    }

    // write each phoneme line
    for (Map.Entry<String, Map<String, Integer>> entry: phonemeStats.entrySet()) {
        csv.append(System.lineSeparator());
        csv.append(entry.getKey());
        for (String name: statNames) {
            Integer value = entry.getValue().get(name);
            csv.append(",");
            if (value != null) {
                csv.append((value / 100d) + "%");
            }
        }
    }

    return csv.toString();
}

Using this input (as Map<String, Map<String, Integer>>):

Phoneme "Y":     ["**"=46,15%] ["I"=30,77%] ["Y"=7,69%] ["i:"=15,38%] 
Phoneme "Z":     ["**"=32,45%] ["I"=10,42%] ["X"=26,97%] ["i:"=5,38%] 

The above method returns:

phoneme,**,I,X,Y,i:
Y,46.15%,30.77%,,7.69%,15.38%
Z,32.45%,10.42%,26.97%,,5.38%

Then you can just write that output to a .CSV file and open it in Excel.

gknicker
  • 5,509
  • 2
  • 25
  • 41