// Path: src/deplambda/others/SentenceKeys.java
// public class SentenceKeys extends in.sivareddy.util.SentenceKeys {
//   public static String DEPLAMBDA_OBLIQUE_TREE = "deplambda_oblique_tree";
//   public static String DEPLAMBDA_EXPRESSION = "deplambda_expression";
// }
// 
// Path: src/deplambda/util/Sentence.java
// public class Sentence {
//   private List<DependencyTree> nodes;
//   private DependencyTree rootNode;
//   private Map<Integer, JsonObject> wordIndexToEntity;
//   private JsonArray words;
// 
//   /**
//    * Constructs {@link Sentence} from json object.
//    * 
//    * @param sentence
//    */
//   public Sentence(JsonObject sentence) {
//     wordIndexToEntity = new HashMap<>();
//     nodes = new ArrayList<>();
//     buildWordToEntityMap(sentence, wordIndexToEntity);
//     Preconditions.checkArgument(sentence.has("words"),
//         "Sentence should have the key 'words'");
//     words = sentence.get("words").getAsJsonArray();
//     rootNode = DependencyTree.parse(words, wordIndexToEntity, nodes);
//   }
// 
//   public JsonObject getEntityAtWordIndex(int index) {
//     return wordIndexToEntity.containsKey(index) ? wordIndexToEntity.get(index)
//         : null;
//   }
// 
//   /**
//    * Constructs a map from word index to entity object.
//    * 
//    * @param sentence
//    * @param wordIndexToEntity
//    */
//   private static void buildWordToEntityMap(JsonObject sentence,
//       Map<Integer, JsonObject> wordIndexToEntity) {
//     if (sentence.has(SentenceKeys.ENTITIES)) {
//       JsonArray entities = sentence.get(SentenceKeys.ENTITIES).getAsJsonArray();
//       for (JsonElement entityElement : entities) {
//         JsonObject entity = entityElement.getAsJsonObject();
// 
//         Preconditions.checkArgument(
//             entity.has(SentenceKeys.ENTITY_INDEX)
//                 || (entity.has(SentenceKeys.START) && entity
//                     .has(SentenceKeys.END)),
//             "An entity should have an index, or a start and an end.");
// 
//         if (!entity.has(SentenceKeys.START)) {
//           int entityIndex = entity.get(SentenceKeys.ENTITY_INDEX).getAsInt();
//           entity.addProperty(SentenceKeys.START, entityIndex);
//           entity.addProperty(SentenceKeys.END, entityIndex);
//         }
// 
//         int start = entity.get(SentenceKeys.START).getAsInt();
//         int end = entity.get(SentenceKeys.END).getAsInt();
// 
//         if (!entity.has(SentenceKeys.ENTITY_INDEX)) {
//           // The last word is the index of the whole entity.
//           entity.addProperty(SentenceKeys.ENTITY_INDEX, end);
//         }
// 
//         while (start <= end) {
//           wordIndexToEntity.put(start, entity);
//           start++;
//         }
//       }
//     }
//   }
// 
//   public JsonArray getWords() {
//     return words;
//   }
// 
//   public DependencyTree getRootNode() {
//     return rootNode;
//   }
// 
//   public String getLemma(int index) {
//     JsonObject word = getWords().get(index).getAsJsonObject();
//     return word.has(SentenceKeys.LEMMA_KEY)
//         && !word.get(SentenceKeys.LEMMA_KEY).getAsString().equals("_") ? word
//         .get(SentenceKeys.LEMMA_KEY).getAsString().toLowerCase() : word
//         .get(SentenceKeys.WORD_KEY).getAsString().toLowerCase();
//   }
// }
// 
// Path: src/deplambda/util/TransformationRuleGroups.java
// public class TransformationRuleGroups {
//   private List<TransformationRuleGroup> ruleGroupList;
//   private List<Relation> relationList;
// 
//   private Map<String, TregexPattern> tregexCache;
//   TregexPatternCompiler patternCompiler;
//   private Map<String, Integer> relationPriority;
// 
//   public TransformationRuleGroups(String ruleFile)
//       throws FileNotFoundException, IOException {
//     patternCompiler = new TregexPatternCompiler();
//     RuleGroups.Builder rulesBuilder = RuleGroups.newBuilder();
//     TextFormat.merge(new FileReader(ruleFile), rulesBuilder);
//     RuleGroups ruleGroups = rulesBuilder.build();
// 
//     ruleGroupList = new ArrayList<>();
//     for (RuleGroup ruleGroup : ruleGroups.getRulegroupList()) {
//       TransformationRuleGroup transformationRuleGroup =
//           new TransformationRuleGroup(ruleGroup);
//       ruleGroupList.add(transformationRuleGroup);
//     }
//     
//     // Sort the groups based on the priority.
//     ruleGroupList.sort(Comparator.comparing(r -> r.getPriority()));
// 
//     // Create relation priority map.
//     relationPriority = new HashMap<>();
//     relationList = new ArrayList<>(ruleGroups.getRelationList());
//     relationList.sort(Comparator.comparing(r -> r.getPriority()));
// 
//     relationList.forEach(relation -> relationPriority.put(relation.getName(),
//         relation.getPriority()));
//   }
// 
//   public TregexPattern getTregex(String pattern) {
//     tregexCache.putIfAbsent(pattern, patternCompiler.compile(pattern));
//     return tregexCache.get(pattern);
//   }
// 
//   public List<TransformationRuleGroup> getRuleGroupList() {
//     return ruleGroupList;
//   }
// 
//   public Map<String, Integer> getRelationPriority() {
//     return relationPriority;
//   }
// }


import java.util.Collections;
import java.util.List;
import org.apache.log4j.Logger;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import deplambda.others.SentenceKeys;
import deplambda.util.Sentence;
import deplambda.util.TransformationRuleGroups;
import edu.cornell.cs.nlp.spf.mr.lambda.LogicalExpression;
import edu.cornell.cs.nlp.utils.composites.Pair;
import in.sivareddy.util.ProcessStreamInterface;
package deplambda.parser;





public class TreeTransformerMain extends ProcessStreamInterface {

  private static Gson gson = new Gson();
  private static JsonParser jsonParser = new JsonParser();

// Path: src/deplambda/others/SentenceKeys.java
// public class SentenceKeys extends in.sivareddy.util.SentenceKeys {
//   public static String DEPLAMBDA_OBLIQUE_TREE = "deplambda_oblique_tree";
//   public static String DEPLAMBDA_EXPRESSION = "deplambda_expression";
// }
// 
// Path: src/deplambda/util/Sentence.java
// public class Sentence {
//   private List<DependencyTree> nodes;
//   private DependencyTree rootNode;
//   private Map<Integer, JsonObject> wordIndexToEntity;
//   private JsonArray words;
// 
//   /**
//    * Constructs {@link Sentence} from json object.
//    * 
//    * @param sentence
//    */
//   public Sentence(JsonObject sentence) {
//     wordIndexToEntity = new HashMap<>();
//     nodes = new ArrayList<>();
//     buildWordToEntityMap(sentence, wordIndexToEntity);
//     Preconditions.checkArgument(sentence.has("words"),
//         "Sentence should have the key 'words'");
//     words = sentence.get("words").getAsJsonArray();
//     rootNode = DependencyTree.parse(words, wordIndexToEntity, nodes);
//   }
// 
//   public JsonObject getEntityAtWordIndex(int index) {
//     return wordIndexToEntity.containsKey(index) ? wordIndexToEntity.get(index)
//         : null;
//   }
// 
//   /**
//    * Constructs a map from word index to entity object.
//    * 
//    * @param sentence
//    * @param wordIndexToEntity
//    */
//   private static void buildWordToEntityMap(JsonObject sentence,
//       Map<Integer, JsonObject> wordIndexToEntity) {
//     if (sentence.has(SentenceKeys.ENTITIES)) {
//       JsonArray entities = sentence.get(SentenceKeys.ENTITIES).getAsJsonArray();
//       for (JsonElement entityElement : entities) {
//         JsonObject entity = entityElement.getAsJsonObject();
// 
//         Preconditions.checkArgument(
//             entity.has(SentenceKeys.ENTITY_INDEX)
//                 || (entity.has(SentenceKeys.START) && entity
//                     .has(SentenceKeys.END)),
//             "An entity should have an index, or a start and an end.");
// 
//         if (!entity.has(SentenceKeys.START)) {
//           int entityIndex = entity.get(SentenceKeys.ENTITY_INDEX).getAsInt();
//           entity.addProperty(SentenceKeys.START, entityIndex);
//           entity.addProperty(SentenceKeys.END, entityIndex);
//         }
// 
//         int start = entity.get(SentenceKeys.START).getAsInt();
//         int end = entity.get(SentenceKeys.END).getAsInt();
// 
//         if (!entity.has(SentenceKeys.ENTITY_INDEX)) {
//           // The last word is the index of the whole entity.
//           entity.addProperty(SentenceKeys.ENTITY_INDEX, end);
//         }
// 
//         while (start <= end) {
//           wordIndexToEntity.put(start, entity);
//           start++;
//         }
//       }
//     }
//   }
// 
//   public JsonArray getWords() {
//     return words;
//   }
// 
//   public DependencyTree getRootNode() {
//     return rootNode;
//   }
// 
//   public String getLemma(int index) {
//     JsonObject word = getWords().get(index).getAsJsonObject();
//     return word.has(SentenceKeys.LEMMA_KEY)
//         && !word.get(SentenceKeys.LEMMA_KEY).getAsString().equals("_") ? word
//         .get(SentenceKeys.LEMMA_KEY).getAsString().toLowerCase() : word
//         .get(SentenceKeys.WORD_KEY).getAsString().toLowerCase();
//   }
// }
// 
// Path: src/deplambda/util/TransformationRuleGroups.java
// public class TransformationRuleGroups {
//   private List<TransformationRuleGroup> ruleGroupList;
//   private List<Relation> relationList;
// 
//   private Map<String, TregexPattern> tregexCache;
//   TregexPatternCompiler patternCompiler;
//   private Map<String, Integer> relationPriority;
// 
//   public TransformationRuleGroups(String ruleFile)
//       throws FileNotFoundException, IOException {
//     patternCompiler = new TregexPatternCompiler();
//     RuleGroups.Builder rulesBuilder = RuleGroups.newBuilder();
//     TextFormat.merge(new FileReader(ruleFile), rulesBuilder);
//     RuleGroups ruleGroups = rulesBuilder.build();
// 
//     ruleGroupList = new ArrayList<>();
//     for (RuleGroup ruleGroup : ruleGroups.getRulegroupList()) {
//       TransformationRuleGroup transformationRuleGroup =
//           new TransformationRuleGroup(ruleGroup);
//       ruleGroupList.add(transformationRuleGroup);
//     }
//     
//     // Sort the groups based on the priority.
//     ruleGroupList.sort(Comparator.comparing(r -> r.getPriority()));
// 
//     // Create relation priority map.
//     relationPriority = new HashMap<>();
//     relationList = new ArrayList<>(ruleGroups.getRelationList());
//     relationList.sort(Comparator.comparing(r -> r.getPriority()));
// 
//     relationList.forEach(relation -> relationPriority.put(relation.getName(),
//         relation.getPriority()));
//   }
// 
//   public TregexPattern getTregex(String pattern) {
//     tregexCache.putIfAbsent(pattern, patternCompiler.compile(pattern));
//     return tregexCache.get(pattern);
//   }
// 
//   public List<TransformationRuleGroup> getRuleGroupList() {
//     return ruleGroupList;
//   }
// 
//   public Map<String, Integer> getRelationPriority() {
//     return relationPriority;
//   }
// }


// Path: src/deplambda/parser/TreeTransformerMain.java
import java.util.Collections;
import java.util.List;
import org.apache.log4j.Logger;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import deplambda.others.SentenceKeys;
import deplambda.util.Sentence;
import deplambda.util.TransformationRuleGroups;
import edu.cornell.cs.nlp.spf.mr.lambda.LogicalExpression;
import edu.cornell.cs.nlp.utils.composites.Pair;
import in.sivareddy.util.ProcessStreamInterface;

package deplambda.parser;





public class TreeTransformerMain extends ProcessStreamInterface {

  private static Gson gson = new Gson();
  private static JsonParser jsonParser = new JsonParser();

  private final TransformationRuleGroups treeTransformationRules;