package it.unitn.disi.annotation.pipelines;

import it.unitn.disi.annotation.pipelines.components.TagCounter;
import it.unitn.disi.nlptools.components.PipelineComponentException;
import org.apache.log4j.Logger;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

/**
 * Reports tag counts.
 *
 * @author <a rel="author" href="http://autayeu.com/">Aliaksandr Autayeu</a>
 */
public class TagCounterContextPipeline extends BaseContextPipeline {

    private static final Logger log = Logger.getLogger(TagCounterContextPipeline.class);

    @Override
    public void beforeProcessing() throws PipelineComponentException {
        TagCounter.getTagCounts().clear();
        super.beforeProcessing();
    }

    @Override
    public void afterProcessing() throws PipelineComponentException {
        super.afterProcessing();
        Map<String, Long> tagCounts = TagCounter.getTagCounts();
        //sort by counts and print
        MapValueComparator<String, Long> mvc = new MapValueComparator<String, Long>(tagCounts);
        TreeMap<String, Long> sortedMap = new TreeMap<String, Long>(mvc);
        sortedMap.putAll(tagCounts);

        for (Map.Entry<String, Long> e : sortedMap.entrySet()) {
            log.info(e.getKey() + "\t" + e.getValue());
        }
    }

    private static class MapValueComparator<A, B extends Comparable<? super B>> implements Comparator<A> {

        private Map<A, B> base;

        public MapValueComparator(Map<A, B> base) {
            this.base = base;
        }

        public int compare(A a, A b) {
            return base.get(b).compareTo(base.get(a));
        }
    }
}

@@ -232,68 +232,9 @@
 til.
-Comparator;%0D%0Aimport java.util.Map;%0D%0Aimport java.util.TreeMap
+*
 ;%0D%0A%0D
@@ -1020,233 +1020,203 @@
     
-TreeMap%3CString, Long%3E sortedMap = new TreeMap%3CString, Long%3E(mvc);%0D%0A        sortedMap.putAll(tagCounts);%0D%0A%0D%0A        for (Map.Entry%3CString, Long%3E e : sortedMap.entrySet()) %7B%0D%0A            log.info(e.getKey() + %22%5Ct%22 + e.getValue(
+ArrayList%3CString%3E tags = new ArrayList%3CString%3E(tagCounts.keySet());%0D%0A        Collections.sort(tags, mvc);%0D%0A%0D%0A        for (String tag : tags) %7B%0D%0A            log.info(tag + %22%5Ct%22 + tagCounts.get(tag
 ));%0D

fix for tags with same counts