File size: 3,191 Bytes
d4035c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
function [D, unmatched, counts] = LMaddwordnet(D, sensesfile, method)
%
% [D, unmatched, counts] = LMaddwordnet(D, sensesfile, method)
%
% Adds synsets to object names. It adds the 'synset' field to
% the 'object' field.
% 
% sensesfile: name of the text file that contains the synonyms list.
% 
% Methods:
%  'concatenate' [default]: concatenates to object name the synset
%  'samename': does not modify the field 'name'. 
%  'synset': replaces the field 'name' with the synset
%
% This tool uses synsets provided by:
% WordNet 2.1 Copyright 2005 by Princeton University.
% http://wordnet.princeton.edu/

% load wordnet file. 
if nargin < 2
    sensesfile = 'wordnetsenses.txt';
end
if nargin < 3
    method = 'concatenate';
end

% load list of stop words
stopwords = removestopwords;

% load senses
if exist(sensesfile)
    fid = fopen(sensesfile);
    C= textscan(fid,'%s', 'delimiter', '\n');
    fclose(fid)
    C = C{1};
    %C = importdata(sensesfile);
    description = strtrim(C(1:4:end));
    synonims = C(2:4:end);    
    synset = C(3:4:end);
end

Nimages = length(D);

% Take labelme description and split it into words
unmatched = []; u = 0;
for i = 1:Nimages
    i
    if isfield(D(i).annotation, 'object')
        N = length(D(i).annotation.object);

        for j = 1:N
            name = D(i).annotation.object(j).name;

            ndx = findsynset(name, description, stopwords);
            if length(ndx)>0
                ndx = ndx(1);
                switch method
                    case 'concatenate'
                        wordnetname = [name '. ' synonims{ndx} '. ' synset{ndx}];
                    case 'synset'
                        % replaces the current name with the wordnet synset
                        wordnetname = [synonims{ndx} '. ' synset{ndx}];
                    otherwise
                        % default (
                        wordnetname = name;
                end
                D(i).annotation.object(j).synset = [synonims{ndx} '. ' synset{ndx}];
            else
                u = u+1;
                unmatched{u} = removestopwords(name, stopwords);
                wordnetname = name;
            end

            wordnetname = strrep(wordnetname, ',', '+');
            wordnetname = strrep(wordnetname, '.', '+');
            wordnetname = strrep(wordnetname, '-', '+');
            wordnetname = strrep(wordnetname, ' ', '+');
            wordnetname = strrep(wordnetname, '++', '+');
            wordnetname = strrep(wordnetname, '+', ' ');
            wordnetname = getwords(wordnetname);
            [foo, si] = unique(wordnetname);
            wordnetname = wordnetname(sort(si));
            wordnetname = strtrim(lower(sprintf('%s ', wordnetname{:})));

            D(i).annotation.object(j).name = wordnetname;
        end
    end
end

% sort unmatched descriptions by counts:
[unmatched, i, ndx] = unique(strtrim(lower(unmatched)));
Nobject = length(unmatched);
[counts, x] = hist(ndx, 1:Nobject);
[counts, j] = sort(-counts); counts = - counts;
unmatched = unmatched(j);




function j = findsynset(name, descriptions, stopwords)

name = removestopwords(name, stopwords);
j = strmatch(name, descriptions, 'exact');