new-words

annotate new-words.sh @ 25:d1eb7dc37feb

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