package com.formulasearchengine.mathosphere.mlp;

import com.google.common.collect.Multiset;
import com.formulasearchengine.mathosphere.mlp.cli.CliParams;
import com.formulasearchengine.mathosphere.mlp.cli.ListCommandConfig;
import com.formulasearchengine.mathosphere.mlp.cli.MlpCommandConfig;
import com.formulasearchengine.mathosphere.mlp.contracts.CreateCandidatesMapper;
import com.formulasearchengine.mathosphere.mlp.contracts.TextAnnotatorMapper;
import com.formulasearchengine.mathosphere.mlp.pojos.ParsedWikiDocument;
import com.formulasearchengine.mathosphere.mlp.pojos.RawWikiDocument;
import com.formulasearchengine.mathosphere.mlp.pojos.Relation;
import com.formulasearchengine.mathosphere.mlp.pojos.WikiDocumentOutput;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.List;

public class RelationExtractor {

  public static void main(String[] args) throws Exception {
    String params[] = {"extract", "-in", "C:/tmp/mlp/linear_regression.txt"};
    MlpCommandConfig config = CliParams.from(params).getExtractCommandConfig();
    run(config);
  }

  public static void run(MlpCommandConfig config) throws Exception {
    WikiDocumentOutput output = getWikiDocumentOutput(config);

    try (PrintWriter pw = createPrinter(config)) {
      List<Relation> relations = output.getRelations();

      for (Relation r : relations) {
        pw.println(r.getIdentifier() + "\t" + r.getDefinition() + "\t" + r.getScore());
      }
      pw.flush();
    }
  }

  private static WikiDocumentOutput getWikiDocumentOutput(MlpCommandConfig config) throws Exception {
    TextAnnotatorMapper annotator = new TextAnnotatorMapper(config);
    annotator.open(null);

    String filePath = config.getInput();
    String text = FileUtils.readFileToString(new File(filePath), "UTF-8");
    RawWikiDocument doc = new RawWikiDocument(filePath, 0, text);
    ParsedWikiDocument parsedDocument = annotator.map(doc);

    CreateCandidatesMapper mlp = new CreateCandidatesMapper(config);
    return mlp.map(parsedDocument);
  }

  public static PrintWriter createPrinter(MlpCommandConfig config) throws FileNotFoundException {
    if (StringUtils.isNotBlank(config.getOutput())) {
      return new PrintWriter(new File(config.getOutput()));
    }

    return new PrintWriter(System.out);
  }

  public static void list(ListCommandConfig config) {
    try {
      PrintWriter pw = createPrinter(config);
      WikiDocumentOutput output = getWikiDocumentOutput(config);
      for (Multiset.Entry<String> stringEntry : output.getIdentifiers()) {
        pw.println(stringEntry.getElement());
      }
      pw.flush();
    } catch (Exception e) {
      e.printStackTrace();
    }

  }
}

@@ -86,16 +86,17 @@
 ltiset;%0A
+%0A
 import c
@@ -725,16 +725,100 @@
 tOutput;
+%0A%0Aimport org.apache.commons.csv.CSVFormat;%0Aimport org.apache.commons.csv.CSVPrinter;
 %0Aimport 
@@ -1521,16 +1521,97 @@
 ions();%0A
+      CSVPrinter printer = CSVFormat.DEFAULT.withRecordSeparator(%22%5Cn%22).print(pw);
 %0A      f
@@ -1648,27 +1648,35 @@
         
-pw.println(
+String%5B%5D record = %7B
 r.getIde
@@ -1684,25 +1684,17 @@
 tifier()
- + %22%5Ct%22 +
+,
  r.getDe
@@ -1707,40 +1707,109 @@
 on()
- + %22%5Ct%22 + r.getScore());%0A      %7D
+, Double.toString(r.getScore())%7D;%0A        printer.printRecord(record);%0A      %7D%0A      printer.flush();
 %0A   

Write proper CSV file output