file = fopen('learningdata4.txt');
queryExpr = '# ....';
splitLineExpr = '\ ';
prevQueryId = '';

tline = fgetl(file); 
queryId = regexp(tline, queryExpr, 'match');

net = initnnetwork();
counter=0;
while(~feof(file))
    prevQueryId = queryId;   
    k=1;    
    docsCells = {};
    counter=counter+1;
    while (strcmp(prevQueryId,queryId))        
        features = regexp(tline, splitLineExpr, 'split');
        featuresList = repmat(0,[1,246]); %initialize array with zeros
        featuresList(246) = str2double(cast(features(1), 'char'));
        for i=2:(length(features)-2)
            tmpStr = cast(features(i), 'char');
            tmpCell = regexp(tmpStr, '\:', 'split');
            index = str2double(cast(tmpCell(1,1), 'char'));
           
            valueChar = cast(tmpCell(1,2), 'char');
            value = str2double(valueChar);
            featuresList(index) = value;
        end;       
        docsCells(k,1)= {featuresList}; %#ok<AGROW>
        k=k+1;
        if(~feof(file))
            tline = fgetl(file); 
            queryId = regexp(tline, queryExpr, 'match');
        else
            break;
        end;
    end;
    %place where all documents for given query were retrieved from txt file
    docsMat = cell2mat(docsCells);
    %sort feature rows by 246 column(relevance level) in reverse order
    sortedData = sortrows(docsMat, -246);
    
    %remove column with relevance levels
    sortedData(:,246)=[];

    %' - транспонирование 7x245 -> 245x7
    completedData = sortedData';
    net.performParam = completedData;
    net = train(net,completedData);
end;
fclose(file);
disp(counter);
%сохранение обученной нейронной сети
save savednet net





