new-words

annotate new-words.sh @ 32:753fb84437aa

link between words support; link is created using @
author Igor Chubin <igor@chub.in>
date Fri Nov 05 20:07:46 2010 +0100 (2010-11-05)
parents 48ca8248e9cc
children 720a701b2ba9
rev   line source
igor@26 1 #!/bin/bash
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@27 17 -a 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@27 20 -t tag tag known words with tag
igor@27 21 -T show list of active tags
igor@27 22 -m tag merge the words tagged with "tag" into the main vocabulary
igor@27 23 -M merge the words tagged with any tag into the main vocabulary
igor@27 24 -r tag remove subvocabulary for the "tag"
igor@26 25 -2 -3 find 2 and 3 words' sequences
igor@16 26
igor@20 27 The language of the text can be specified also
igor@20 28 by name of the program new-words (correspondent link must be created before).
igor@20 29 For example, these calls are equivalent:
igor@0 30
igor@20 31 de-words URL
igor@0 32 new-words -l de URL
igor@0 33
igor@20 34 HELP
igor@20 35 }
igor@0 36
igor@20 37 if [ "$1" = "-h" ]
igor@20 38 then
igor@20 39 show_usage
igor@20 40 exit 0
igor@20 41 fi
igor@0 42
igor@0 43 WORK_DIR=~/.new-words/
igor@28 44 TEMP1=`mktemp /tmp/new-words-temp1.XXXXXXXXXX`
igor@28 45 TEMP2=`mktemp /tmp/new-words-temp2.XXXXXXXXXX`
igor@28 46 export ORIGINAL_TEXT=`mktemp /tmp/new-words-orig.XXXXXXXXXX`
igor@0 47 editor=${EDITOR:-vim}
igor@0 48
igor@24 49 # language detection
igor@21 50
igor@2 51 LANGUAGE=en
igor@2 52 my_name="`echo $0 | sed s@.*/@@ | sed s/-.*// `"
igor@22 53 for arg
igor@22 54 do
igor@22 55 if echo "$arg" | grep -q http://...wikipedia.org/wiki/
igor@22 56 then
igor@22 57 LANGUAGE="`echo $arg | sed s@http://@@ | sed s@.wikipedia.*@@`"
igor@22 58 fi
igor@22 59 done
igor@2 60 [ "${my_name}" = "new" ] || LANGUAGE="$my_name"
igor@24 61
igor@24 62 #----------------------------------------------------
igor@24 63 # command line options processing
igor@24 64
igor@24 65 STAT_ONLY=NO
igor@24 66 NEED_TO_USE_VOCABULARY_WHEN_SORT=NO
igor@24 67 DONT_ADD_MARKS=NO
igor@24 68 NON_INTERACTIVE_MODE=NO
igor@25 69 PART_TO_PROCESS=''
igor@26 70 GROUP_WORDS_BY_THREE=NO
igor@26 71 GROUP_WORDS_BY_TWO=NO
igor@27 72 TAG_NAME=''
igor@27 73 MERGE_THIS_TAGS=''
igor@27 74 TAGS_LIST_ONLY=NO
igor@27 75 MERGE_TAGGED_WORDS=NO
igor@27 76 MERGE_ALL_TAGGED=NO
igor@27 77 DONT_ADD_MARKLINES=NO
igor@27 78 while getopts l:skanp:t:Tm:Mr:23 opt
igor@24 79 do
igor@24 80 case "$opt" in
igor@24 81 s) STAT_ONLY=YES;;
igor@24 82 k) NEED_TO_USE_VOCABULARY_WHEN_SORT=YES;;
igor@24 83 l) LANGUAGE="$OPTARG";;
igor@27 84 a) DONT_ADD_MARKS=YES;;
igor@24 85 n) NON_INTERACTIVE_MODE=YES;;
igor@25 86 p) PART_TO_PROCESS="$OPTARG";;
igor@27 87 t) TAG_NAME="$OPTARG";;
igor@27 88 T) TAGS_LIST_ONLY="YES";;
igor@27 89 m) DONT_ADD_MARKLINES="YES"; MERGE_TAGGED_WORDS="YES"; MERGE_THIS_TAGS="$TAG_NAME $OPTARG";;
igor@27 90 M) DONT_ADD_MARKLINES="YES"; MERGE_ALL_TAGGED="YES";;
igor@27 91 r) REMOVE_TAG="YES"; TAG_NAME="$TAG_NAME $OPTARG";;
igor@26 92 2) GROUP_WORDS_BY_TWO=YES;;
igor@26 93 3) GROUP_WORDS_BY_THREE=YES;;
igor@24 94 \?) # unknown flag
igor@24 95 show_usage
igor@24 96 exit 1;;
igor@24 97 esac
igor@24 98 done
igor@24 99 shift `expr $OPTIND - 1`
igor@24 100
igor@0 101 if [ "$1" = "-l" ]
igor@0 102 then
igor@0 103 LANGUAGE="$2"
igor@0 104 shift 2
igor@0 105 fi
igor@24 106
igor@2 107 VOCABULARY=${LANGUAGE}.txt
igor@2 108 NOTES_FILE=notes-${LANGUAGE}.txt
igor@0 109
igor@21 110 #----------------------------------------------------
igor@21 111
igor@0 112 get_words()
igor@0 113 {
igor@1 114 tr ' ' '\n' | sed 's/--/ /g' \
igor@3 115 | sed "s/'/__APOSTROPHE__/g" \
igor@31 116 | perl -MEncode -Mutf8 -n -e '$_ = decode( "utf8", $_);y/*\r,.:#@()+=—<>$;"?!|·[]^%&/ /; binmode STDOUT, ":utf8"; print if /^[[:alpha:] '"'"'_-]*$/'\
igor@26 117 | sed "s/__APOSTROPHE__/'/g" \
igor@3 118 | tr ' ' '\n' \
igor@3 119 | tee "$1" \
igor@3 120 | grep_v_english_perl \
igor@0 121 | sort | uniq -c | awk '{if ($2!="") print;}' | sort -rn
igor@0 122 }
igor@0 123
igor@3 124 add_stat()
igor@3 125 {
igor@27 126 if [ "$DONT_ADD_MARKLINES" = "YES" ]
igor@27 127 then
igor@27 128 cat
igor@27 129 return
igor@27 130 fi
igor@3 131 before="$1"
igor@3 132 after=${before}2
igor@3 133 cat > "$after"
igor@3 134 total="`wc -w $1 | awk '{print $1}'`"
igor@3 135 total_unknown="`cat $after|awk '{s=s+$1}END{print s}'`"
igor@3 136 total_known="`echo $total-$total_unknown|bc`"
igor@3 137 percentage="`echo '100*('$total-$total_unknown')'/$total | bc -l | sed 's/\\.\(.\).*/.\1/'`"
igor@18 138 #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 139 sentences="`cat $ORIGINAL_TEXT | perl -e 'local $/; $_=<>; s/[^.]//msg; print length($_);'`"
igor@18 140
igor@18 141
igor@16 142 if [ "$STAT_ONLY" = "YES" ]
igor@16 143 then
igor@18 144 echo "LANG KNOWN% UNKNOWN% KNOWN TOTAL WPS UWPS*10"
igor@18 145 echo "$LANGUAGE $percentage `echo \(100-$percentage\) | bc -l` $total_known $total `echo $total/$sentences|bc` `echo 10*$total_unknown/$sentences|bc` "
igor@17 146 rm $after
igor@16 147 return 0
igor@16 148 else
igor@16 149 echo "# $LANGUAGE, $percentage, <$total_known/$total>"
igor@16 150 fi
igor@16 151
igor@14 152 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
igor@14 153 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@14 154 my $total=shift(@ARGV);
igor@14 155 my $total_known=shift(@ARGV);
igor@14 156 my $s=0;
igor@16 157 my $mark_line=int($total_known*100/$total/5)*5;
igor@19 158 if ($mark_line>=90) {
igor@19 159 $mark_line=int($total_known*100/$total)+1;
igor@19 160 } else { $mark_line +=5; };
igor@14 161 while(<>)
igor@14 162 {
igor@14 163 print;
igor@14 164 /^\s*([0-9]*)\s*/;
igor@14 165 $s+=$1;
igor@19 166 if (($total_known+$s)*100/$total>=$mark_line) {
igor@14 167 print "# $mark_line\n";
igor@14 168 if ($mark_line>=90) { $mark_line+=1; } else { $mark_line +=5; };
igor@14 169 }
igor@14 170 }
igor@14 171 PERL_SCRIPT
igor@14 172 perl $PERL_SCRIPT_TEMP_NAME "$total" "$total_known" "$after"
igor@14 173 rm $PERL_SCRIPT_TEMP_NAME
igor@3 174 rm $after
igor@3 175 }
igor@3 176
igor@26 177 two_and_three_words()
igor@26 178 {
igor@26 179 if [ $GROUP_WORDS_BY_THREE = NO -a $GROUP_WORDS_BY_TWO = NO ]
igor@26 180 then
igor@26 181 cat
igor@26 182 else
igor@26 183 cat
igor@26 184
igor@26 185 export GROUP_WORDS_BY_THREE
igor@26 186 export GROUP_WORDS_BY_TWO
igor@26 187 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-two-and-three-XXXXXXXX`
igor@26 188 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@26 189 #!/usr/bin/perl
igor@26 190 local $/;
igor@26 191 $words=<>;
igor@26 192 $words=~ s@[!?;,:#1-9".]@ @g;
igor@26 193 $words =~ s@\s+@ @g;
igor@26 194 @words = split /\s+/, $words;
igor@26 195 for ($i=0; $i<$#words-3;$i++) {
igor@26 196 my ($a,$b,$c)= ($words[$i],$words[$i+1],$words[$i+2]);
igor@26 197 if ($ENV{GROUP_WORDS_BY_THREE} eq "YES" and ($a && $b && $c)) {
igor@26 198 print "${a}_${b}_${c}\n";
igor@26 199 };
igor@26 200 if ($ENV{GROUP_WORDS_BY_TWO} eq "YES" and ($a && $b)) {
igor@26 201 print "${a}_${b}\n";
igor@26 202 };
igor@26 203 }
igor@26 204 PERL_SCRIPT
igor@26 205 perl $PERL_SCRIPT_TEMP_NAME "$ORIGINAL_TEXT"
igor@26 206 rm $PERL_SCRIPT_TEMP_NAME
igor@26 207 fi
igor@26 208 }
igor@26 209
igor@0 210 grep_v_english()
igor@0 211 {
igor@0 212 [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
igor@0 213 eval $(cat $VOCABULARY | tr -d "'" | xargs -n10 echo | tr ' ' '|' | sed 's/^/egrep -xv "RRRRRRR|/' | sed 's/$/"/' | tr '\n' '|')cat
igor@0 214 }
igor@0 215
igor@0 216 grep_v_english_perl()
igor@0 217 {
igor@0 218 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
igor@0 219 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@27 220 $voc_files=$ENV{VOC_FILES};
igor@27 221 $voc_files=~s@^ @@;
igor@27 222 for $voc_file (split /\s+/,$voc_files) {
igor@27 223 if (open(VOC, $voc_file)) {
igor@27 224 while (<VOC>){
igor@27 225 chomp;
igor@27 226 #s/'//g;
igor@27 227 $voc{$_}="1";
igor@27 228 }
igor@27 229 }
igor@0 230 }
igor@0 231 while(<>) {
igor@0 232 chomp;
igor@0 233 if (not defined($voc{$_})) { print "$_\n"; }
igor@0 234 }
igor@0 235 PERL_SCRIPT
igor@0 236 [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
igor@27 237 export VOCABULARY VOC_FILES
igor@27 238 VOC_FILES=$VOCABULARY
igor@27 239 for i in $TAG_NAME
igor@27 240 do
igor@27 241 VOC_FILES="${VOC_FILES} `tag_file_name $i`"
igor@27 242 done
igor@0 243 perl $PERL_SCRIPT_TEMP_NAME
igor@0 244 rm $PERL_SCRIPT_TEMP_NAME
igor@0 245 }
igor@0 246
igor@11 247 group_words()
igor@11 248 {
igor@22 249 #if [ "$LANGUAGE" != "en" ]
igor@22 250 #then
igor@22 251 # cat
igor@22 252 # return
igor@22 253 #fi
igor@11 254 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-group-words-XXXXXXXX`
igor@11 255 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@11 256 #!/usr/bin/perl
igor@11 257
igor@29 258 use Encode;
igor@29 259 use utf8;
igor@32 260 use Lingua::Stem::Snowball qw(stem);
igor@29 261
igor@12 262 eval {
igor@12 263 # http://stackoverflow.com/questions/251694/how-can-i-check-if-i-have-a-perl-module-before-using-it
igor@12 264 require String::Similarity;
igor@12 265 String::Similarity->import();
igor@12 266 };
igor@12 267 unless($@)
igor@12 268 {
igor@12 269 our $HAVE_String_Similarity=1;
igor@12 270 }
igor@12 271
igor@32 272
igor@32 273 sub load_notes_dict()
igor@32 274 {
igor@32 275 my %dict;
igor@32 276 if (open(NOTES, $ENV{NOTES_FILE})) {
igor@32 277 while(<NOTES>) {
igor@32 278 $_ = decode( "utf8", $_);
igor@32 279 chomp;
igor@32 280 s/^\s+//;
igor@32 281 my ($a,$b)=split /\s+/,$_,2;
igor@32 282 $dict{$a}=$b;
igor@32 283 }
igor@32 284 }
igor@32 285 return %dict;
igor@32 286 }
igor@32 287
igor@12 288 sub similar($$){
igor@12 289 my $a=shift;
igor@12 290 my $b=shift;
igor@12 291 if ($HAVE_String_Similarity) {
igor@12 292 return $Similarity{"$a $b"};
igor@12 293 }
igor@12 294 else {
igor@12 295 return 0;
igor@12 296 }
igor@12 297 }
igor@12 298
igor@32 299
igor@32 300 sub normalize_without_linked($)
igor@11 301 {
igor@22 302 if ( $ENV{LANGUAGE} eq "en" ) { return normalize_english(shift); }
igor@22 303 elsif ( $ENV{LANGUAGE} eq "de" ) { return normalize_german(shift); }
igor@29 304 elsif ( $ENV{LANGUAGE} eq "uk" ) { return normalize_ukrainian(shift); }
igor@31 305 elsif ( $ENV{LANGUAGE} eq "io" ) { return normalize_esperanto(shift); }
igor@22 306 else { return shift ; }
igor@22 307 }
igor@22 308
igor@32 309 sub normalize_with_linked($)
igor@32 310 {
igor@32 311 my $word = normalize_without_linked(shift);
igor@32 312 #return $word;
igor@32 313 if ($linked_words{$word}) {
igor@32 314 return $linked_words{$word};
igor@32 315 }
igor@32 316 else {
igor@32 317 return $word;
igor@32 318 }
igor@32 319 }
igor@32 320
igor@32 321 sub normalize($)
igor@32 322 {
igor@32 323 return normalize_with_linked(shift);
igor@32 324 }
igor@32 325
igor@29 326 sub normalize_ukrainian($)
igor@29 327 {
igor@29 328 $_=lc(shift);
igor@29 329 s/[юіоеуаи]$//g;
igor@29 330 return $_;
igor@29 331 }
igor@29 332
igor@31 333 sub normalize_esperanto($)
igor@31 334 {
igor@31 335 $_=lc(shift);
igor@31 336 # verbs
igor@31 337 s/i$//; s/is$//; s/os$//; s/as$//; s/us$//;
igor@31 338
igor@31 339 # nouns
igor@31 340 s/j?n?$//;
igor@31 341
igor@31 342 return $_;
igor@31 343 }
igor@31 344
igor@22 345 sub normalize_german($)
igor@22 346 {
igor@32 347 @stems = stem('de', \@_);
igor@32 348 return $stems[0];
igor@32 349 }
igor@32 350
igor@32 351 sub normalize_german_($)
igor@32 352 {
igor@22 353 $_=lc(shift);
igor@22 354
igor@22 355 s/heit$//; s/keit$//; s/tum$//; s/ung$//; s/nis$//;s/schaft$//; s/ist$//;
igor@22 356 s/en$//; s/er$//;
igor@22 357
igor@22 358 s/lich$//; s/ig$//;
igor@22 359 s/al$//; s/isch$//;
igor@22 360 s/ell$//; s/haft$//;
igor@22 361
igor@22 362 s/bar$//; s/sam$//; s/lich$//;
igor@22 363
igor@22 364 @prefixes=qw(
igor@22 365 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 366 be emp ent er ge miss ver zer durch über um unter wieder);
igor@24 367 @prefixes=();
igor@22 368 for $pref (@prefixes) {
igor@22 369 s/^$pref//;
igor@22 370 }
igor@22 371
igor@22 372
igor@22 373 return $_;
igor@22 374 }
igor@22 375
igor@22 376 sub normalize_english($)
igor@22 377 {
igor@11 378 $_=lc(shift);
igor@12 379
igor@11 380 s/s$//;
igor@22 381
igor@22 382 s/ation$//; s/ness$//; s/ship$//; s/ally$//; s/ance$//;s/ity$//; s/ment$//;
igor@22 383
igor@11 384 s/ed$//;
igor@12 385 s/en$//;
igor@12 386 s/er$//;
igor@12 387 s/est$//;
igor@11 388 s/ing$//;
igor@12 389
igor@12 390 s/ism$//; s/ist$//; s/ful$//; s/able$//; s/ably$//;
igor@22 391 s/ify$//; s/fy$//; s/ly$//;
igor@12 392 s/ise$//; s/ize$//;
igor@12 393
igor@12 394 s/e$//;
igor@11 395 return $_;
igor@11 396 }
igor@11 397
igor@12 398
igor@11 399 sub compare($$)
igor@11 400 {
igor@11 401 my $a=shift;
igor@11 402 my $b=shift;
igor@11 403 $a =~ s/^\s*//;
igor@11 404 $b =~ s/^\s*//;
igor@11 405 my ($a1, $a2)= split /\s+/,$a,2;
igor@11 406 my ($b1, $b2)= split /\s+/,$b,2;
igor@11 407
igor@11 408 my $cmp = $group_weight{normalize($a2)} <=> $group_weight{normalize($b2)};
igor@12 409
igor@11 410 if ($cmp) {
igor@11 411 return $cmp;
igor@11 412 }
igor@11 413 else {
igor@11 414 if (normalize($a2) ne normalize($b2)) {
igor@11 415 return normalize($a2) cmp normalize($b2);
igor@11 416 }
igor@11 417 else {
igor@11 418 return $a1 <=> $b1;
igor@11 419 }
igor@11 420 }
igor@11 421 }
igor@11 422
igor@32 423 sub log_($)
igor@32 424 {
igor@32 425 return;
igor@32 426 open(LOG, ">>", "/tmp/log1");
igor@32 427 print LOG $_[0];
igor@32 428 close(LOG);
igor@32 429 }
igor@32 430
igor@32 431 sub find_linked_words($)
igor@32 432 {
igor@32 433 my %linked_words;
igor@32 434 my $dict = shift;
igor@32 435 log_("1");
igor@32 436 log_(join(" ", keys(%$dict)));
igor@32 437
igor@32 438 for $key (keys(%$dict)) {
igor@32 439 $val = $dict->{$key};
igor@32 440 log_($key."\n");
igor@32 441 if ($val =~ /\@([a-z]*)/) {
igor@32 442 $linked_words{normalize($key)} = normalize($1);
igor@32 443 log_(normalize($key)." = ".normalize($1)."\n");
igor@32 444 }
igor@32 445 }
igor@32 446 return %linked_words;
igor@32 447 }
igor@32 448
igor@32 449 our %dict = load_notes_dict();
igor@32 450 our %linked_words = find_linked_words(\%dict);
igor@32 451
igor@20 452 our %Vocabulary;
igor@20 453 open(VOC, $ENV{VOCABULARY})
igor@20 454 or die "Can't open VOCABULARY";
igor@20 455 while (<VOC>){
igor@20 456 chomp;
igor@20 457 #s/'//g;
igor@20 458 $Vocabulary{normalize($_)}="1";
igor@20 459 }
igor@20 460 close(VOC);
igor@20 461
igor@29 462 binmode STDIN,":utf8";
igor@20 463 @lines=<STDIN>;
igor@11 464 for $L (@lines) {
igor@11 465 chomp($L);
igor@29 466 #$L = decode( "utf8", $L);
igor@11 467 $l=$L;
igor@11 468 $l =~ s/^\s*//;
igor@11 469 my ($a, $b)=split(/\s+/,$l,2);
igor@11 470 $group_weight{normalize($b)}+=$a;
igor@11 471 }
igor@20 472 if ($ENV{NEED_TO_USE_VOCABULARY_WHEN_SORT} eq "YES") {
igor@20 473 for $k (keys %group_weight) {
igor@20 474 if (defined($Vocabulary{$k})) {
igor@20 475 $group_weight{$k} *= 2;
igor@20 476 }
igor@20 477 }
igor@20 478 }
igor@11 479 @lines2 = sort { compare($b,$a) } @lines;
igor@29 480 binmode STDOUT, ":utf8";
igor@11 481 for $l (@lines2) {
igor@11 482 print "$l\n";
igor@11 483 }
igor@11 484 PERL_SCRIPT
igor@20 485 export VOCABULARY
igor@20 486 export NEED_TO_USE_VOCABULARY_WHEN_SORT
igor@22 487 export LANGUAGE
igor@32 488 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
igor@32 489 export NOTES_FILE
igor@11 490 perl $PERL_SCRIPT_TEMP_NAME
igor@11 491 rm $PERL_SCRIPT_TEMP_NAME
igor@11 492 }
igor@11 493
igor@0 494 text_from_url()
igor@0 495 {
igor@0 496 lynx -dump "$1" | perl -p -e 's@http://[a-zA-Z&_.:/0-9%?=,#+()\[\]~-]*@@'
igor@0 497 }
igor@0 498
igor@2 499 add_marks()
igor@2 500 {
igor@2 501 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
igor@2 502 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@29 503 use Encode;
igor@29 504
igor@32 505 sub load_notes_dict()
igor@32 506 {
igor@32 507 my %dict;
igor@32 508 if (open(NOTES, $ENV{NOTES_FILE})) {
igor@32 509 while(<NOTES>) {
igor@32 510 $_ = decode( "utf8", $_);
igor@32 511 chomp;
igor@32 512 s/^\s+//;
igor@32 513 my ($a,$b)=split /\s+/,$_,2;
igor@32 514 $dict{$a}=$b;
igor@32 515 }
igor@32 516 }
igor@32 517 return %dict;
igor@32 518 }
igor@32 519
igor@32 520 %dict = load_notes_dict();
igor@32 521
igor@2 522 $file = $ARGV[0];
igor@2 523 if (open(F, $file)) {
igor@2 524 @lines=<F>;
igor@2 525 close(F);
igor@29 526 for (@lines) {$_ = decode( "utf8", $_);};
igor@2 527
igor@2 528 if (open(F, ">$file")) {
igor@29 529 binmode F, ":utf8";
igor@2 530 for (@lines) {
igor@2 531 m/\s+\S+\s+(\S+)/;
igor@2 532 $name=$1;
igor@3 533 if (not /^#/ and defined($dict{$name})) {
igor@2 534 chomp;
igor@2 535 $mark=$dict{$name};
igor@2 536 $space=" "x(30-length($_));
igor@2 537 print F "$_$space$mark\n";
igor@2 538 }
igor@2 539 else {
igor@2 540 print F "$_";
igor@2 541 }
igor@2 542 }
igor@2 543 close(F);
igor@2 544 }
igor@2 545 }
igor@2 546 PERL_SCRIPT
igor@2 547 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
igor@2 548 export NOTES_FILE
igor@2 549 perl $PERL_SCRIPT_TEMP_NAME "$1"
igor@2 550 rm $PERL_SCRIPT_TEMP_NAME
igor@2 551 }
igor@2 552
igor@2 553 remove_marks()
igor@2 554 {
igor@2 555 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
igor@2 556 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@2 557 $file = $ARGV[0];
igor@2 558 our %dict;
igor@2 559 if (open(F, $file)) {
igor@2 560 @lines=<F>;
igor@2 561 close(F);
igor@2 562
igor@2 563 if (open(F, ">$file")) {
igor@2 564 for (@lines) {
igor@2 565 chomp;
igor@3 566 if (not /^#/ and m/(\s+)(\S+)(\s+)(\S+)(\s+)(.*)/) {
igor@2 567 my $name=$4;
igor@2 568 my $comment=$6;
igor@2 569 $dict{$name}=$comment;
igor@2 570 print F "$1$2$3$4\n";
igor@2 571 }
igor@2 572 else {
igor@2 573 print F "$_\n";
igor@2 574 }
igor@2 575 }
igor@2 576 }
igor@2 577 }
igor@21 578 if (($ENV{DONT_ADD_MARKS} ne "YES") and open(NOTES, $ENV{NOTES_FILE})) {
igor@2 579 @lines=<NOTES>;
igor@2 580 close(NOTES);
igor@2 581
igor@2 582 if (open(NOTES, ">".$ENV{NOTES_FILE})) {
igor@2 583 for (@lines) {
igor@2 584 chomp;
igor@2 585 s/^\s+//;
igor@2 586 my ($a,$b)=split /\s+/,$_,2;
igor@2 587 if (not defined($dict{$a}) || ($dict{$a} eq $b)) {
igor@2 588 print NOTES "$_\n";
igor@2 589 if (defined($dict{$a})) { unset($dict{$a}); }
igor@2 590 }
igor@2 591 }
igor@2 592 for (keys %dict) {
igor@2 593 $mark=$dict{$_};
igor@2 594 $space=" "x(30-length($_));
igor@2 595 print NOTES "$_$space$mark\n";
igor@2 596 }
igor@2 597 }
igor@2 598 }
igor@2 599 PERL_SCRIPT
igor@2 600 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
igor@2 601 export NOTES_FILE
igor@21 602 export DONT_ADD_MARKS
igor@2 603 perl $PERL_SCRIPT_TEMP_NAME "$1"
igor@2 604 rm $PERL_SCRIPT_TEMP_NAME
igor@2 605 }
igor@2 606
igor@25 607 part()
igor@25 608 {
igor@25 609 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-part-XXXXXXXX`
igor@25 610 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@25 611 #!/usr/bin/perl
igor@25 612
igor@25 613 my @lines=<STDIN>;
igor@25 614 my $lines=$#lines;
igor@25 615 my $interval=$ARGV[0];
igor@25 616 if (not $interval) {
igor@25 617 print @lines;
igor@25 618 }
igor@25 619 else {
igor@25 620 my ($start,$stop,$total);
igor@25 621 if ($interval =~ m@(.*)/(.*)@) {
igor@25 622 $start = $1;
igor@25 623 $total = $2;
igor@25 624 }
igor@25 625 else {
igor@25 626 $start=$interval;
igor@25 627 $total=0;
igor@25 628 }
igor@25 629 if ($start =~ m@(.*)-(.*)@) {
igor@25 630 $start = $1;
igor@25 631 $stop = $2;
igor@25 632 }
igor@25 633 if ($start =~ m@(.*)\+(.*)@) {
igor@25 634 $start = $1;
igor@25 635 $stop = $start+$2;
igor@25 636 }
igor@25 637
igor@25 638 $start=int($lines/$total*$start);
igor@25 639 $stop=int($lines/$total*$stop);
igor@25 640
igor@25 641 for($i=$start;$i<$stop;$i++){
igor@25 642 print $lines[$i];
igor@25 643 }
igor@25 644 }
igor@25 645 PERL_SCRIPT
igor@25 646 perl $PERL_SCRIPT_TEMP_NAME "$1"
igor@25 647 rm $PERL_SCRIPT_TEMP_NAME
igor@25 648 }
igor@25 649
igor@27 650 if [ "$TAGS_LIST_ONLY" = "YES" ]
igor@27 651 then
igor@27 652 cd "${WORK_DIR}"
igor@27 653 echo ${LANGUAGE}_*.txt | tr ' ' '\n' | grep -v '*' | sed 's/[^_]*_//;s/.txt$//'
igor@27 654 exit 0
igor@27 655 fi
igor@27 656
igor@27 657 tag_file_name()
igor@27 658 {
igor@27 659 echo "${LANGUAGE}_${1}.txt"
igor@27 660 }
igor@27 661
igor@27 662 if [ "$REMOVE_TAG" = "YES" ]
igor@27 663 then
igor@27 664 cd "${WORK_DIR}"
igor@27 665 for i in $TAG_NAME
igor@27 666 do
igor@27 667 echo "$TAGNAME" | grep -q '[/*?]' && continue
igor@27 668 f="`tag_file_name $i`"
igor@27 669 if [ -e "$f" ]
igor@27 670 then
igor@27 671 rm -f "$f" && echo Tag "'$i'" removed
igor@27 672 else
igor@27 673 echo Unknown tag "'$i'"
igor@27 674 fi
igor@27 675 done
igor@27 676 exit 0
igor@27 677 fi
igor@27 678
igor@0 679 mkdir -p $WORK_DIR
igor@8 680 oldpwd="$PWD"
igor@0 681 cd $WORK_DIR
igor@27 682 if [ "$MERGE_TAGGED_WORDS" = "YES" ]
igor@27 683 then
igor@27 684 VOC_FILES=''
igor@27 685 for i in $MERGE_THIS_TAGS
igor@27 686 do
igor@27 687 f=`tag_file_name $i`
igor@27 688 [ -e "$f" ] && VOC_FILES="${VOC_FILES} $f"
igor@27 689 done
igor@27 690 if [ -z "$VOC_FILES" ]
igor@27 691 then
igor@27 692 echo Unknown tags: $MERGE_THIS_TAGS > /dev/stderr
igor@27 693 else
igor@27 694 cat $VOC_FILES
igor@27 695 fi
igor@27 696 elif [ "$MERGE_ALL_TAGGED" = "YES" ]
igor@27 697 then
igor@27 698 cat ${LANGUAGE}_*.txt
igor@27 699 elif echo "$1" | grep -q http:
igor@0 700 then
igor@11 701 text_from_url "$1"
igor@0 702 elif [ "$#" != 0 ]
igor@0 703 then
igor@13 704 if echo $1 | grep -q ^/
igor@13 705 then
igor@13 706 cat "$1"
igor@13 707 else
igor@13 708 cat "$oldpwd/$1"
igor@13 709 fi
igor@0 710 else
igor@11 711 cat
igor@11 712 fi \
igor@25 713 | part $PART_TO_PROCESS \
igor@11 714 | tee $ORIGINAL_TEXT \
igor@26 715 | two_and_three_words \
igor@11 716 | get_words ${TEMP1}-full \
igor@11 717 | group_words \
igor@11 718 | add_stat ${TEMP1}-full \
igor@11 719 | tee "$TEMP1" > "$TEMP2"
igor@0 720
igor@16 721 if [ "$STAT_ONLY" = "YES" ]
igor@0 722 then
igor@16 723 cat "$TEMP1"
igor@21 724 elif [ "$NON_INTERACTIVE_MODE" = "YES" ]
igor@21 725 then
igor@21 726 cat "$TEMP1"
igor@0 727 else
igor@27 728 if [ `wc -l "$TEMP2" | awk '{print $1}'` != 0 ]
igor@16 729 then
igor@27 730 [ "$DONT_ADD_MARKS" = "YES" ] || add_marks "$TEMP2"
igor@27 731 if [ "$editor" = vim ]
igor@27 732 then
igor@27 733 vim -c 'set keywordprg='"$LANGUAGE" -c 'set iskeyword=@,48-57,/,.,-,_,+,,,#,$,%,~,=,48-255' "$TEMP2" < /dev/tty > /dev/tty
igor@27 734 else
igor@27 735 $editor "$TEMP2"
igor@27 736 fi
igor@27 737 remove_marks "$TEMP2"
igor@27 738
igor@27 739 vocabulary="$VOCABULARY"
igor@27 740 [ -n "$TAG_NAME" ] && vocabulary="`tag_file_name $TAG_NAME`"
igor@27 741 diff "$TEMP1" "$TEMP2" | awk '{print $3}' | sort -u >> "$vocabulary"
igor@16 742 fi
igor@0 743 fi
igor@2 744
igor@5 745 rm -f "$TEMP1" "$TEMP2" "${TEMP1}-full" "$ORIGINAL_TEXT"