%WSJ_LDC_POS_TAGGER 
%   WSJ_LDC_POS_TAGGER Performs POS tagging of WSJ section of PTB corpus,
%   using Latent-Descriptor Clustering (LDC) algorithm,
%   implemented in LDCLUSTERING.

%   Three main parameter settings are available: 
%   SETUP 1: Infer 50 labels and evaluate against PTB45 gold standard,
%       yielding MTO score (mean and std over 20 runs) of .7116(.0006)
%   SETUP 2: Infer 17 labels and evaluate against PTB17 gold standard,
%       yielding MTO score (mean and std over 20 runs) of .7507(.0004)
%   SETUP 3: Infer 45 labels and evaluate against PTB45 gold standard,
%       yielding MTO score (mean and std over 20 runs) of .6806(.0023)

%   Two additional parameter settings are available,
%   yielding better results under the OTO criterion
%   (if you insist on using OTO!):
%   SETUP 4: Infer 50 labels and evaluate against PTB45 gold standard,
%       yielding OTO score (mean and std over 20 runs) of .5095(.0022)
%   SETUP 5: Infer 17 labels and evaluate against PTB17 gold standard,
%       yielding OTO score (mean and std over 20 runs) of .5972(.0001)

%   The LDC algorithm is described in detail in:
%   Latent-Descriptor Clustering for Unsupervised POS Induction,
%   Michael Lamar, Yariv Maron, and Elie Bienenstock, EMNLP 2010.

%   The present code as well as LDCLUSTERING are available from
%   http://www.dam.brown.edu/people/elie/code/

%   Elie Bienenstock
%   Last modified October 4, 2010

clear
load PTB_WSJ_corpus_lowercase_noEOS corpus
%    loads the lowercased WSJ section of the PTB without End-Of-Sentence markers,
%       encoded by decreasing frequency:
%       most frequent word, encoded as 1, is ','
%       second most frequent word, encoded as 2, is 'the'
%       N_tokens = 1173766 
%       N_types  = 43766

setup = 2;  % SETUP = 1: % Infer 50 labels and evaluate against PTB45
            % SETUP = 2: % Infer 17 labels and evaluate against PTB17
            % SETUP = 3: % Infer 45 labels and evaluate against PTB45
            % SETUP = 4: % Infer 50 labels and evaluate against PTB45 (best under OTO)
            % SETUP = 5: % Infer 17 labels and evaluate against PTB17 (best under OTO)

switch setup
    
    case 1
        k = 50;
        sigma_schedule = .5*exp(-.22*(0:14));
        r_1 = 17;                   % reduced rank for iteration 1
        mce = 0;                    % do NOT use mixture coefficients
        load PTB_WSJ_TAGs45 tags    % gold-standard tagging (to be used for evaluation)
        gs = 'PTB45';               % name of gold standard
    case 2
        k = 17;
        sigma_schedule = .4*exp(-.18*(0:14));
        r_1 = 17;                   % reduced rank for iteration 1
        mce = 0;                    % do NOT use mixture coefficients
        load PTB_WSJ_TAGs17 tags    % gold-standard tagging (to be used for evaluation)        
        gs = 'PTB17';               % name of gold standard
    case 3
        k = 45;
        sigma_schedule = .49*exp(-.19*(0:17));
        r_1 = 18;                   % reduced rank for iteration 1
        mce = 0;                    % do NOT use mixture coefficients
        load PTB_WSJ_TAGs45 tags    % gold-standard tagging (to be used for evaluation)        
        gs = 'PTB45';               % name of gold standard
    case 4
        k = 50;
        sigma_schedule = [.55 .35];
        r_1 = 14;                   % reduced rank for iteration 1
        mce = 1;                    % use mixture coefficients
        load PTB_WSJ_TAGs45 tags    % gold-standard tagging (to be used for evaluation)        
        gs = 'PTB45';               % name of gold standard
    case 5
        k = 17;
        sigma_schedule = .3*exp(-.7*(0:13))+.1;
        r_1 = 17;                   % reduced rank for iteration 1
        mce = 1;                    % use mixture coefficients
        load PTB_WSJ_TAGs17 tags    % gold-standard tagging (to be used for evaluation)
        gs = 'PTB17';               % name of gold standard
        
end

[A, mto_score] = ldclustering(corpus, k, sigma_schedule, r_1, mce, tags, gs);
%                                   A is the clustering returned by the LDC algorithm, i.e.,
%                                   an assignment of each of the N_types word types
%                                   to one of the K labels
%                                   MTO_SCORE is the accuracy of the tagging of the corpus
%                                   under the Many-to-One mapping.

L_induced = A(corpus);              % induced labelling of corpus
