% Nacteni Toolboxu
% Vyzaduje nacteni toolboxu ADT, ktery lze stahnout z oficialnich stranek matlabu
% Taky vyzaduje stahnout databazi TimiT
% 
% Tento soubor nacte z databaze timit z ruznych regionu vety krom SA1 a SA2

sc = 10000;    % pocet vet na pohlavi z kazdeho regionu
dialect = {'dr1', 'dr2', 'dr3', 'dr4', 'dr5', 'dr6', 'dr7', 'dr8'}; % regiony
usage = 'test';
p = 1;

phone_tr_dict = wphl_timit();

disp('Nacitani dat');

W = [];
M = [];
F = [];

for i = 1:length(dialect)

    [w f m] = query(DB_TIMIT, 'sex', 'm', 'usage', usage, 'sentence', '~SA1', 'sentence', '~SA2', 'dialect', dialect(i), sc);
    
    ww =[];
    mm = [];
    ff = [];
    for j = 1:length(w)
        if rand() <= p
            ww = [ww; w(j)];
            mm = [mm; m(j)];
            ff = [ff; f];
        end
    end
    
    W = [W;ww];
    F = [F; repmat(f, [length(ww) 1])];
    M = [M; mm];
    
    [w f m] = query(DB_TIMIT, 'sex', 'f', 'usage', usage, 'sentence', '~SA1', 'sentence', '~SA2', 'dialect', dialect(i), sc);
    
    ww =[];
    mm = [];
    ff = [];
    for j = 1:length(w)
        if rand() <= p
            ww = [ww; w(j)];
            mm = [mm; m(j)];
            ff = [ff; f];
        end
    end
    
    W = [W;ww];
    F = [F; repmat(f, [length(ww) 1])];
    M = [M; mm];
    
end

data = cell(6,length(W));

for i = 1:length(W)
    
    data{1,i} = W{i};
    data{2,i} = M{i}.word.name(1:size(M{i}.word.name,1)-1,:);
    word_phonemes = cell(size(M{i}.word.name,1)-1, 1);
    
    for j = 1:(size(M{i}.word.name,1)-1)

        word = strrep(M{i}.word.name(j,:), ' ', '');
        word_phonemes{j, 1} = find_word(phone_tr_dict, word);
        
    end
    
    data{3,i} = word_phonemes;
    data{4,i} = M{i}.word.b;
    data{5,i} = M{i}.word.e;
    data{6,i} = F(i);
    
end

    
sentences_save(strcat('sentences_', usage) ,data);

+