package org.oscii;

import org.oscii.lex.Expression;
import org.oscii.lex.Meaning;
import org.oscii.lex.Translation;

import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
 * A map from expressions to meanings.
 */
public class Lexicon {
    Map<Expression, List<Meaning>> lexicon = new HashMap<>();
    List<Meaning> empty = Collections.emptyList();

    // Logging
    private final static Logger logger = Logger.getLogger(Lexicon.class.getName());

    static {
        logger.setLevel(Level.INFO);
    }

    public void add(Meaning meaning) {
        if (!lexicon.containsKey(meaning.expression)) {
            lexicon.put(meaning.expression, new ArrayList<>());
        }
        lexicon.get(meaning.expression).add(meaning);
        logger.info(meaning.toString());
    }

    public List<Meaning> lookup(String query, String language) {
        // TODO(denero) Query using degraded text
        logger.info(String.format("Looking up %s %s", query, language));
        return lexicon.getOrDefault(new Expression(query, language), empty);
    }

    public List<Translation> translate(String query, String source, String target) {
        logger.info(String.format("Translating %s from %s to %s", query, source, target));
        return lookup(query, source).stream()
                // Aggregate and filter by target language
                .flatMap(m -> m.translations.stream()
                        .filter(t -> t.translation.language.equals(target)))
                        // Remove textual duplicates, choosing the first of each group
                .collect(Collectors.groupingBy((Translation t) -> t.translation.text))
                .values().stream().map(ts -> ts.iterator().next())
                .collect(Collectors.toList());
    }

    // A mock lexicon that always translates "adult" to "adulto".
    public static class Mock extends Lexicon {
        @Override
        public List<Meaning> lookup(String query, String language) {
            Expression adult = new Expression("adulto", "en");
            Expression adulto = new Expression("adulto", "es");
            return Arrays.asList(new Meaning(adult, Arrays.asList(adulto)));
        }
    }
}

@@ -135,73 +135,8 @@
 .*;%0A
-import java.util.logging.Level;%0Aimport java.util.logging.Logger;%0A
 impo
@@ -167,16 +167,16 @@
 ectors;%0A
+
 %0A/**%0A * 
@@ -356,165 +356,8 @@
 );%0A%0A
-    // Logging%0A    private final static Logger logger = Logger.getLogger(Lexicon.class.getName());%0A%0A    static %7B%0A        logger.setLevel(Level.INFO);%0A    %7D%0A%0A
     
@@ -579,49 +579,8 @@
 g);%0A
-        logger.info(meaning.toString());%0A
     
@@ -701,81 +701,8 @@
 ext%0A
-        logger.info(String.format(%22Looking up %25s %25s%22, query, language));%0A
     
@@ -773,32 +773,32 @@
  empty);%0A    %7D%0A%0A
+
     public List%3C
@@ -870,99 +870,8 @@
 ) %7B%0A
-        logger.info(String.format(%22Translating %25s from %25s to %25s%22, query, source, target));%0A
     

remove logging from Lexicon