summaryrefslogtreecommitdiff
path: root/senttok/senttok.pl
blob: 543f818066efe8d8c5816abc54615f5cbfb997a2 (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
#!/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/>.
#
use strict;


my $TOO_LONG = 70;

# where are we running the splitter from?
my $path = $0;
$path =~ s/[^\/]+$//;
if ($path eq "") {
    $path = "./";
}
my $licSentences = $path . "licensesentence.dict";

open FH, "<$ARGV[0]";
my @licensesentencelist=();
open LICENSESENTENCEFILE, "<$licSentences";
my $line;
while ($line = <LICENSESENTENCEFILE>){
    chomp $line;
    next if $line =~ /^\#/;
    next if $line =~ /^ *$/;
    die "Illegal format in license expression [$line] " unless $line =~ /(.*?):(.*?):(.*)/;
  push @licensesentencelist,$line;
}

#foreach $line (@licensesentencelist) {
#  print $line;
#}
close LICENSESENTENCEFILE;
while ($line = <>){
    my $saveLine;
    my $originalLine;
    chomp $line;
    $originalLine = $line;

    if ($line =~ s/^Alternatively,? ?//) {
        print "Altern\n";
    }

    $line = Normalize_Sentence($line);


    my $check=0;
    my $matchname="UNKNOWN";
    my @parm=();
    my $sentence;
    my $distance=1; #maximum? number
    my $mostsimilarname="UNKNOWN";
    my $before; 
    my $after;
    my $gpl = 0;
    my ($gplLater, $gplVersion);

    $saveLine = $line;

#        print "Original
#   [$line]
#\n";


    my $lineAsGPL ='';

    if (Looks_Like_GPL($line)) {
        my $old = $line;
        $gpl = 1;
        ($line, $gplLater, $gplVersion) = Normalize_GPL($line);
        $lineAsGPL = $line;
    }
    my ($name, $subRule, $number, $regexp, $option);
    my $saveLine = $line;
    my $saveGPL = $gpl;
    my $LGPL = "";
    foreach $sentence (@licensesentencelist) {
        ($name, $subRule, $number, $regexp, $option) = split(/:/, $sentence);
        # we need this due to the goto again
        $line = $saveLine;
        $gpl = $saveGPL;
        $LGPL = "";
      again:
#       print "Testing 
#   lin[$line]
#   ori[$saveLine]
#   re [$regexp]
#   lpg[$LGPL]
#\n";
        if ( $line =~ /$regexp/im ){
            $before = $`; 
            $after = $'; #';
            $check=1;
            $matchname=$name;
            for (my $i = 1; $i <= $number; $i++){
                no strict 'refs';
                push @parm,$$i;
            }
            last;
        } else{
#            print "NO MATCH\n";
            # let us try again in cas it is lesser/library
            # do it only once
            if ($gpl and $line =~ s/(Lesser|Library) GPL/GPL/i) {
                $LGPL = $1;
                goto again;
            }
            if ($gpl) {
                $gpl = 0;
                $line = $saveLine;
                goto again;
            }
            next;## dmg
            my $targetset=$regexp;
            $targetset =~ s/^(.*)$/$1/;
            my $tmpdist=&levenshtein($line,$targetset)/max(length($targetset),length($sentence));
            if ($tmpdist<$distance){
                $mostsimilarname=$name;
                $distance=$tmpdist;
            }
        }
        last; ###
    }
    if ($check == 1){
        #licensesentence name, parm1, parm2,..
        if ($gpl) {
            $matchname .= "Ver" . $gplVersion;
            $matchname .= "+" if $gplLater;
            $matchname = $LGPL . $matchname;
        } else {
        }
        if (length($before)>$TOO_LONG ||
            length($after) >$TOO_LONG) {
            $matchname .= "-TOOLONG";
        }
        my $parmstrings=join(";",$matchname, $subRule, $before, $after, @parm);
        print $parmstrings,":$originalLine\n";

        
    }else{
        #UNKNOWN, sentence
        chomp $line;
        print $matchname,";",0, ";", $mostsimilarname,";",$distance,";",$saveLine,":$originalLine\n";
    } 
    
}

close FH;
exit 0;

sub Normalize_GPL
{
    my ($line) = @_;
    my $later = 0;
    my $version = 0;

    # do some very quick spelling corrections for english/british words
    $line =~ s/Version 2,? \(June 1991\)/Version 2/gi;
    $line =~ s/Version 2,? dated June 1991/Version 2/gi;
    $line =~ s/Version 2\.1,? dated February 1999/Version 2.1/gi;
    if ($line =~ s/,? or \(?at your option\)?,? any later version//i) {
        $later = 1;
    }
    if ($line =~ s/, or any later version//i) {
        $later = 1;
    }
    if ($line =~ s/ or (greater|later)//i) {
        $later = 1;
    }
    if ($line =~ s/or (greater|later) //i) {
        $later = 1;
    }
    if ($line =~ s/(version|v\.?) ([123\.0]+)/<VERSION>/i) {
        $version = $2;
#        print "Version [$version]\n";
    }
    if ($line =~ s/GPL ?[v\-]([123\.0]+)/GPL <VERSION>/i) {
        $version = $1;
    }
    if ($line =~ s/v\.?([123\.0]+)( *[0-9]+)/<VERSION>$2/i) {
        $version = $1;
    }

    $line =~ s/(distributable|licensed|released|made available)/<LICENSED>/ig;
    $line =~ s/Library General Public License/Library General Public License/ig;
    $line =~ s/Lesser General Public License/Lesser General Public License/ig;

    $line =~ s/General Public License/GPL/gi;
    $line =~ s/GPL \(GPL\)/GPL/gi;
    $line =~ s/GPL \(<QUOTES>GPL<QUOTES>\)/GPL/gi;


    $line =~ s/GNU //gi;
    $line =~ s/under GPL/under the GPL/gi;
    $line =~ s/under Lesser/under the Lesser/gi;
    $line =~ s/under Library/under the Library/gi;

    $line =~ s/of GPL/of the GPL/gi;
    $line =~ s/of Lesser/of the Lesser/gi;
    $line =~ s/of Library/of the Library/gi;

    $line =~ s/(can|may)/can/gi;
    $line =~ s/<VERSION> only/<VERSION>/gi;
    $line =~ s/<VERSION> of the license/<VERSION>/gi;
    $line =~ s/(<VERSION>|GPL),? as published by the Free Software Foundation/$1/gi;
    $line =~ s/(<VERSION>|GPL) \(as published by the Free Software Foundation\)/$1/gi;
    $line =~ s/(<VERSION>|GPL),? incorporated herein by reference/$1/gi;
    $line =~ s/terms and conditions/terms/gi;
    $line =~ s/GPL along with/GPL with/gi;

    $line =~ s/GPL \(<VERSION\)/GPL <VERSION>/gi;

    $line =~ s/ +/ /;
    $line =~ s/ +$//;

#    print ">>>>>>>>>>$line:$later:$version\n";

    return ($line,$later,$version);
}

sub Looks_Like_GPL
{
    my ($line) = @_;

    return 1 if $line =~ /GNU/;
    return 1 if $line =~ /General Public License/;
    return 1 if $line =~ /GPL/;

    return 0;
}


sub Normalize_Sentence
{
    my ($line) = @_;
    # do some very quick spelling corrections for english/british words
    $line=~ s/icence/icense/ig;
    $line=~ s/(\.|;)$//;

    return $line;
}

# Return the Levenshtein distance (also called Edit distance) 
# between two strings
#
# The Levenshtein distance (LD) is a measure of similarity between two
# strings, denoted here by s1 and s2. The distance is the number of
# deletions, insertions or substitutions required to transform s1 into
# s2. The greater the distance, the more different the strings are.
#
# The algorithm employs a proximity matrix, which denotes the distances
# between substrings of the two given strings. Read the embedded comments
# for more info. If you want a deep understanding of the algorithm, print
# the matrix for some test strings and study it
#
# The beauty of this system is that nothing is magical - the distance
# is intuitively understandable by humans
#
# The distance is named after the Russian scientist Vladimir
# Levenshtein, who devised the algorithm in 1965
#
sub levenshtein
  {
    # $s1 and $s2 are the two strings
    # $len1 and $len2 are their respective lengths
    #
    my ($s1, $s2) = @_;
    my ($len1, $len2) = (length $s1, length $s2);
    
    # If one of the strings is empty, the distance is the length
    # of the other string
    #
    return $len2 if ($len1 == 0);
    return $len1 if ($len2 == 0);
    
    my %mat;
    
    # Init the distance matrix
    #
    # The first row to 0..$len1
    # The first column to 0..$len2
    # The rest to 0
    #
    # The first row and column are initialized so to denote distance
    # from the empty string
    #
    for (my $i = 0; $i <= $len1; ++$i)
      {
        for (my $j = 0; $j <= $len2; ++$j)
	  {
            $mat{$i}{$j} = 0;
            $mat{0}{$j} = $j;
	  }
	
        $mat{$i}{0} = $i;
      }
    
    # Some char-by-char processing is ahead, so prepare
    # array of chars from the strings
    #
    my @ar1 = split(//, $s1);
    my @ar2 = split(//, $s2);
    
    for (my $i = 1; $i <= $len1; ++$i)
      {
        for (my $j = 1; $j <= $len2; ++$j)
	  {
            # Set the cost to 1 iff the ith char of $s1
            # equals the jth of $s2
            # 
            # Denotes a substitution cost. When the char are equal
            # there is no need to substitute, so the cost is 0
            #
            my $cost = ($ar1[$i-1] eq $ar2[$j-1]) ? 0 : 1;
	    
            # Cell $mat{$i}{$j} equals the minimum of:
            #
            # - The cell immediately above plus 1
            # - The cell immediately to the left plus 1
            # - The cell diagonally above and to the left plus the cost
            #
            # We can either insert a new char, delete a char or
            # substitute an existing char (with an associated cost)
            #
            $mat{$i}{$j} = min([$mat{$i-1}{$j} + 1,
                                $mat{$i}{$j-1} + 1,
                                $mat{$i-1}{$j-1} + $cost]);
	  }
      }
    
    # Finally, the Levenshtein distance equals the rightmost bottom cell
    # of the matrix
    #
    # Note that $mat{$x}{$y} denotes the distance between the substrings
    # 1..$x and 1..$y
    #
    return $mat{$len1}{$len2};
  }
  
  
  # minimal element of a list
  #
  sub min
    {
      my @list = @{$_[0]};
      my $min = $list[0];
      
      foreach my $i (@list)
	{
	  $min = $i if ($i < $min);
	}
      
      return $min;
    }
    
    sub max{
      my @list = @_;
      return $list[0]>$list[1]?$list[0]:$list[1];
    }