new-words

annotate new-words.sh @ 29:c631833fa2be

minifixes related to unicode support
author Igor Chubin <igor@chub.in>
date Mon Jun 21 19:46:58 2010 +0300 (2010-06-21)
parents 7db7bbf96fad
children 48ca8248e9cc
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@26 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@29 260
igor@12 261 eval {
igor@12 262 # http://stackoverflow.com/questions/251694/how-can-i-check-if-i-have-a-perl-module-before-using-it
igor@12 263 require String::Similarity;
igor@12 264 String::Similarity->import();
igor@12 265 };
igor@12 266 unless($@)
igor@12 267 {
igor@12 268 our $HAVE_String_Similarity=1;
igor@12 269 }
igor@12 270
igor@12 271 sub similar($$){
igor@12 272 my $a=shift;
igor@12 273 my $b=shift;
igor@12 274 if ($HAVE_String_Similarity) {
igor@12 275 return $Similarity{"$a $b"};
igor@12 276 }
igor@12 277 else {
igor@12 278 return 0;
igor@12 279 }
igor@12 280 }
igor@12 281
igor@11 282 sub normalize($)
igor@11 283 {
igor@22 284 if ( $ENV{LANGUAGE} eq "en" ) { return normalize_english(shift); }
igor@22 285 elsif ( $ENV{LANGUAGE} eq "de" ) { return normalize_german(shift); }
igor@29 286 elsif ( $ENV{LANGUAGE} eq "uk" ) { return normalize_ukrainian(shift); }
igor@22 287 else { return shift ; }
igor@22 288 }
igor@22 289
igor@29 290 sub normalize_ukrainian($)
igor@29 291 {
igor@29 292 $_=lc(shift);
igor@29 293 s/[юіоеуаи]$//g;
igor@29 294 return $_;
igor@29 295 }
igor@29 296
igor@22 297 sub normalize_german($)
igor@22 298 {
igor@22 299 $_=lc(shift);
igor@22 300
igor@22 301 s/heit$//; s/keit$//; s/tum$//; s/ung$//; s/nis$//;s/schaft$//; s/ist$//;
igor@22 302 s/en$//; s/er$//;
igor@22 303
igor@22 304 s/lich$//; s/ig$//;
igor@22 305 s/al$//; s/isch$//;
igor@22 306 s/ell$//; s/haft$//;
igor@22 307
igor@22 308 s/bar$//; s/sam$//; s/lich$//;
igor@22 309
igor@22 310 @prefixes=qw(
igor@22 311 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 312 be emp ent er ge miss ver zer durch über um unter wieder);
igor@24 313 @prefixes=();
igor@22 314 for $pref (@prefixes) {
igor@22 315 s/^$pref//;
igor@22 316 }
igor@22 317
igor@22 318
igor@22 319 return $_;
igor@22 320 }
igor@22 321
igor@22 322 sub normalize_english($)
igor@22 323 {
igor@11 324 $_=lc(shift);
igor@12 325
igor@11 326 s/s$//;
igor@22 327
igor@22 328 s/ation$//; s/ness$//; s/ship$//; s/ally$//; s/ance$//;s/ity$//; s/ment$//;
igor@22 329
igor@11 330 s/ed$//;
igor@12 331 s/en$//;
igor@12 332 s/er$//;
igor@12 333 s/est$//;
igor@11 334 s/ing$//;
igor@12 335
igor@12 336 s/ism$//; s/ist$//; s/ful$//; s/able$//; s/ably$//;
igor@22 337 s/ify$//; s/fy$//; s/ly$//;
igor@12 338 s/ise$//; s/ize$//;
igor@12 339
igor@12 340 s/e$//;
igor@11 341 return $_;
igor@11 342 }
igor@11 343
igor@12 344
igor@11 345 sub compare($$)
igor@11 346 {
igor@11 347 my $a=shift;
igor@11 348 my $b=shift;
igor@11 349 $a =~ s/^\s*//;
igor@11 350 $b =~ s/^\s*//;
igor@11 351 my ($a1, $a2)= split /\s+/,$a,2;
igor@11 352 my ($b1, $b2)= split /\s+/,$b,2;
igor@11 353
igor@11 354 my $cmp = $group_weight{normalize($a2)} <=> $group_weight{normalize($b2)};
igor@12 355
igor@11 356 if ($cmp) {
igor@11 357 return $cmp;
igor@11 358 }
igor@11 359 else {
igor@11 360 if (normalize($a2) ne normalize($b2)) {
igor@11 361 return normalize($a2) cmp normalize($b2);
igor@11 362 }
igor@11 363 else {
igor@11 364 return $a1 <=> $b1;
igor@11 365 }
igor@11 366 }
igor@11 367 }
igor@11 368
igor@20 369 our %Vocabulary;
igor@20 370 open(VOC, $ENV{VOCABULARY})
igor@20 371 or die "Can't open VOCABULARY";
igor@20 372 while (<VOC>){
igor@20 373 chomp;
igor@20 374 #s/'//g;
igor@20 375 $Vocabulary{normalize($_)}="1";
igor@20 376 }
igor@20 377 close(VOC);
igor@20 378
igor@29 379 binmode STDIN,":utf8";
igor@20 380 @lines=<STDIN>;
igor@11 381 for $L (@lines) {
igor@11 382 chomp($L);
igor@29 383 #$L = decode( "utf8", $L);
igor@11 384 $l=$L;
igor@11 385 $l =~ s/^\s*//;
igor@11 386 my ($a, $b)=split(/\s+/,$l,2);
igor@11 387 $group_weight{normalize($b)}+=$a;
igor@11 388 }
igor@20 389 if ($ENV{NEED_TO_USE_VOCABULARY_WHEN_SORT} eq "YES") {
igor@20 390 for $k (keys %group_weight) {
igor@20 391 if (defined($Vocabulary{$k})) {
igor@20 392 $group_weight{$k} *= 2;
igor@20 393 }
igor@20 394 }
igor@20 395 }
igor@11 396 @lines2 = sort { compare($b,$a) } @lines;
igor@29 397 binmode STDOUT, ":utf8";
igor@11 398 for $l (@lines2) {
igor@11 399 print "$l\n";
igor@11 400 }
igor@11 401 PERL_SCRIPT
igor@20 402 export VOCABULARY
igor@20 403 export NEED_TO_USE_VOCABULARY_WHEN_SORT
igor@22 404 export LANGUAGE
igor@11 405 perl $PERL_SCRIPT_TEMP_NAME
igor@11 406 rm $PERL_SCRIPT_TEMP_NAME
igor@11 407 }
igor@11 408
igor@0 409 text_from_url()
igor@0 410 {
igor@0 411 lynx -dump "$1" | perl -p -e 's@http://[a-zA-Z&_.:/0-9%?=,#+()\[\]~-]*@@'
igor@0 412 }
igor@0 413
igor@2 414 add_marks()
igor@2 415 {
igor@2 416 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
igor@2 417 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@29 418 use Encode;
igor@29 419
igor@2 420 $file = $ARGV[0];
igor@2 421 our $dict;
igor@2 422 if (open(NOTES, $ENV{NOTES_FILE})) {
igor@2 423 while(<NOTES>) {
igor@29 424 $_ = decode( "utf8", $_);
igor@2 425 chomp;
igor@2 426 s/^\s+//;
igor@2 427 my ($a,$b)=split /\s+/,$_,2;
igor@2 428 $dict{$a}=$b;
igor@2 429 }
igor@2 430 }
igor@2 431 if (open(F, $file)) {
igor@2 432 @lines=<F>;
igor@2 433 close(F);
igor@29 434 for (@lines) {$_ = decode( "utf8", $_);};
igor@2 435
igor@2 436 if (open(F, ">$file")) {
igor@29 437 binmode F, ":utf8";
igor@2 438 for (@lines) {
igor@2 439 m/\s+\S+\s+(\S+)/;
igor@2 440 $name=$1;
igor@3 441 if (not /^#/ and defined($dict{$name})) {
igor@2 442 chomp;
igor@2 443 $mark=$dict{$name};
igor@2 444 $space=" "x(30-length($_));
igor@2 445 print F "$_$space$mark\n";
igor@2 446 }
igor@2 447 else {
igor@2 448 print F "$_";
igor@2 449 }
igor@2 450 }
igor@2 451 close(F);
igor@2 452 }
igor@2 453 }
igor@2 454 PERL_SCRIPT
igor@2 455 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
igor@2 456 export NOTES_FILE
igor@2 457 perl $PERL_SCRIPT_TEMP_NAME "$1"
igor@2 458 rm $PERL_SCRIPT_TEMP_NAME
igor@2 459 }
igor@2 460
igor@2 461 remove_marks()
igor@2 462 {
igor@2 463 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
igor@2 464 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@2 465 $file = $ARGV[0];
igor@2 466 our %dict;
igor@2 467 if (open(F, $file)) {
igor@2 468 @lines=<F>;
igor@2 469 close(F);
igor@2 470
igor@2 471 if (open(F, ">$file")) {
igor@2 472 for (@lines) {
igor@2 473 chomp;
igor@3 474 if (not /^#/ and m/(\s+)(\S+)(\s+)(\S+)(\s+)(.*)/) {
igor@2 475 my $name=$4;
igor@2 476 my $comment=$6;
igor@2 477 $dict{$name}=$comment;
igor@2 478 print F "$1$2$3$4\n";
igor@2 479 }
igor@2 480 else {
igor@2 481 print F "$_\n";
igor@2 482 }
igor@2 483 }
igor@2 484 }
igor@2 485 }
igor@21 486 if (($ENV{DONT_ADD_MARKS} ne "YES") and open(NOTES, $ENV{NOTES_FILE})) {
igor@2 487 @lines=<NOTES>;
igor@2 488 close(NOTES);
igor@2 489
igor@2 490 if (open(NOTES, ">".$ENV{NOTES_FILE})) {
igor@2 491 for (@lines) {
igor@2 492 chomp;
igor@2 493 s/^\s+//;
igor@2 494 my ($a,$b)=split /\s+/,$_,2;
igor@2 495 if (not defined($dict{$a}) || ($dict{$a} eq $b)) {
igor@2 496 print NOTES "$_\n";
igor@2 497 if (defined($dict{$a})) { unset($dict{$a}); }
igor@2 498 }
igor@2 499 }
igor@2 500 for (keys %dict) {
igor@2 501 $mark=$dict{$_};
igor@2 502 $space=" "x(30-length($_));
igor@2 503 print NOTES "$_$space$mark\n";
igor@2 504 }
igor@2 505 }
igor@2 506 }
igor@2 507 PERL_SCRIPT
igor@2 508 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
igor@2 509 export NOTES_FILE
igor@21 510 export DONT_ADD_MARKS
igor@2 511 perl $PERL_SCRIPT_TEMP_NAME "$1"
igor@2 512 rm $PERL_SCRIPT_TEMP_NAME
igor@2 513 }
igor@2 514
igor@25 515 part()
igor@25 516 {
igor@25 517 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-part-XXXXXXXX`
igor@25 518 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@25 519 #!/usr/bin/perl
igor@25 520
igor@25 521 my @lines=<STDIN>;
igor@25 522 my $lines=$#lines;
igor@25 523 my $interval=$ARGV[0];
igor@25 524 if (not $interval) {
igor@25 525 print @lines;
igor@25 526 }
igor@25 527 else {
igor@25 528 my ($start,$stop,$total);
igor@25 529 if ($interval =~ m@(.*)/(.*)@) {
igor@25 530 $start = $1;
igor@25 531 $total = $2;
igor@25 532 }
igor@25 533 else {
igor@25 534 $start=$interval;
igor@25 535 $total=0;
igor@25 536 }
igor@25 537 if ($start =~ m@(.*)-(.*)@) {
igor@25 538 $start = $1;
igor@25 539 $stop = $2;
igor@25 540 }
igor@25 541 if ($start =~ m@(.*)\+(.*)@) {
igor@25 542 $start = $1;
igor@25 543 $stop = $start+$2;
igor@25 544 }
igor@25 545
igor@25 546 $start=int($lines/$total*$start);
igor@25 547 $stop=int($lines/$total*$stop);
igor@25 548
igor@25 549 for($i=$start;$i<$stop;$i++){
igor@25 550 print $lines[$i];
igor@25 551 }
igor@25 552 }
igor@25 553 PERL_SCRIPT
igor@25 554 perl $PERL_SCRIPT_TEMP_NAME "$1"
igor@25 555 rm $PERL_SCRIPT_TEMP_NAME
igor@25 556 }
igor@25 557
igor@27 558 if [ "$TAGS_LIST_ONLY" = "YES" ]
igor@27 559 then
igor@27 560 cd "${WORK_DIR}"
igor@27 561 echo ${LANGUAGE}_*.txt | tr ' ' '\n' | grep -v '*' | sed 's/[^_]*_//;s/.txt$//'
igor@27 562 exit 0
igor@27 563 fi
igor@27 564
igor@27 565 tag_file_name()
igor@27 566 {
igor@27 567 echo "${LANGUAGE}_${1}.txt"
igor@27 568 }
igor@27 569
igor@27 570 if [ "$REMOVE_TAG" = "YES" ]
igor@27 571 then
igor@27 572 cd "${WORK_DIR}"
igor@27 573 for i in $TAG_NAME
igor@27 574 do
igor@27 575 echo "$TAGNAME" | grep -q '[/*?]' && continue
igor@27 576 f="`tag_file_name $i`"
igor@27 577 if [ -e "$f" ]
igor@27 578 then
igor@27 579 rm -f "$f" && echo Tag "'$i'" removed
igor@27 580 else
igor@27 581 echo Unknown tag "'$i'"
igor@27 582 fi
igor@27 583 done
igor@27 584 exit 0
igor@27 585 fi
igor@27 586
igor@0 587 mkdir -p $WORK_DIR
igor@8 588 oldpwd="$PWD"
igor@0 589 cd $WORK_DIR
igor@27 590 if [ "$MERGE_TAGGED_WORDS" = "YES" ]
igor@27 591 then
igor@27 592 VOC_FILES=''
igor@27 593 for i in $MERGE_THIS_TAGS
igor@27 594 do
igor@27 595 f=`tag_file_name $i`
igor@27 596 [ -e "$f" ] && VOC_FILES="${VOC_FILES} $f"
igor@27 597 done
igor@27 598 if [ -z "$VOC_FILES" ]
igor@27 599 then
igor@27 600 echo Unknown tags: $MERGE_THIS_TAGS > /dev/stderr
igor@27 601 else
igor@27 602 cat $VOC_FILES
igor@27 603 fi
igor@27 604 elif [ "$MERGE_ALL_TAGGED" = "YES" ]
igor@27 605 then
igor@27 606 cat ${LANGUAGE}_*.txt
igor@27 607 elif echo "$1" | grep -q http:
igor@0 608 then
igor@11 609 text_from_url "$1"
igor@0 610 elif [ "$#" != 0 ]
igor@0 611 then
igor@13 612 if echo $1 | grep -q ^/
igor@13 613 then
igor@13 614 cat "$1"
igor@13 615 else
igor@13 616 cat "$oldpwd/$1"
igor@13 617 fi
igor@0 618 else
igor@11 619 cat
igor@11 620 fi \
igor@25 621 | part $PART_TO_PROCESS \
igor@11 622 | tee $ORIGINAL_TEXT \
igor@26 623 | two_and_three_words \
igor@11 624 | get_words ${TEMP1}-full \
igor@11 625 | group_words \
igor@11 626 | add_stat ${TEMP1}-full \
igor@11 627 | tee "$TEMP1" > "$TEMP2"
igor@0 628
igor@16 629 if [ "$STAT_ONLY" = "YES" ]
igor@0 630 then
igor@16 631 cat "$TEMP1"
igor@21 632 elif [ "$NON_INTERACTIVE_MODE" = "YES" ]
igor@21 633 then
igor@21 634 cat "$TEMP1"
igor@0 635 else
igor@27 636 if [ `wc -l "$TEMP2" | awk '{print $1}'` != 0 ]
igor@16 637 then
igor@27 638 [ "$DONT_ADD_MARKS" = "YES" ] || add_marks "$TEMP2"
igor@27 639 if [ "$editor" = vim ]
igor@27 640 then
igor@27 641 vim -c 'set keywordprg='"$LANGUAGE" -c 'set iskeyword=@,48-57,/,.,-,_,+,,,#,$,%,~,=,48-255' "$TEMP2" < /dev/tty > /dev/tty
igor@27 642 else
igor@27 643 $editor "$TEMP2"
igor@27 644 fi
igor@27 645 remove_marks "$TEMP2"
igor@27 646
igor@27 647 vocabulary="$VOCABULARY"
igor@27 648 [ -n "$TAG_NAME" ] && vocabulary="`tag_file_name $TAG_NAME`"
igor@27 649 diff "$TEMP1" "$TEMP2" | awk '{print $3}' | sort -u >> "$vocabulary"
igor@16 650 fi
igor@0 651 fi
igor@2 652
igor@5 653 rm -f "$TEMP1" "$TEMP2" "${TEMP1}-full" "$ORIGINAL_TEXT"