function [wordnet, word, wordnet_ADE20K, correctedNames] = wn(word, ADDFLAG, wordnet_ADE20K, correctedNames)
%
% [wordnet, word] = wn(word);
%
% [wordnet, word] = wn(word, ADDFLAG);
% Set ADDFLAG to 1 if you want to add new terms to the database.
% Set ADDFLAG to 2 if you want to revise existing terms to the database.
% (it adds them if they do not exist).

% index = load ('ADE20K_2015/index_ade20k_2015')
% [~,j] = sort(index.objectcounts, 'descend');
% for i = 505:1000; disp(i); wn(index.objectnames{j(i)}, 1); end

changed1 = 0;
changed2 = 0;

%load wordnet_ADE20K;
if nargin<3
    load ('wordnet_ADE20K.mat', 'wordnet_ADE20K', 'correctedNames')
end

if nargin <2
    ADDFLAG = 0;
end

% If there are no input arguments, show the full list of words inside the
% dictionary:
if nargin==0
    wordnet = keys(wordnet_ADE20K);
    if nargout == 0
        disp('List of words in the dictionary')
        disp(wordnet')
    end
    return
end

word = strrep(word, '  ',' ');
word = strtrim(word);
word = lower(word);

% Correct the word (spelling errors, ...):
if ADDFLAG ~=2 && isKey(correctedNames, word)
    word = correctedNames(word);
else
    if ADDFLAG ~= 0
        [correctedNames, changed1] = addNameToDictionary(correctedNames, word);
        word = correctedNames(word);        
    end
end

% Find word in wordnet. 
if ADDFLAG ~=2 && isKey(wordnet_ADE20K, word) 
    wordnet = wordnet_ADE20K(word);
else
    if ADDFLAG ~= 0
        [wordnet_ADE20K, changed2] = addToWordnet(wordnet_ADE20K, word);
        if isKey(wordnet_ADE20K, word)
            wordnet = wordnet_ADE20K(word);
        else
            warning(['"' word '" not in the dictionary.'])
            wordnet = '';
        end
    else
        warning(['"' word '" not in the dictionary.'])
        wordnet = '';
    end
    % want to add it?
    % [wordnet, word] = wn(word, 'add')
    % it will add the word if it is not present.
    % [word, found(i)] = getWordnetSense(word);
    % and then select synset

end

if nargout == 0
    disp([word ' =>'])
    disp(wordnet)
end

%if nargout == 1
%    wordnet = keys(wordnet_ADE20K);
%end


if nargout<3 && ADDFLAG==1
    disp('Saving...')
    file = which('wordnet_ADE20K.mat');
    if changed1==1 && changed2==1
        save (file, 'correctedNames', 'wordnet_ADE20K', '-append')
        disp('Saved. Updated "correctedNames" in wordnet_ADE20K.mat')
        disp('Saved. Updated "wordnet_ADE20K" in wordnet_ADE20K.mat')
    end
    if changed1==1 && changed2==0
        save (file, 'correctedNames', '-append')
        disp('Saved. Updated "correctedNames" in wordnet_ADE20K.mat')
    end
    if changed1==0 && changed2==1
        save (file, 'wordnet_ADE20K', '-append')
        disp('Saved. Updated "wordnet_ADE20K" in wordnet_ADE20K.mat')
    end
end


function [correctedNames, changed] = addNameToDictionary(correctedNames, word)

%file = which('wordnet_ADE20K.mat');
changed = 0;

disp('Word not found in "correctedNames". You can add it now.')
disp(['Current word: ' word]);
newword = input('Write corrected word (return if already correct): ', 's');

newword = strrep(newword, '  ',' ');
newword = strtrim(newword);
newword = lower(newword);

if isempty(newword)
    newword = word;
    disp('word entered as already correct')
else
    disp('word corrected')
end

correctedNames(word) = newword;
changed = 1;



function [wordnet_ADE20K, changed] = addToWordnet(wordnet_ADE20K, word)

%file = which('wordnet_ADE20K.mat');
changed = 0;

disp(['Word: ' word])
[wordn, found] = getWordnetSense(word);


if found    
    % show the N cases and query the corresponding one
    select = [];
    N=0;
    for w = 1:length(wordn)
        for n = 1:length(wordn(w).branch)
            N = N+1;
            disp('-------------------')
            disp(sprintf('Sense %d)', N));
            disp(['Synonyms: ' wordn(w).synonyms{n}])
            disp(['Hyper:    ' wordn(w).sense{n}])
            %disp(word(1).branch{n})
            disp(['Gloss:    ' wordn(w).sentences{n}])
            select(N,:) = [w n];
        end
    end
    
    disp('................')
    str = input('Select sense (return for none): ');
    
    if isempty(str)
        disp('* Marked as not found')
    else
        w = select(str,1);
        n = select(str,2);
        clear wordnet
        wordnet.synset = lower(wordn(w).sense{n});
        wordnet.hypernym = lower(wordn(w).branch{n});
        wordnet.gloss = lower(wordn(w).sentences{n});
        wordnet.synonyms = lower(wordn(w).synonyms{n});
        wordnet.frequency = wordn(w).frequency(n);
        disp('')
        disp('* Assigned sense:')
        disp(wordnet.synset)
        wordnet_ADE20K(word) = wordnet;
        wordnet_ADE20K(wordnet.synonyms) = wordnet;
        %save (file, 'wordnet_ADE20K', '-append')
        %disp('Saved updated "wordnet_ADE20K" in wordnet_ADE20K.mat')
        changed = 1;
    end
    
else
    disp('* Word not found in wordnet!')
end





        
        