package de.exciteproject.refext.train;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.apache.commons.io.FilenameUtils;

import de.exciteproject.refext.util.FileUtils;
import pl.edu.icm.cermine.exception.AnalysisException;

/**
 * Class for converting XML tagged references into BIO notation
 */
public class XmlToBioConverter {

    public static void main(String[] args) throws AnalysisException, IOException {
        File inputDir = new File(args[0]);
        File outputDir = new File(args[1]);
        if (!outputDir.exists()) {
            outputDir.mkdirs();
        }
        XmlToBioConverter xmlToBioConverter = new XmlToBioConverter();
        List<File> inputFiles = FileUtils.asList(inputDir);
        String inputDirectoryPath = FileUtils.getDirctory(inputDir).getAbsolutePath();

        for (File inputFile : inputFiles) {
            String inputFileSubPath = inputFile.getAbsolutePath().replaceAll(inputDirectoryPath, "");
            inputFileSubPath = FilenameUtils.removeExtension(inputFileSubPath).concat(".csv");

            File outputFile = new File(outputDir + inputFileSubPath);
            // if (outputFile.exists()) {
            // continue;
            // }

            List<String> annotatedText = xmlToBioConverter.annotateText(inputFile);
            Files.write(Paths.get(outputFile.getAbsolutePath()), annotatedText, Charset.defaultCharset());
        }
    }

    public List<String> annotateText(File inputFile) throws IOException, AnalysisException {
        Scanner s = new Scanner(inputFile);
        ArrayList<String> xmlAnnotatedLines = new ArrayList<String>();
        while (s.hasNextLine()) {
            xmlAnnotatedLines.add(s.nextLine());
        }
        s.close();

        List<String> bioAnnotatedLines = new ArrayList<String>();
        boolean insideRef = false;
        boolean insideOth = false;
        for (int i = 0; i < xmlAnnotatedLines.size(); i++) {
            String label = "";
            String line = xmlAnnotatedLines.get(i);
            if (line.contains("<ref>")) {
                label = "B-REF";
                insideRef = true;
                line = line.replaceFirst("<ref>", "");
            }
            if (line.contains("</ref")) {
                if (label.isEmpty()) {
                    label = "I-REF";
                }
                insideRef = false;
                line = line.replaceFirst("</ref>", "");
            }
            if (line.contains("<oth>")) {
                label = "O-REF";
                insideOth = true;
                line = line.replaceFirst("<oth>", "");
            }
            if (line.contains("</oth")) {
                if (label.isEmpty()) {
                    label = "O-REF";
                }
                insideOth = false;
                line = line.replaceFirst("</oth>", "");
            }
            if (label.isEmpty()) {
                if (insideOth) {
                    label = "O-REF";
                } else {
                    if (insideRef) {
                        label = "I-REF";
                    } else {
                        label = "O";
                    }
                }
            }
            bioAnnotatedLines.add(label + "\t" + line);

        }
        return bioAnnotatedLines;
    }
}

@@ -669,24 +669,169 @@
 e(args%5B1%5D);%0A
+        String bRefLabel = args%5B2%5D;%0A        String iRefLabel = args%5B3%5D;%0A        String oRefLabel = args%5B4%5D;%0A        String otherLabel = args%5B5%5D;%0A
         if (
@@ -963,16 +963,59 @@
 nverter(
+bRefLabel, iRefLabel, oRefLabel, otherLabel
 );%0A     
@@ -1758,24 +1758,403 @@
    %7D%0A    %7D%0A%0A
+    private String bRefLabel;%0A    private String iRefLabel;%0A    private String oRefLabel;%0A    private String otherLabel;%0A%0A    public XmlToBioConverter(String bRefLabel, String iRefLabel, String oRefLabel, String otherLabel) %7B%0A        this.bRefLabel = bRefLabel;%0A        this.iRefLabel = iRefLabel;%0A        this.oRefLabel = oRefLabel;%0A        this.otherLabel = otherLabel;%0A    %7D%0A%0A
     public L
@@ -2812,15 +2812,22 @@
 l = 
-%22B-REF%22
+this.bRefLabel
 ;%0A  
@@ -3028,39 +3028,46 @@
         label = 
-%22I-REF%22
+this.iRefLabel
 ;%0A              
@@ -3233,39 +3233,46 @@
         label = 
-%22O-REF%22
+this.oRefLabel
 ;%0A              
@@ -3461,39 +3461,46 @@
         label = 
-%22O-REF%22
+this.oRefLabel
 ;%0A              
@@ -3708,15 +3708,22 @@
 l = 
-%22O-REF%22
+this.oRefLabel
 ;%0A  
@@ -3818,15 +3818,22 @@
 l = 
-%22I-REF%22
+this.iRefLabel
 ;%0A  
@@ -3895,11 +3895,23 @@
 l = 
-%22O%22
+this.otherLabel
 ;%0A  

Add parameters for labels to XmlToBioConverter