package org.oscii.morph;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.oscii.lex.Lexicon;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Apply neural morphology rules across multiple languages
 */
public class MorphologyManager {

    private final static Logger logger = LogManager.getLogger(MorphologyManager.class);
    private final static Gson gson = new Gson();

    private final Map<String, Stemmer> stemmers = new HashMap<>();
    private final Lexicon lexicon;

    public MorphologyManager(Lexicon lexicon) {
        this.lexicon = lexicon;
    }

    public void add(String lang, String path, List<String> embeddingVocab) throws FileNotFoundException {
        if (stemmers.containsKey(lang)) {
            logger.error("Repeat language: {}", lang);
        }
        logger.info("Loading morphology rules for {} from {}.", lang, path);
        JsonReader reader = new JsonReader(new FileReader(new File(path)));
        Substitutor subber = gson.fromJson(reader, SubstituteMain.class);
        logger.info("Indexing morphological transformations for {}.", lang);
        Stemmer stemmer = new Stemmer(subber, embeddingVocab, lexicon, lang);
        logger.info("Done.");
        stemmers.put(lang, stemmer);
    }

    /**
     * Return the stem in the lexicon that is the most likely match to the query.
     *
     * @param query  text (raw)
     * @param language
     * @return stem or original query if no stem is found
     */
    public String getKnownStem(String query, String language) {
        Stemmer stemmer = stemmers.get(language);
        List<String> stems = stemmer.getKnownStems(query);
        if (stems.isEmpty()) {
            return query;
        } else {
            return stems.get(0);
        }
    }
}

@@ -44,24 +44,66 @@
 .gson.Gson;%0A
+import com.google.gson.reflect.TypeToken;%0A
 import com.g
@@ -329,24 +329,83 @@
 FileReader;%0A
+import java.lang.reflect.Type;%0Aimport java.util.ArrayList;%0A
 import java.
@@ -1259,16 +1259,100 @@
 ath)));%0A
+        Type listOfRuleScored = new TypeToken%3CArrayList%3CRuleScored%3E%3E()%7B%7D.getType();%0A
         
@@ -1398,28 +1398,24 @@
 er, 
-SubstituteMain.class
+listOfRuleScored
 );%0A 

fix type of gson