new-words

annotate new-words.sh @ 34:3827cce83602

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