new-words

annotate new-words.sh @ 24:1318aa5898ee

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