% readsenselabstats.m
% loads a node matrix with names
% a weight matrix w(i,j) records the connections clicks; node i, then node j

% read in the data file

% fid = fopen('c:\docume~1\Administrator\desktop\senselabstats1', 'rt');
fid = fopen('c:\docume~1\Administrator\desktop\senselabstats2', 'rt');
fid = fopen('c:\docume~1\morse\desktop\matlabsenselab\senselabstats', 'rt');
literal='->';
y = 0;
c=cell(1,1000);
nodes=cell(1,1000);
i=1;
numofnodes=0;
top_links=cell(1,50); 
top_links_flag=0; % turn true while doing top links
l_i=1;  %   top_links index

while feof(fid) == 0
    tline = fgetl(fid);
    %  disp(tline);
    matches = findstr(tline, literal);
    num = length(matches);
    if num > 0
        y = y + num;
        disp(['There are ' int2str(num) ' matches in ' tline]);
    end
    test_top_links= findstr(tline, 'TOP Links');
    if length(test_top_links)
        top_links_flag=1;
        tline = fgetl(fid);
        tline = fgetl(fid); % reading two lines get's the first top links
    end

    test_top_paths= findstr(tline, 'TOP PATHS');
    if length(test_top_paths)
        top_links_flag=0; % reset to zero when over
    end

    if top_links_flag
        top_links(l_i)=cellstr(tline);
        l_i=l_i+1;
    end

end

num_of_top_links=l_i-3;

if num==1
    c(i)=cellstr(tline);

    x=strfind(char(c(i)),'PATH');
    if x
        y=char(c(i));
        z=strfind(char(c(i)),'->');
        q=strfind(char(c(i)),'---');
        if z
            testnode=y(x+5:z-1);
            othertestnode=y(z+2:q-1);
            newnode=1;
            for nodecntr=1:numofnodes
                 if findstr(char(node(nodecntr)),testnode)
                    newnode=0;
                 end
            end
            if newnode
              numofnodes=numofnodes+1;
              node(numofnodes)=cellstr(testnode);
            end
    
            newnode=1;
            for nodecntr=1:numofnodes
                if findstr(char(node(nodecntr)),othertestnode)
                    newnode=0;
                    end
                end
                if newnode
                  numofnodes=numofnodes+1;
                  node(numofnodes)=cellstr(othertestnode);
                end
            end
        end
        i=i+1;
    end
end
fclose(fid);

num_of_lines=i-1;

% go through the nodes again, this time assign the weight coefficient

w=zeros(numofnodes, numofnodes);

for i=1:num_of_lines
    x=strfind(char(c(i)),'PATH');
    if x
       y=char(c(i));
        z=strfind(char(c(i)),'->');
        q=strfind(char(c(i)),'---');
       if z
            testnode=y(x+5:z-1);
            othertestnode=y(z+2:q-1);

            startnode=0;    % first index for weight coeff
            for nodecntr=1:numofnodes
                if findstr(char(node(nodecntr)),testnode)
                    startnode=nodecntr;
                end
            end

            endnode=0;
            for nodecntr=1:numofnodes
                if findstr(char(node(nodecntr)),othertestnode)
                    endnode=nodecntr;
                end
            end

            if startnode*endnode>0
                d=char(c(i));
                w(startnode,endnode)=eval(d(q+4:end));
            else
                disp(['can not find node(s): ' testnode ',' othertestnode]);
                break    
            end
        end
    end
end
link_names=cell(1,num_of_top_links);
link_counts=zeros(1,num_of_top_links);

for i=1:num_of_top_links
    x=strfind(char(top_links(i)),'--:');
    if x
       y=char(top_links(i));
       z=strfind(char(top_links(i)),'---:');
       q=strfind(char(top_links(i)),'--(');
       if z
            link_names(i)=cellstr(y(x+3:z-1));
            link_counts(i)=eval(y(z+4:q-1));
        end
    end
end
