new-words

annotate new-words.sh @ 22:46e987f4636d

part.pl script + german normalization support
author Igor Chubin <igor@chub.in>
date Sun May 16 18:20:18 2010 +0300 (2010-05-16)
parents 190d4ac6b07c
children 4b9d13c78de2
rev   line source
igor@0 1 #!/bin/sh
igor@20 2
igor@20 3 show_usage()
igor@20 4 {
igor@20 5 cat <<HELP > /dev/stderr
igor@0 6
igor@16 7 USAGE:
igor@16 8
igor@16 9 new-words [ -l lang ] [ -s ] [ ARG ]
igor@16 10
igor@16 11 SWITCHES:
igor@16 12
igor@20 13 -h print this screen
igor@20 14 -k put higher words that are similar to the known words (only for English)
igor@16 15 -l lang override language settings
igor@21 16 -n non-interactive mode (don't run vi)
igor@21 17 -m don't add marks (and don't save marks added by user)
igor@20 18 -s show the text statistics (percentage of known words and so on) and exit
igor@16 19
igor@20 20 The language of the text can be specified also
igor@20 21 by name of the program new-words (correspondent link must be created before).
igor@20 22 For example, these calls are equivalent:
igor@0 23
igor@20 24 de-words URL
igor@0 25 new-words -l de URL
igor@0 26
igor@20 27 HELP
igor@20 28 }
igor@0 29
igor@20 30 if [ "$1" = "-h" ]
igor@20 31 then
igor@20 32 show_usage
igor@20 33 exit 0
igor@20 34 fi
igor@0 35
igor@0 36 WORK_DIR=~/.new-words/
igor@17 37 TEMP1=`mktemp /tmp/new-words-XXXXXXXXXX-temp1`
igor@17 38 TEMP2=`mktemp /tmp/new-words-XXXXXXXXXX-temp2`
igor@17 39 export ORIGINAL_TEXT=`mktemp /tmp/new-words-XXXXXXXXXX-orig`
igor@0 40 editor=${EDITOR:-vim}
igor@0 41
igor@21 42 #----------------------------------------------------
igor@21 43 # command line options processing
igor@21 44
igor@2 45 LANGUAGE=en
igor@2 46 my_name="`echo $0 | sed s@.*/@@ | sed s/-.*// `"
igor@22 47 for arg
igor@22 48 do
igor@22 49 if echo "$arg" | grep -q http://...wikipedia.org/wiki/
igor@22 50 then
igor@22 51 LANGUAGE="`echo $arg | sed s@http://@@ | sed s@.wikipedia.*@@`"
igor@22 52 fi
igor@22 53 done
igor@2 54 [ "${my_name}" = "new" ] || LANGUAGE="$my_name"
igor@0 55 if [ "$1" = "-l" ]
igor@0 56 then
igor@0 57 LANGUAGE="$2"
igor@0 58 VOCABULARY="$LANGUAGE".txt
igor@0 59 shift 2
igor@0 60 fi
igor@2 61 VOCABULARY=${LANGUAGE}.txt
igor@2 62 NOTES_FILE=notes-${LANGUAGE}.txt
igor@0 63
igor@16 64 STAT_ONLY=NO
igor@16 65 if [ "$1" = "-s" ]
igor@16 66 then
igor@16 67 STAT_ONLY=YES
igor@16 68 shift
igor@16 69 fi
igor@16 70
igor@20 71 NEED_TO_USE_VOCABULARY_WHEN_SORT=NO
igor@20 72 if [ "$1" = "-k" ]
igor@20 73 then
igor@20 74 NEED_TO_USE_VOCABULARY_WHEN_SORT=YES
igor@20 75 shift
igor@20 76 fi
igor@20 77
igor@21 78 DONT_ADD_MARKS=NO
igor@21 79 if [ "$1" = "-m" ]
igor@21 80 then
igor@21 81 DONT_ADD_MARKS=YES
igor@21 82 shift
igor@21 83 fi
igor@21 84
igor@21 85 NON_INTERACTIVE_MODE=NO
igor@21 86 if [ "$1" = "-n" ]
igor@21 87 then
igor@21 88 NON_INTERACTIVE_MODE=YES
igor@21 89 shift
igor@21 90 fi
igor@21 91
igor@21 92 #----------------------------------------------------
igor@21 93
igor@0 94 get_words()
igor@0 95 {
igor@1 96 tr ' ' '\n' | sed 's/--/ /g' \
igor@3 97 | sed "s/'/__APOSTROPHE__/g" \
igor@3 98 | tr '—·-' '-----' \
igor@3 99 | tr '*\r,.:#@()+=—<>$;"?!|·[]^%&' ' ' \
igor@3 100 | tr ' ' '\n' \
igor@3 101 | grep -x '[[:alpha:]'"'"'-]*' \
igor@3 102 | tee "$1" \
igor@3 103 | grep_v_english_perl \
igor@0 104 | sort | uniq -c | awk '{if ($2!="") print;}' | sort -rn
igor@0 105 }
igor@0 106
igor@3 107 add_stat()
igor@3 108 {
igor@3 109 before="$1"
igor@3 110 after=${before}2
igor@3 111 cat > "$after"
igor@3 112 total="`wc -w $1 | awk '{print $1}'`"
igor@3 113 total_unknown="`cat $after|awk '{s=s+$1}END{print s}'`"
igor@3 114 total_known="`echo $total-$total_unknown|bc`"
igor@3 115 percentage="`echo '100*('$total-$total_unknown')'/$total | bc -l | sed 's/\\.\(.\).*/.\1/'`"
igor@18 116 #sentences="`cat $after | perl -e 'local $/; $_=<>; s@http://[a-zA-Z&_.:/0-9%?=,\#+()\[\]~-]*@@g; s@\n@@g; s@(Mr|Mrs)\.@\1POINT@g; @sentences=split /\\./;print $#sentences;'`"
igor@18 117 sentences="`cat $ORIGINAL_TEXT | perl -e 'local $/; $_=<>; s/[^.]//msg; print length($_);'`"
igor@18 118
igor@18 119
igor@16 120 if [ "$STAT_ONLY" = "YES" ]
igor@16 121 then
igor@18 122 echo "LANG KNOWN% UNKNOWN% KNOWN TOTAL WPS UWPS*10"
igor@18 123 echo "$LANGUAGE $percentage `echo \(100-$percentage\) | bc -l` $total_known $total `echo $total/$sentences|bc` `echo 10*$total_unknown/$sentences|bc` "
igor@17 124 rm $after
igor@16 125 return 0
igor@16 126 else
igor@16 127 echo "# $LANGUAGE, $percentage, <$total_known/$total>"
igor@16 128 fi
igor@16 129
igor@14 130 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
igor@14 131 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@14 132 my $total=shift(@ARGV);
igor@14 133 my $total_known=shift(@ARGV);
igor@14 134 my $s=0;
igor@16 135 my $mark_line=int($total_known*100/$total/5)*5;
igor@19 136 if ($mark_line>=90) {
igor@19 137 $mark_line=int($total_known*100/$total)+1;
igor@19 138 } else { $mark_line +=5; };
igor@14 139 while(<>)
igor@14 140 {
igor@14 141 print;
igor@14 142 /^\s*([0-9]*)\s*/;
igor@14 143 $s+=$1;
igor@19 144 if (($total_known+$s)*100/$total>=$mark_line) {
igor@14 145 print "# $mark_line\n";
igor@14 146 if ($mark_line>=90) { $mark_line+=1; } else { $mark_line +=5; };
igor@14 147 }
igor@14 148 }
igor@14 149 PERL_SCRIPT
igor@14 150 perl $PERL_SCRIPT_TEMP_NAME "$total" "$total_known" "$after"
igor@14 151 rm $PERL_SCRIPT_TEMP_NAME
igor@3 152 rm $after
igor@3 153 }
igor@3 154
igor@0 155 grep_v_english()
igor@0 156 {
igor@0 157 [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
igor@0 158 eval $(cat $VOCABULARY | tr -d "'" | xargs -n10 echo | tr ' ' '|' | sed 's/^/egrep -xv "RRRRRRR|/' | sed 's/$/"/' | tr '\n' '|')cat
igor@0 159 }
igor@0 160
igor@0 161 grep_v_english_perl()
igor@0 162 {
igor@0 163 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
igor@0 164 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@0 165 open(VOC, $ENV{VOCABULARY})
igor@0 166 or die "Can't open VOCABULARY";
igor@0 167 while (<VOC>){
igor@0 168 chomp;
igor@3 169 #s/'//g;
igor@0 170 $voc{$_}="1";
igor@0 171 }
igor@0 172 while(<>) {
igor@0 173 chomp;
igor@0 174 if (not defined($voc{$_})) { print "$_\n"; }
igor@0 175 }
igor@0 176 PERL_SCRIPT
igor@0 177 [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
igor@0 178 export VOCABULARY
igor@0 179 perl $PERL_SCRIPT_TEMP_NAME
igor@0 180 rm $PERL_SCRIPT_TEMP_NAME
igor@0 181 }
igor@0 182
igor@11 183 group_words()
igor@11 184 {
igor@22 185 #if [ "$LANGUAGE" != "en" ]
igor@22 186 #then
igor@22 187 # cat
igor@22 188 # return
igor@22 189 #fi
igor@11 190 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-group-words-XXXXXXXX`
igor@11 191 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@11 192 #!/usr/bin/perl
igor@11 193
igor@12 194 eval {
igor@12 195 # http://stackoverflow.com/questions/251694/how-can-i-check-if-i-have-a-perl-module-before-using-it
igor@12 196 require String::Similarity;
igor@12 197 String::Similarity->import();
igor@12 198 };
igor@12 199 unless($@)
igor@12 200 {
igor@12 201 our $HAVE_String_Similarity=1;
igor@12 202 }
igor@12 203
igor@12 204 sub similar($$){
igor@12 205 my $a=shift;
igor@12 206 my $b=shift;
igor@12 207 if ($HAVE_String_Similarity) {
igor@12 208 return $Similarity{"$a $b"};
igor@12 209 }
igor@12 210 else {
igor@12 211 return 0;
igor@12 212 }
igor@12 213 }
igor@12 214
igor@11 215 sub normalize($)
igor@11 216 {
igor@22 217 if ( $ENV{LANGUAGE} eq "en" ) { return normalize_english(shift); }
igor@22 218 elsif ( $ENV{LANGUAGE} eq "de" ) { return normalize_german(shift); }
igor@22 219 else { return shift ; }
igor@22 220 }
igor@22 221
igor@22 222 sub normalize_german($)
igor@22 223 {
igor@22 224 $_=lc(shift);
igor@22 225
igor@22 226 s/heit$//; s/keit$//; s/tum$//; s/ung$//; s/nis$//;s/schaft$//; s/ist$//;
igor@22 227 s/en$//; s/er$//;
igor@22 228
igor@22 229 s/lich$//; s/ig$//;
igor@22 230 s/al$//; s/isch$//;
igor@22 231 s/ell$//; s/haft$//;
igor@22 232
igor@22 233 s/bar$//; s/sam$//; s/lich$//;
igor@22 234
igor@22 235 @prefixes=qw(
igor@22 236 ab an auf aus bei dazwischen ein fest heraus her hinaus hin los mit nach voraus vorbei vor weg weiter zurück zusammen zu
igor@22 237 be emp ent er ge miss ver zer durch über um unter wieder);
igor@22 238 for $pref (@prefixes) {
igor@22 239 s/^$pref//;
igor@22 240 }
igor@22 241
igor@22 242
igor@22 243 return $_;
igor@22 244 }
igor@22 245
igor@22 246 sub normalize_english($)
igor@22 247 {
igor@11 248 $_=lc(shift);
igor@12 249
igor@11 250 s/s$//;
igor@22 251
igor@22 252 s/ation$//; s/ness$//; s/ship$//; s/ally$//; s/ance$//;s/ity$//; s/ment$//;
igor@22 253
igor@11 254 s/ed$//;
igor@12 255 s/en$//;
igor@12 256 s/er$//;
igor@12 257 s/est$//;
igor@11 258 s/ing$//;
igor@12 259
igor@12 260 s/ism$//; s/ist$//; s/ful$//; s/able$//; s/ably$//;
igor@22 261 s/ify$//; s/fy$//; s/ly$//;
igor@12 262 s/ise$//; s/ize$//;
igor@12 263
igor@12 264 s/e$//;
igor@11 265 return $_;
igor@11 266 }
igor@11 267
igor@12 268
igor@11 269 sub compare($$)
igor@11 270 {
igor@11 271 my $a=shift;
igor@11 272 my $b=shift;
igor@11 273 $a =~ s/^\s*//;
igor@11 274 $b =~ s/^\s*//;
igor@11 275 my ($a1, $a2)= split /\s+/,$a,2;
igor@11 276 my ($b1, $b2)= split /\s+/,$b,2;
igor@11 277
igor@11 278 my $cmp = $group_weight{normalize($a2)} <=> $group_weight{normalize($b2)};
igor@12 279
igor@11 280 if ($cmp) {
igor@11 281 return $cmp;
igor@11 282 }
igor@11 283 else {
igor@11 284 if (normalize($a2) ne normalize($b2)) {
igor@11 285 return normalize($a2) cmp normalize($b2);
igor@11 286 }
igor@11 287 else {
igor@11 288 return $a1 <=> $b1;
igor@11 289 }
igor@11 290 }
igor@11 291 }
igor@11 292
igor@20 293 our %Vocabulary;
igor@20 294 open(VOC, $ENV{VOCABULARY})
igor@20 295 or die "Can't open VOCABULARY";
igor@20 296 while (<VOC>){
igor@20 297 chomp;
igor@20 298 #s/'//g;
igor@20 299 $Vocabulary{normalize($_)}="1";
igor@20 300 }
igor@20 301 close(VOC);
igor@20 302
igor@20 303 @lines=<STDIN>;
igor@11 304 for $L (@lines) {
igor@11 305 chomp($L);
igor@11 306 $l=$L;
igor@11 307 $l =~ s/^\s*//;
igor@11 308 my ($a, $b)=split(/\s+/,$l,2);
igor@11 309 $group_weight{normalize($b)}+=$a;
igor@11 310 }
igor@20 311 if ($ENV{NEED_TO_USE_VOCABULARY_WHEN_SORT} eq "YES") {
igor@20 312 for $k (keys %group_weight) {
igor@20 313 if (defined($Vocabulary{$k})) {
igor@20 314 $group_weight{$k} *= 2;
igor@20 315 }
igor@20 316 }
igor@20 317 }
igor@11 318 @lines2 = sort { compare($b,$a) } @lines;
igor@11 319 for $l (@lines2) {
igor@11 320 print "$l\n";
igor@11 321 }
igor@11 322 PERL_SCRIPT
igor@20 323 export VOCABULARY
igor@20 324 export NEED_TO_USE_VOCABULARY_WHEN_SORT
igor@22 325 export LANGUAGE
igor@11 326 perl $PERL_SCRIPT_TEMP_NAME
igor@11 327 rm $PERL_SCRIPT_TEMP_NAME
igor@11 328 }
igor@11 329
igor@0 330 text_from_url()
igor@0 331 {
igor@0 332 lynx -dump "$1" | perl -p -e 's@http://[a-zA-Z&_.:/0-9%?=,#+()\[\]~-]*@@'
igor@0 333 }
igor@0 334
igor@2 335 add_marks()
igor@2 336 {
igor@2 337 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
igor@2 338 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@2 339 $file = $ARGV[0];
igor@2 340 our $dict;
igor@2 341 if (open(NOTES, $ENV{NOTES_FILE})) {
igor@2 342 while(<NOTES>) {
igor@2 343 chomp;
igor@2 344 s/^\s+//;
igor@2 345 my ($a,$b)=split /\s+/,$_,2;
igor@2 346 $dict{$a}=$b;
igor@2 347 }
igor@2 348 }
igor@2 349 if (open(F, $file)) {
igor@2 350 @lines=<F>;
igor@2 351 close(F);
igor@2 352
igor@2 353 if (open(F, ">$file")) {
igor@2 354 for (@lines) {
igor@2 355 m/\s+\S+\s+(\S+)/;
igor@2 356 $name=$1;
igor@3 357 if (not /^#/ and defined($dict{$name})) {
igor@2 358 chomp;
igor@2 359 $mark=$dict{$name};
igor@2 360 $space=" "x(30-length($_));
igor@2 361 print F "$_$space$mark\n";
igor@2 362 }
igor@2 363 else {
igor@2 364 print F "$_";
igor@2 365 }
igor@2 366 }
igor@2 367 close(F);
igor@2 368 }
igor@2 369 }
igor@2 370 PERL_SCRIPT
igor@2 371 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
igor@2 372 export NOTES_FILE
igor@2 373 perl $PERL_SCRIPT_TEMP_NAME "$1"
igor@2 374 rm $PERL_SCRIPT_TEMP_NAME
igor@2 375 }
igor@2 376
igor@2 377 remove_marks()
igor@2 378 {
igor@2 379 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
igor@2 380 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@2 381 $file = $ARGV[0];
igor@2 382 our %dict;
igor@2 383 if (open(F, $file)) {
igor@2 384 @lines=<F>;
igor@2 385 close(F);
igor@2 386
igor@2 387 if (open(F, ">$file")) {
igor@2 388 for (@lines) {
igor@2 389 chomp;
igor@3 390 if (not /^#/ and m/(\s+)(\S+)(\s+)(\S+)(\s+)(.*)/) {
igor@2 391 my $name=$4;
igor@2 392 my $comment=$6;
igor@2 393 $dict{$name}=$comment;
igor@2 394 print F "$1$2$3$4\n";
igor@2 395 }
igor@2 396 else {
igor@2 397 print F "$_\n";
igor@2 398 }
igor@2 399 }
igor@2 400 }
igor@2 401 }
igor@21 402 if (($ENV{DONT_ADD_MARKS} ne "YES") and open(NOTES, $ENV{NOTES_FILE})) {
igor@2 403 @lines=<NOTES>;
igor@2 404 close(NOTES);
igor@2 405
igor@2 406 if (open(NOTES, ">".$ENV{NOTES_FILE})) {
igor@2 407 for (@lines) {
igor@2 408 chomp;
igor@2 409 s/^\s+//;
igor@2 410 my ($a,$b)=split /\s+/,$_,2;
igor@2 411 if (not defined($dict{$a}) || ($dict{$a} eq $b)) {
igor@2 412 print NOTES "$_\n";
igor@2 413 if (defined($dict{$a})) { unset($dict{$a}); }
igor@2 414 }
igor@2 415 }
igor@2 416 for (keys %dict) {
igor@2 417 $mark=$dict{$_};
igor@2 418 $space=" "x(30-length($_));
igor@2 419 print NOTES "$_$space$mark\n";
igor@2 420 }
igor@2 421 }
igor@2 422 }
igor@2 423 PERL_SCRIPT
igor@2 424 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
igor@2 425 export NOTES_FILE
igor@21 426 export DONT_ADD_MARKS
igor@2 427 perl $PERL_SCRIPT_TEMP_NAME "$1"
igor@2 428 rm $PERL_SCRIPT_TEMP_NAME
igor@2 429 }
igor@2 430
igor@0 431 mkdir -p $WORK_DIR
igor@8 432 oldpwd="$PWD"
igor@0 433 cd $WORK_DIR
igor@0 434 if echo "$1" | grep -q http:
igor@0 435 then
igor@11 436 text_from_url "$1"
igor@0 437 elif [ "$#" != 0 ]
igor@0 438 then
igor@13 439 if echo $1 | grep -q ^/
igor@13 440 then
igor@13 441 cat "$1"
igor@13 442 else
igor@13 443 cat "$oldpwd/$1"
igor@13 444 fi
igor@0 445 else
igor@11 446 cat
igor@11 447 fi \
igor@11 448 | tee $ORIGINAL_TEXT \
igor@11 449 | get_words ${TEMP1}-full \
igor@11 450 | group_words \
igor@11 451 | add_stat ${TEMP1}-full \
igor@11 452 | tee "$TEMP1" > "$TEMP2"
igor@0 453
igor@16 454 if [ "$STAT_ONLY" = "YES" ]
igor@0 455 then
igor@16 456 cat "$TEMP1"
igor@21 457 elif [ "$NON_INTERACTIVE_MODE" = "YES" ]
igor@21 458 then
igor@21 459 cat "$TEMP1"
igor@0 460 else
igor@21 461 [ "$DONT_ADD_MARKS" = "YES" ] || add_marks "$TEMP2"
igor@16 462 if [ "$editor" = vim ]
igor@16 463 then
igor@16 464 vim -c 'set keywordprg='"$LANGUAGE" -c 'set iskeyword=@,48-57,/,.,-,_,+,,,#,$,%,~,=' "$TEMP2" < /dev/tty > /dev/tty
igor@16 465 else
igor@16 466 echo 2
igor@16 467 $editor "$TEMP2"
igor@16 468 fi
igor@16 469 remove_marks "$TEMP2"
igor@0 470 fi
igor@2 471
igor@0 472 diff "$TEMP1" "$TEMP2" | awk '{print $3}' | sort -u >> "$VOCABULARY"
igor@5 473 rm -f "$TEMP1" "$TEMP2" "${TEMP1}-full" "$ORIGINAL_TEXT"