
function [nr, from_line, to_line, from_projection, to_projection] = find_nearest_match(model, sentence_nr, from_lang, to_lang, n)

   
    if nargin<3
        from_lang = 'est';
    end
    
    if nargin<4
        to_lang = 'eng';
    end
    
    if nargin<5
        n = 1;
    end


    if (strcmp(from_lang, 'est'))
        from_projections = model.est_projections;
        from_lines = model.lines_est1;
    else
        from_projections = model.eng_projections;
        from_lines = model.lines_eng1;
    end
        
    
    from_projection = from_projections(:, sentence_nr);
    
    
    from_sentence_line_nr = model.vector_nr_to_line_nr.get(sentence_nr);
    
    from_line = from_lines(from_sentence_line_nr, 1);
    from_line = char(from_line{1});
    
    if (strcmp(to_lang, 'est'))
        to_projections = model.est_projections;
        to_lines = model.lines_est1;
    else
        to_projections = model.eng_projections;
        to_lines = model.lines_eng1;
    end
    
    
    
    num_projections = cols(to_projections);
    
    
    best_i = zeros(1, n);
    best_matches = ones(1, n) * Inf;
    worst_best_match = Inf;
    
    for i = 1:num_projections
       
        to_projection = to_projections(:, i);
        match = sum(abs(to_projection - from_projection));
        
        if (match < worst_best_match)
            
            % worst_best_match = match;
            
            best_matches(1, 1) = match;
            [best_matches, IX] = sort(best_matches, 'descend');   % esimene sissekanne maatriksis on kehvim
            
            worst_best_match = max(best_matches);
            
            
            best_i(1, 1) = i;
            
            % best_i = i;
            best_i2 = zeros(1, n);
            for j = 1:n
               
                src = IX(j);
                best_i2(j) = best_i(src); 
                
            end
            
            best_i = best_i2;
            
        end     % if (match < worst_best_match)
        
    end     % for i = 1:num_projections
    

    nr = best_i;
    if n == 1
        to_projection = to_projections(:, best_i);
        
        
        to_line_nr = model.vector_nr_to_line_nr.get(best_i);
        
        to_line = to_lines(to_line_nr, 1);
        to_line = char(to_line{1});
    end

end
