summaryrefslogtreecommitdiff
path: root/matcher/matcher.pl
blob: 49cd56712919d4a2078d486fc809c86bb5c37cec (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/usr/bin/perl

#
#    Copyright (C) 2009-2010  Yuki Manabe and Daniel M. German
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

#
# matchter.pl
#
# This script use a set of license sentence name as input 
# and output license name corresponds to a rule which match the set.
#
# author: Yuki Manabe
#
# usage: matchter.pl (inputfilename)
#

use strict;
my $debug = 0;

my %NonCriticalRules ;

# these should go into a file, but for the time being, let us keep them here

# once we have matched a rule, these are not that important

my @generalNonCritical = ('AllRights');

my @gplNonCritical = ('GPLnoVersion',
                      'FSFwarranty',
                      'LibraryGPLcopyVer0',
                      'GPLseeVer0',
                      'GPLwrite',
                      'SeeFile',
                      'FreeSoftware',
                      'FSFwarrantyVer0',
                      'LibraryGPLseeDetailsVer0',
                      'FSFwarranty',
                      'LesserGPLseeDetailsVer0',
                      'GPLcopyVer0',
                      'GNUurl',
                      'GPLseeDetailsVer0');

$NonCriticalRules{"LibraryGPLv3+"} = [@gplNonCritical];
$NonCriticalRules{"LibraryGPLv3"} = [@gplNonCritical];
$NonCriticalRules{"LibraryGPLv2+"} = [@gplNonCritical];
$NonCriticalRules{"LibraryGPLv2"} = [@gplNonCritical];
$NonCriticalRules{"LesserGPLv3"} = [@gplNonCritical, 'LesserGPLseeVer3','LesserGPLcopyVer3','SeeFileVer3'];
$NonCriticalRules{"LesserGPLv2.1+"} = [@gplNonCritical];
$NonCriticalRules{"LesserGPLv2.1"} = [@gplNonCritical];
$NonCriticalRules{"LGPLv2orv3"}= [@gplNonCritical];
$NonCriticalRules{"LesserGPLv2"} = [@gplNonCritical];
$NonCriticalRules{"LesserGPLv2+"} = [@gplNonCritical];
$NonCriticalRules{"GPLVer2.1or3KDE+"} = [@gplNonCritical];
$NonCriticalRules{"LGPLVer2.1or3KDE+"} = [@gplNonCritical];


$NonCriticalRules{"GPLv2+"} = [@gplNonCritical];
$NonCriticalRules{"GPLv2"} = [@gplNonCritical];
$NonCriticalRules{"GPLv1+"} = [@gplNonCritical];
$NonCriticalRules{"GPLv1"} = [@gplNonCritical];
$NonCriticalRules{"GPLv3+"} = [@gplNonCritical];
$NonCriticalRules{"GPLv3"} = [@gplNonCritical];
$NonCriticalRules{"AGPLv3"} = [@gplNonCritical, 'AGPLreceivedVer0','AGPLseeVer0'];
$NonCriticalRules{"AGPLv3+"} = [@gplNonCritical, 'AGPLreceivedVer0','AGPLseeVer0'];
$NonCriticalRules{"GPLnoVersion"} = [@gplNonCritical];

$NonCriticalRules{"Apache-1.1"} = ['ApacheLic1_1'];
$NonCriticalRules{"Apache-2"}   = ['ApachePre','ApacheSee'];

$NonCriticalRules{"LibGCJLic"}  = ['LibGCJSee'];
$NonCriticalRules{"CDDLicV1"}  = ['Compliance','CDDLicWhere','ApachesPermLim','CDDLicIncludeFile','UseSubjectToTerm', 'useOnlyInCompliance'];
$NonCriticalRules{"CDDLic"}  = ['Compliance','CDDLicWhere','ApachesPermLim','CDDLicIncludeFile','UseSubjectToTerm', 'useOnlyInCompliance'];

$NonCriticalRules{"CDDLorGPLv2"}= ["CDDLorGPLv2doNotAlter","AllRights","useOnlyInCompliance", "CDDLorGPLv2whereVer0", "ApachesPermLim", "CDDLorGPLv2include","CDDLorGPLv2IfApplicable", "CDDLorGPLv2Portions", "CDDLorGPLv2ifYouWishVer2", "CDDLorGPLv2IfYouAddVer2"];

$NonCriticalRules{"CPLv1orGPLv2+orLGPLv2+"} = ["licenseBlockBegin", "licenseBlockEnd"];

$NonCriticalRules{"Qt"} = ["Copyright","qtNokiaExtra","QTNokiaContact", "qtDiaTems"];
$NonCriticalRules{"orLGPLVer2.1"} = ["LesserqtReviewGPLVer2.1","qtLGPLv2.1where"];
$NonCriticalRules{"orGPLv3"} = ["qtReviewGPLVer3.0","qtReviewGPLVer3","qtGPLwhere"];
$NonCriticalRules{"digiaQTExceptionNoticeVer1.1"} = ["qtDigiaExtra"];

$NonCriticalRules{"MPLv1_0"}  = ['ApacheLicWherePart1','MPLwarranty','MPLSee'];
$NonCriticalRules{"MPLv1_1"}  = ['ApacheLicWherePart1','MPLwarranty','MPLSee'];
$NonCriticalRules{"NPLv1_1"}  = ['ApacheLicWherePart1','MPLwarranty','MPLSee'];
$NonCriticalRules{"NPLv1_0"}  = ['ApacheLicWherePart1','MPLwarranty','MPLSee'];

$NonCriticalRules{"subversion"} = ['SeeFileSVN','subversionHistory'];
$NonCriticalRules{"subversion+"} = ['SeeFileSVN','subversionHistory'];
$NonCriticalRules{"tmate+"} = ['SeeFileSVN'];

$NonCriticalRules{"openSSLvar2"} = ['BSDcondAdvPart2'];

$NonCriticalRules{"MPLv1_1"} = ['licenseBlockBegin','MPLsee','Copyright','licenseBlockEnd','ApacheLicWherePart1','MPLwarranty', 'MPLwarrantyVar'];
$NonCriticalRules{"MPL1_1andLGPLv2_1"} = ["MPLoptionIfNotDelete2licsVer0",'MPL_LGPLseeVer0'];

$NonCriticalRules{"FreeType"} = ["FreeTypeNotice"];
$NonCriticalRules{"boostV1"} = ["boostSeev1", "SeeFile"];

# initialize

my $path = $0;
$path =~ s/[^\/]+$//;
if ($path eq "") {
    $path = "./";
  }

my $rules= $path . "rules.dict";
my $interrules= $path . "interrules.dict";

die "Usage $0 <filename>.senttok" unless $ARGV[0] =~ /\.senttok$/;


# read rules

my $countUnknowns = 0;


# read the rules

my @rulelist = Read_Rules($rules);

my @interRuleList = Read_Inter_Rules($interrules);


my @licSentNames=();
my @original;

Read_Original($ARGV[0], \@licSentNames, \@original);


#foreach my $x (@licSentNames) {
#    print "$x\n";
#}
#exit;

#foreach my $x (@original) {
#    print "$x\n";
#}
#exit;

##########################################

#for my $ref( @interRuleList ){
#   print "@$ref\n";
#}

# matching spdx requires to match strict licenses, with no alternatives...

my $senttok= "," . join(",",@licSentNames) . ",";
my @result=();
my $countMatches = 0;

print "[$senttok]\n" if $debug;
Match_License();

# do we have to check again?
## todo, verifythat we have unmatched sentences...

@licSentNames = split(',', $senttok);

# first remove the extrict part from it

#Print_Result();

my $match = 0;
for (my $i=0;$i<=$#licSentNames ;$i++) {
    if ($licSentNames[$i] == 0 and 
        ($licSentNames[$i] ne "UNKNOWN"  and
         $licSentNames[$i] ne "")) {
#        print "[$licSentNames[$i]]\n";
        $licSentNames[$i] =~ s/Extrict$//;
        $match ++;
    }
}

#Print_Result();


if ($match > 0) {
#    print "REDO\n";
    for (my $i=0;$i<=$#interRuleList ;$i++){
        #for my $ref( @interRuleList[$i]){
        #  print "@$ref\n";
        #}
        #print $interRuleList[$i][0];
        @licSentNames = map { $_ eq $interRuleList[$i][0] ? $interRuleList[$i][1] : $_ } @licSentNames;
    }
    
    $senttok= join(",",@licSentNames) . ',';
    
    Match_License();
}

Print_Result();


exit 0;



#print @licSentNames;
#print join(";",@licSentNames)."\n";


# 3. matching
###############################

# we will iterate over rules, matching as many as we can...





sub Is_Unknown
{
    my ($s) = @_;
    my @f = split (/,/, $s);
    return $f[0] eq "UNKNOWN";
}


sub Read_Rules
{
    my ($rulesF) = @_;
    open (RULES, "<$rulesF") or die ("Error: rules.dict is not found.");
    my $sentence;
    my @rules = ();
    while ($sentence=<RULES>){
        chomp $sentence;
	# clean up spaces
	$sentence=~ s/^\s+//;
	$sentence=~ s/\s+$//;
	$sentence=~ s/\s*,\s*/,/g;
	$sentence=~ s/\s*:\s*/:/g;
        #check format
        if ($sentence =~ /^#/ || $sentence !~ /(.*):(.*,)*(.*)/){
            next;
        }
        $sentence =~ /(.*?):(.*)/;
        push (@rules,[$1,$2]);
    }
    close RULES;
    return @rules;
}


sub Read_Inter_Rules
{
    my ($interrules) = @_;

    my @list;
    open (IRULES, "<$interrules") or die ("Error: interrules.dict is not found.");
    my $sentence;
    while ($sentence=<IRULES>){
        chomp $sentence;
        #check format
        if ($sentence =~ /^#/ || $sentence !~ /(.*?):(.*)/){
            next;
        }
        foreach my $item (split(/\|/,$2)){
            push (@list,[$item,$1]);
        }
    }
    close IRULES;
    return @list;
}

sub Read_Original
{
    my ($inputF, $tokens, $originals) = @_;

    open (INPUTFILE, $inputF) or die ("Error: $inputF is not found.");
    
    my $sentence;
    my @original;
    while ($sentence = <INPUTFILE>){
        chomp $sentence;
        my @fields = split(':',$sentence);
        push(@$originals,$fields[1]);
        my @token = split(';', $fields[0]);
        push(@$tokens,$token[0]);
    }
    if (scalar(@$originals) == 0) {
        print "NONE\n";
        exit 0;
    }
    
#print join(";",@licSentNames)."\n";
    
    close INPUTFILE;
}

sub Match_License
{
    
# create a string with the sentences
    
    for (my $j=0;$j<=$#rulelist;$j++){
        
        my $rule=$rulelist[$j][1];
        my $rulename=$rulelist[$j][0];
        my $lenRule = scalar(split(',', $rule));
        # replace rule with the length of the rule
	print "To try [$rulename][$rule] on [$senttok]\n" if $debug;
        while ($senttok =~ s/,${rule},/,$lenRule,/){
            $countMatches ++;
            push (@result,$rulename);
#        print ">>>>$senttok|$rulelist[$j][1]\n";
#        print "Result: ", join(',', @result);
#        print "\n";
        }
    }
    
#    print ">>>>[$senttok]\n";

    my $onlyAllRight = 0;

# ok, at this point we have removed all the matched sentences...
#print STDERR "Ending>>>>>>>$senttok\n";
#print STDERR "Size>>" , scalar(@result), "\n";
#print STDERR "Result>>", join(',', @result), "\n";
    
# let us remove allrights
#    my $onlyAllRight = 1;
#    for my $i (0.. scalar(@licSentNames)-1){
#        if (($licSentNames[$i] eq "AllRights")) {
#            $licSentNames[$i] = '';
#        } else {
#            $onlyAllRight = 0;
#        }
#    }

# output result
    if (scalar(@result) > 0){
        # at this point we have matched
        
        
        # let us clean up the rules... let us print the matched rules, and the 
#    if (grep(/GPL/, @result)) {
#        print "GPL...\n";
#        foreach my $r ($NonCriticalRules{GPL}) {
#            $senttok =~ s/(,|^)$r(,|$)/$1$2/g;
#        }
#    }
        # general removal of rules
        

        foreach my $r (@generalNonCritical) {
            while ($senttok =~ s/,$r,/,-1,/) {
                ;
            }
        }
#        print "[$senttok]\n";
                
        foreach my $res (@result) {
            my $temp = $NonCriticalRules{$res};
            foreach my $r (@$temp) {
#            print ">>Senttok [$r][$senttok]\n";
                while ($senttok =~ s/,$r,/,-1,/g) {
                    ;
                }
            }
        }
#        print "[$senttok]\n";        
    }
}


sub Print_Result
{
#        $senttok =~ s/AllRights(,?)/$1/g;
#        $senttok =~ s/UNKNOWN,/,/g;
#        $senttok =~ s/,+/,/g;

    my $save = $senttok;
    # ok, so now, what I want to output it:
    # licenses; number of licenses matched;number of sentences matched; number of sentences ignored;number of sentences not matched;number of sentences unknown
    my @sections = split(',', $senttok);
    die "assertion 1" if $sections[0] ne "";
    die "assertion 2" if $sections[scalar(@sections)] ne "";
    
    my $ignoredLines = 0;
    my $licenseLines = 0;
    my $unknownLines = 0;
    my $unmatchedLines = 0;
    foreach my $i (1..scalar(@sections)-1) {
#        print "$i;$sections[$i]\n";
        if ($sections[$i] < 0) {
            $ignoredLines += - $sections[$i];
        } elsif ($sections[$i] != 0) {
            $licenseLines += $sections[$i];
        } elsif ($sections[$i] eq "UNKNOWN") {
            $unknownLines ++;
        } else {
            $unmatchedLines++;
        }
    }
    $senttok =~ s/^,(.*),$/$1/;
    
#    print "$ignoredLines > $licenseLines > $unknownLines > $unmatchedLines\n";
    if (scalar (@result) == 0) {
	print "UNKNOWN";
    } else {
	print join(',',@result);
    }
    print ";$countMatches;$licenseLines;$ignoredLines;$unmatchedLines;$unknownLines;$senttok\n";
    $senttok = $save;
    
}