
function corpus_out = readcorpus(filename, max_rows, filename2, skip_rows) 


    if nargin<2
        max_rows = -1;
    end
    
    if nargin<3
        filename2 = '';
    end
    
    if nargin<4
        skip_rows = 0;
    end


    
    
    if isempty(filename2)
       
        out_est = [filename '.est'];
        out_eng = [filename '.eng'];
        
        fid_est = fopen(out_est, 'w');
        fid_eng = fopen(out_eng, 'w');
        
    else
        
        out_est = '';
        out_eng = '';
        
    end     % if isempty(filename2)
    
    
    
    
    corpus_out = struct;
    
    corpus_out.est = [];
    corpus_out.eng = [];


    % fid = fopen(filename);
    
    % file = textscan(fid, '%s', 'MultipleDelimsAsOne', '1', 'Delimiter', '\n', 'TreatAsEmpty', '<eesti>', 'TreatAsEmpty', '</eesti>', 'TreatAsEmpty', '<inglise>', 'TreatAsEmpty', '</inglise>');
    % file = textscan(fid, '%s', 'Delimiter', '\n');
    
    % fclose(fid);
    
    
    % corpus = file(1);
    
    
    corpus = textread(filename, '%s', 'delimiter', '\n');
    
    if ~isempty(filename2)
        
        corpus2 = textread(filename2, '%s', 'delimiter', '\n');
        
        len = rows(corpus) * 2;
        skip_rows = skip_rows * 2;
        if max_rows ~= -1
            len = min(max_rows * 2, len);
        end  
        
    else        % if ~isempty(filename2)
        
        corpus2 = {};
        
        len = rows(corpus);
        skip_rows = skip_rows * 2;
        if max_rows ~= -1
            len = min(max_rows * 2, len);
        end
        
    end        % if ~isempty(filename2)
    
    
    
    
    
    
    corpus_out.est = cell(floor((len - skip_rows) / 2), 1);
    corpus_out.eng = cell(floor((len - skip_rows) / 2), 1);
    
    out_i = 1;
    for i = 1+skip_rows : len
       
        if isempty(corpus2)
        
            row = cell2mat(corpus(i, :));

        else 
        
            if (mod(i, 2) == 1)
                row = cell2mat(corpus(ceil(i / 2), :));
            else
                row = cell2mat(corpus2(ceil(i / 2), :));
            end
            
        end
        
        
        
        mlen = match_beginning(row, '<eesti>');
        if mlen > 0
            row = row(1, mlen+1 : cols(row));
        else
            mlen = match_beginning(row, '<inglise>');
            if mlen > 0
                row = row(1, mlen+1 : cols(row));
            end
        end
        
        
        rowlen = cols(row);
        mlen = match_end(row, '</eesti>');
        if mlen > 0
            row = row(1, 1 : rowlen - mlen);
        else
            mlen = match_end(row, '</inglise>');
            if mlen > 0
                row = row(1, 1 : rowlen - mlen);
            end
        end       
            
        
        
        row = lower(row);
        
        
        % if (cols(row) > 0)    
        %     corpus_out(out_i, :) = {row};
        %     out_i = out_i + 1;
        % end
        
        if (mod(i, 2) == 1)     % eesti
            
            if cols(row) > 0
                corpus_out.est(out_i, :) = {row};
            end
                
            
            if ~isempty(out_est)
               
                % fwrite(fid_est, row);
                % fwrite(fid_est, '\n');
                fprintf(fid_est, '%s\n', row);
                
            end
            
        else     % if (mod(i, 2) == 1)
            
            if cols(row) > 0
                corpus_out.eng(out_i, :) = {row};
            end
            
            out_i = out_i + 1;
            
            
            if ~isempty(out_eng)
               
                % fwrite(fid_eng, row);
                % fwrite(fid_eng, '\n');
                fprintf(fid_eng, '%s\n', row);
                
            end
            
        end     % if (mod(i, 2) == 1)
        
        
    end     % for i = 1:len
    
    
    fclose(fid_est);
    fclose(fid_eng);
    
    

end
