new-words

annotate new-words.sh @ 66:53ba2847501f

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