/*
 * Copyright 2018 Dmitry Ustalov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.nlpub.watset.cli;

import com.beust.jcommander.Parameter;
import org.jgrapht.Graph;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.nlpub.graph.DummyClustering;
import org.nlpub.vsm.DummyContextSimilarity;
import org.nlpub.watset.Application;
import org.nlpub.watset.Watset;
import org.nlpub.watset.sense.Sense;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Locale;
import java.util.Map;

import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;

public class CommandSenses implements Runnable {
    private final Application application;

    public CommandSenses(Application application) {
        this.application = application;
    }

    @Parameter(required = true, description = "Local clustering algorithm", names = {"-l", "--local"})
    private String local;

    @Parameter(description = "Local clustering algorithm parameters", names = {"-lp", "--local-params"})
    private String localParams;

    @Override
    public void run() {
        requireNonNull(local);

        final AlgorithmProvider<String, DefaultWeightedEdge> algorithm = new AlgorithmProvider<>(local, localParams);

        final Graph<String, DefaultWeightedEdge> graph = application.getGraph();

        final Watset<String, DefaultWeightedEdge> watset = new Watset<>(
                graph, algorithm, DummyClustering.provider(),
                new DummyContextSimilarity<>()
        );

        watset.run();

        final Map<String, Map<Sense<String>, Map<String, Number>>> inventory = watset.getInventory();

        try {
            write(application.output, inventory);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void write(Path path, Map<String, Map<Sense<String>, Map<String, Number>>> inventory) throws IOException {
        try (final BufferedWriter writer = Files.newBufferedWriter(path)) {
            for (final Map.Entry<String, Map<Sense<String>, Map<String, Number>>> wordEntry : inventory.entrySet()) {
                final String word = wordEntry.getKey();
                int i = 0;

                for (final Map.Entry<Sense<String>, Map<String, Number>> senseEntry : wordEntry.getValue().entrySet()) {
                    final String context = senseEntry.getValue().entrySet().stream().
                            map(e -> String.format("%s:%f", e.getKey(), e.getValue().doubleValue())).
                            collect(joining(","));
                    writer.write(String.format(Locale.ROOT, "%s\t%d\t%s\n", word, i++, context));
                }
            }
        }
    }
}

@@ -889,16 +889,60 @@
 Watset;%0A
+import org.nlpub.watset.sense.IndexedSense;%0A
 import o
@@ -2814,35 +2814,8 @@
 y();
-%0A                int i = 0;
 %0A%0A  
@@ -2925,32 +2925,132 @@
 ).entrySet()) %7B%0A
+                    final IndexedSense%3CString%3E sense = (IndexedSense%3CString%3E) senseEntry.getKey();%0A%0A
                 
@@ -3358,11 +3358,24 @@
 rd, 
-i++
+sense.getSense()
 , co

Use the real sense identifiers