package de.exciteproject.refext.extract;

import java.util.ArrayList;
import java.util.List;

import pl.edu.icm.cermine.tools.CharacterUtils;

/**
 * Class for extracting and merging references from a list of BIO tagged input
 * lines.
 */
public class ReferenceLineMerger {

    private final static String hyphenList = String.valueOf(CharacterUtils.DASH_CHARS).replaceAll("-", "") + "-";

    public static List<String> merge(List<String> inputLines) {
        List<String> references = new ArrayList<String>();

        String currentReference = null;
        for (String inputLine : inputLines) {
            String[] lineSplit = inputLine.split("\t");

            if (lineSplit[0].equals("O") || lineSplit[0].equals("B-REF")) {
                if (currentReference != null) {
                    references.add(currentReference);
                    currentReference = null;
                }
            }
            if (lineSplit[0].equals("B-REF")) {
                currentReference = lineSplit[1];
            }
            if (lineSplit[0].equals("I-REF")) {
                if (currentReference == null) {
                    continue;
                }
                if (lineSplit.length > 1) {
                    currentReference = ReferenceLineMerger.mergeLines(currentReference, lineSplit[1]);
                }
            }
        }
        if (currentReference != null) {
            references.add(currentReference);
        }
        return references;

    }

    /**
     * this approach on merging lines that end with a hyphen is based on
     * pl.edu.icm.cermine.bibref.KMeansBibReferenceExtractor
     * 
     * @param currentString
     * @param stringToAdd
     * @return
     */
    public static String mergeLines(String currentString, String stringToAdd) {
        if (currentString.matches(".*[a-zA-Z][" + hyphenList + "]")) {
            currentString = currentString.substring(0, currentString.length() - 1);
        } else {
            currentString += " ";
        }
        currentString += stringToAdd;
        return currentString;
    }
}

@@ -671,36 +671,8 @@
 if (
-lineSplit%5B0%5D.equals(%22O%22) %7C%7C 
 line
@@ -868,70 +868,8 @@
   %7D%0A
-            %7D%0A            if (lineSplit%5B0%5D.equals(%22B-REF%22)) %7B%0A
     
@@ -1539,17 +1539,16 @@
 r%0A     *
- 
 %0A     * 

Remove check for O label in ReferenceLineMerger