// Path: src/edu/stanford/nlp/semparse/open/core/eval/CandidateStatistics.java
// public class CandidateStatistics {
//   public final Candidate candidate;
//   public final int rank, uniqueRank;    // rank and uniqueRank are 1-indexed
//   public final double score;
//   
//   public CandidateStatistics(Candidate candidate, int rank, int uniqueRank, double score) {
//     this.candidate = candidate;
//     this.rank = rank;
//     this.uniqueRank = uniqueRank;
//     this.score = score;
//   }
//   
//   /**
//    * Convert Pair<Candidate, Double> to CandidateStatistics
//    */
//   public static List<CandidateStatistics> getRankedCandidateStats(List<Pair<Candidate, Double>> rankedCandidates) {
//     List<CandidateStatistics> answer = new ArrayList<>();
//     Set<List<String>> foundPredictedEntities = new HashSet<>();
//     for (int rank = 0; rank < rankedCandidates.size(); rank++) {
//       Pair<Candidate, Double> entry = rankedCandidates.get(rank);
//       Candidate candidate = entry.getFirst();
//       foundPredictedEntities.add(candidate.predictedEntities);
//       answer.add(new CandidateStatistics(candidate, rank + 1, foundPredictedEntities.size(), entry.getSecond()));
//     }
//     return answer;
//   }
// }
// 
// Path: src/edu/stanford/nlp/semparse/open/model/candidate/Candidate.java
// public class Candidate {
//   public final Example ex;
//   public final CandidateGroup group;
//   public final TreePattern pattern;
//   public final List<String> predictedEntities;
//   public FeatureVector features;
//   
//   public Candidate(CandidateGroup group, TreePattern pattern) {
//     this.pattern = pattern;
//     this.group = group;
//     group.candidates.add(this);
//     // Perform shallow copy
//     this.ex = group.ex;
//     this.predictedEntities = group.predictedEntities;
//   }
//   
//   public int numEntities() {
//     return group.numEntities();
//   }
//   
//   public double getReward() {
//     return group.ex.expectedAnswer.reward(this);
//   }
//   
//   public Map<String, Double> getCombinedFeatures() {
//     Map<String, Double> map = new HashMap<>();
//     features.increment(1, map);
//     group.features.increment(1, map);
//     return map;
//   }
//   
//   // ============================================================
//   // Debug Print
//   // ============================================================
//   
//   public String sampleEntities() {
//     return group.sampleEntities();
//   }
//   
//   public String allEntities() {
//     return group.allEntities();
//   }
// }


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import edu.stanford.nlp.semparse.open.core.eval.CandidateStatistics;
import edu.stanford.nlp.semparse.open.model.candidate.Candidate;
import fig.basic.LogInfo;
package edu.stanford.nlp.semparse.open.core;



public class InteractiveDemo {
  
  public final OpenSemanticParser parser;
  
  public InteractiveDemo(OpenSemanticParser parser) {
    this.parser = parser;
  }

  public void run() {
    LogInfo.log("Starting interactive mode ...");
    try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in))) {
      while (true) {
        System.out.println("============================================================");
        System.out.print("Query: list of ");
        String phrase = in.readLine();
        if (phrase == null) {System.out.println(); break;}
        if (phrase.isEmpty()) continue;
        System.out.print("Web Page URL (blank for Google Search): ");
        String url = in.readLine();
        if (url == null) {System.out.println(); break;}
        CandidateStatistics pred = url.isEmpty() ? parser.predict(phrase) : parser.predict(phrase, url);
        LogInfo.begin_track("PRED (top scoring candidate):");
        if (pred == null) {
          LogInfo.log("Rank 1 [Unique Rank 1]: NO CANDIDATE FOUND!");
        } else {
          LogInfo.logs("Rank 1 [Unique Rank 1]: (Total Feature Score = %s)", pred.score);
// Path: src/edu/stanford/nlp/semparse/open/core/eval/CandidateStatistics.java
// public class CandidateStatistics {
//   public final Candidate candidate;
//   public final int rank, uniqueRank;    // rank and uniqueRank are 1-indexed
//   public final double score;
//   
//   public CandidateStatistics(Candidate candidate, int rank, int uniqueRank, double score) {
//     this.candidate = candidate;
//     this.rank = rank;
//     this.uniqueRank = uniqueRank;
//     this.score = score;
//   }
//   
//   /**
//    * Convert Pair<Candidate, Double> to CandidateStatistics
//    */
//   public static List<CandidateStatistics> getRankedCandidateStats(List<Pair<Candidate, Double>> rankedCandidates) {
//     List<CandidateStatistics> answer = new ArrayList<>();
//     Set<List<String>> foundPredictedEntities = new HashSet<>();
//     for (int rank = 0; rank < rankedCandidates.size(); rank++) {
//       Pair<Candidate, Double> entry = rankedCandidates.get(rank);
//       Candidate candidate = entry.getFirst();
//       foundPredictedEntities.add(candidate.predictedEntities);
//       answer.add(new CandidateStatistics(candidate, rank + 1, foundPredictedEntities.size(), entry.getSecond()));
//     }
//     return answer;
//   }
// }
// 
// Path: src/edu/stanford/nlp/semparse/open/model/candidate/Candidate.java
// public class Candidate {
//   public final Example ex;
//   public final CandidateGroup group;
//   public final TreePattern pattern;
//   public final List<String> predictedEntities;
//   public FeatureVector features;
//   
//   public Candidate(CandidateGroup group, TreePattern pattern) {
//     this.pattern = pattern;
//     this.group = group;
//     group.candidates.add(this);
//     // Perform shallow copy
//     this.ex = group.ex;
//     this.predictedEntities = group.predictedEntities;
//   }
//   
//   public int numEntities() {
//     return group.numEntities();
//   }
//   
//   public double getReward() {
//     return group.ex.expectedAnswer.reward(this);
//   }
//   
//   public Map<String, Double> getCombinedFeatures() {
//     Map<String, Double> map = new HashMap<>();
//     features.increment(1, map);
//     group.features.increment(1, map);
//     return map;
//   }
//   
//   // ============================================================
//   // Debug Print
//   // ============================================================
//   
//   public String sampleEntities() {
//     return group.sampleEntities();
//   }
//   
//   public String allEntities() {
//     return group.allEntities();
//   }
// }


// Path: src/edu/stanford/nlp/semparse/open/core/InteractiveDemo.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import edu.stanford.nlp.semparse.open.core.eval.CandidateStatistics;
import edu.stanford.nlp.semparse.open.model.candidate.Candidate;
import fig.basic.LogInfo;

package edu.stanford.nlp.semparse.open.core;



public class InteractiveDemo {
  
  public final OpenSemanticParser parser;
  
  public InteractiveDemo(OpenSemanticParser parser) {
    this.parser = parser;
  }

  public void run() {
    LogInfo.log("Starting interactive mode ...");
    try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in))) {
      while (true) {
        System.out.println("============================================================");
        System.out.print("Query: list of ");
        String phrase = in.readLine();
        if (phrase == null) {System.out.println(); break;}
        if (phrase.isEmpty()) continue;
        System.out.print("Web Page URL (blank for Google Search): ");
        String url = in.readLine();
        if (url == null) {System.out.println(); break;}
        CandidateStatistics pred = url.isEmpty() ? parser.predict(phrase) : parser.predict(phrase, url);
        LogInfo.begin_track("PRED (top scoring candidate):");
        if (pred == null) {
          LogInfo.log("Rank 1 [Unique Rank 1]: NO CANDIDATE FOUND!");
        } else {
          LogInfo.logs("Rank 1 [Unique Rank 1]: (Total Feature Score = %s)", pred.score);
          Candidate candidate = pred.candidate;