new-words

annotate new-words.sh @ 44:7eb1a8c3eade

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