package eu.modernmt.io;

import eu.modernmt.model.corpus.BilingualCorpus;
import eu.modernmt.model.corpus.Corpus;
import org.apache.commons.io.IOUtils;

import java.io.IOException;

/**
 * Created by davide on 04/05/17.
 */
public class IOCorporaUtils {

    public static void copy(Corpus source, Corpus destination) throws IOException {
        copy(source, destination, false);
    }

    public static void copy(Corpus source, Corpus destination, boolean append) throws IOException {
        LineReader reader = null;
        LineWriter writer = null;

        try {
            reader = source.getContentReader();
            writer = destination.getContentWriter(append);

            String line;
            while ((line = reader.readLine()) != null)
                writer.writeLine(line);
        } finally {
            IOUtils.closeQuietly(reader);
            IOUtils.closeQuietly(writer);
        }
    }

    public static void copy(BilingualCorpus source, BilingualCorpus destination) throws IOException {
        copy(source, destination, false);
    }

    public static void copy(BilingualCorpus source, BilingualCorpus destination, boolean append) throws IOException {
        BilingualCorpus.BilingualLineReader reader = null;
        BilingualCorpus.BilingualLineWriter writer = null;

        try {
            reader = source.getContentReader();
            writer = destination.getContentWriter(append);

            BilingualCorpus.StringPair pair;
            while ((pair = reader.read()) != null)
                writer.write(pair);
        } finally {
            IOUtils.closeQuietly(reader);
            IOUtils.closeQuietly(writer);
        }
    }

}

@@ -256,136 +256,34 @@
     
-public static void copy(Corpus source, Corpus destination) throws IOException %7B%0A        copy(source, destination, false);%0A    %7D%0A
+// Monolingual corpus copy
 %0A   
@@ -333,32 +333,49 @@
 pus destination,
+ long linesLimit,
  boolean append)
@@ -578,33 +578,32 @@
 Writer(append);%0A
-%0A
             Stri
@@ -611,16 +611,44 @@
 g line;%0A
+            long lines = 0;%0A
         
@@ -684,32 +684,56 @@
 dLine()) != null
+ && lines++ %3C linesLimit
 )%0A              
@@ -911,41 +911,351 @@
 opy(
-BilingualCorpus source, Bilingual
+Corpus source, Corpus destination, long linesLimit) throws IOException %7B%0A        copy(source, destination, linesLimit, false);%0A    %7D%0A%0A    public static void copy(Corpus source, Corpus destination, boolean append) throws IOException %7B%0A        copy(source, destination, Long.MAX_VALUE, append);%0A    %7D%0A%0A    public static void copy(Corpus source, 
 Corp
@@ -1325,30 +1325,68 @@
 nation, 
-false);%0A    %7D%0A
+Long.MAX_VALUE);%0A    %7D%0A%0A    // Bilingual corpus copy
 %0A    pub
@@ -1450,32 +1450,49 @@
 pus destination,
+ long pairsLimit,
  boolean append)
@@ -1799,16 +1799,44 @@
 r pair;%0A
+            long pairs = 0;%0A
         
@@ -1876,16 +1876,40 @@
  != null
+ && pairs++ %3C pairsLimit
 )%0A      
@@ -2059,10 +2059,534 @@
 %0A    %7D%0A%0A
+    public static void copy(BilingualCorpus source, BilingualCorpus destination, long linesLimit) throws IOException %7B%0A        copy(source, destination, linesLimit, false);%0A    %7D%0A%0A    public static void copy(BilingualCorpus source, BilingualCorpus destination, boolean append) throws IOException %7B%0A        copy(source, destination, Long.MAX_VALUE, append);%0A    %7D%0A%0A    public static void copy(BilingualCorpus source, BilingualCorpus destination) throws IOException %7B%0A        copy(source, destination, Long.MAX_VALUE);%0A    %7D%0A%0A
 %7D%0A

copy with linelimit