clear variables

filePath = 'D:\Users\Tom\Dropbox\';

% This file is kind of long, but all it is going to do is replace the
% candidate names at the top of the pollbypoll results with the party
% that candidate belongs to


%%%% Set constants

header = {'province' 'district' 'id' 'candname' 'party' 'votes'};

partyList = {'Liberal' 'Conservative' 'NDP' 'Independent' 'Green Party' 'CHP' ...
    'Marxist-Leninist' 'Bloc Q' 'Rhinoceros' 'Communist' 'Pirate Party' 'CAP' ...
    'No Affiliation' 'United Party' 'Libertarian' 'Animal Alliance/Environment Voters' ...
    'Radical Marijuana' 'PCParty' 'FPNP' 'WBP' 'N.D.P.' 'Marijuana Party' ...
    'Christian Heritage' 'Canadian Action' 'PC Party' 'AACEV Party' ...
    'NL First Party' 'neorhino.ca' 'AAEV Party of Canada' 'PPP' ...
    'Work Less Party'}';

newPartyList = partyList;

%%%% Read in data

rawNames = cell(3,1);
[~,~, rawNames{1}] = xlsread(strcat(filePath,'table12master.xlsx'),'2006');
[~,~, rawNames{2}] = xlsread(strcat(filePath,'table12master.xlsx'),'2008');
[~,~, rawNames{3}] = xlsread(strcat(filePath,'table12master.xlsx'),'2011');

%%%% Loop through each line of the raw candidate name lists to associate
%%%% a unique party with each candidate

partyNameLoc = cell(3,1);
candidateName = cell(3,1);
partyName = cell(3,1);
outCandList = cell(3,1);
for m=1:3
    rawNames{m}(1,:) = [];
    partyNameLoc{m} = zeros(size(rawNames{m},1),1);
    if m == 1
       pointer = 3;
    else
       pointer = 4;
    end
    
    for n=1:size(partyList,1)
        test = cellfun('size',strfind(rawNames{m}(:,pointer), partyList{n}),2)>0;
        foundParty = cell2mat(strfind(rawNames{m}(:,pointer), partyList{n}));
        if size(foundParty,2)
            partyNameLoc{m}(test) = foundParty(:,1);
        end
    end
    
    candidateName{m} = cell(size(partyNameLoc,1),1);
    partyName{m} = cell(size(partyNameLoc,1),1);
    for k=1:size(rawNames{m})
        candidateName{m}{k} = rawNames{m}{k,pointer}(1:partyNameLoc{m}(k)-2);
        partyName{m}{k} = rawNames{m}{k,pointer}(partyNameLoc{m}(k):end);
    end
    
    candidateName{m} = strrep(candidateName{m},' **','');   
    partyName{m} = strtok(partyName{m},'/');
    
    partyName{m} = strrepbatch(partyName{m},{'N.D.P.','NDP';...
        'NDP-New Democratic Party','NDP'},0);
    partyName{m} = strrepbatch(partyName{m},{'Bloc Québécois','Bloc';...
        'NDP-New Democratic Party','NDP'},0);
    
    if m==1
        outCandList{m} = [rawNames{m}(:,1:2) cell(length(rawNames{1}),1) candidateName{m} partyName{m} rawNames{m}(:,6)];
    else
        outCandList{m} = [rawNames{m}(:,1:3) candidateName{m} partyName{m} rawNames{m}(:,7)];
    end
end

%%%% Output district level data

cell2csv(strcat(filePath,'my2006table12.csv'),[header; outCandList{1}]);
cell2csv(strcat(filePath,'my2008table12.csv'),[header; outCandList{2}]);
cell2csv(strcat(filePath,'my2011table12.csv'),[header; outCandList{3}]);

% if I want to do something about special independents, it should be here

[~, files{1}] = xlsread(strcat(filePath,'code\','filelist2006.csv'));
[~, files{2}] = xlsread(strcat(filePath,'code\','filelist2008.csv'));
[~, files{3}] = xlsread(strcat(filePath,'code\','filelist2011.csv'));

%%%% Find district ID's

districtID = cell(3,1);
for n=1:3
    districtID{n} = cell2mat(strrepbatch(files{n},{'pollbypoll_bureauparbureau', ''; ...
        '.csv', ''},1));
end

%%%% Replace the candidate names with their party affiliation. This looks
%%%% complicated because one party affiliation (Independent) can occur more
%%%% than once in the same district

yearPointer = [2006 2008 2011];
for m=1:3
    for n=1:length(files{m})
        disp(n)     
        tempFile = strcat(filePath,num2str(yearPointer(m)),'\',files{m}(n));
        [~, ~, tempText] = xlsread(tempFile{1});
        
        if m==1
            tempText(1,:) = strrepbatch(tempText(1,:),outCandList{m}(strcmp(strtok(outCandList{m}(:,2),'/'),{strtok(tempText{2,1},'/')}),4:5),1);
            tempText(2:end,3) = strrep(tempText(2:end,3),',','');                      
        else
            tempText(1,:) = strrepbatch(tempText(1,:),outCandList{m}(strcmp(strtok(outCandList{m}(:,2),'/'),{strtok(tempText{2,2},'/')}),4:5),1);
            tempText(2:end,4) = strrep(tempText(2:end,4),',','');
            tempText([false; cellfun('size',tempText(2:end,5),2)>20],5) = {''};
        end
        
        if size(unique(tempText(1,:)),2)<size(tempText(1,:),2)
            testIndep = find(cellfun('size',strfind(tempText(1,:),'Independent'),2)>0);
            for p=2:size(testIndep,2)
                tempText(1,testIndep(p)) = strcat(tempText(1,testIndep(p)),num2str(p));
            end
        end
        
        tempFile = strcat(filePath,num2str(yearPointer(m)),'out\',files{m}(n));
        cell2csv(tempFile{1},tempText);
    end
end