new-words

annotate new-words.sh @ 20:23f949c03f95

Usage information, oneliners prototype script, autoraising of the words that are similar to known
author Igor Chubin <igor@chub.in>
date Wed Apr 28 21:00:38 2010 +0300 (2010-04-28)
parents 416394a87d9f
children 190d4ac6b07c
rev   line source
igor@0 1 #!/bin/sh
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@20 16 -s show the text statistics (percentage of known words and so on) and exit
igor@16 17
igor@20 18 The language of the text can be specified also
igor@20 19 by name of the program new-words (correspondent link must be created before).
igor@20 20 For example, these calls are equivalent:
igor@0 21
igor@20 22 de-words URL
igor@0 23 new-words -l de URL
igor@0 24
igor@20 25 HELP
igor@20 26 }
igor@0 27
igor@20 28 if [ "$1" = "-h" ]
igor@20 29 then
igor@20 30 show_usage
igor@20 31 exit 0
igor@20 32 fi
igor@0 33
igor@0 34 WORK_DIR=~/.new-words/
igor@17 35 TEMP1=`mktemp /tmp/new-words-XXXXXXXXXX-temp1`
igor@17 36 TEMP2=`mktemp /tmp/new-words-XXXXXXXXXX-temp2`
igor@17 37 export ORIGINAL_TEXT=`mktemp /tmp/new-words-XXXXXXXXXX-orig`
igor@0 38 editor=${EDITOR:-vim}
igor@0 39
igor@2 40 LANGUAGE=en
igor@2 41 my_name="`echo $0 | sed s@.*/@@ | sed s/-.*// `"
igor@3 42 if echo "$1" | grep -q http://...wikipedia.org/wiki/
igor@3 43 then
igor@3 44 LANGUAGE="`echo $1 | sed s@http://@@ | sed s@.wikipedia.*@@`"
igor@3 45 fi
igor@2 46 [ "${my_name}" = "new" ] || LANGUAGE="$my_name"
igor@0 47 if [ "$1" = "-l" ]
igor@0 48 then
igor@0 49 LANGUAGE="$2"
igor@0 50 VOCABULARY="$LANGUAGE".txt
igor@0 51 shift 2
igor@0 52 fi
igor@2 53 VOCABULARY=${LANGUAGE}.txt
igor@2 54 NOTES_FILE=notes-${LANGUAGE}.txt
igor@0 55
igor@16 56 STAT_ONLY=NO
igor@16 57 if [ "$1" = "-s" ]
igor@16 58 then
igor@16 59 STAT_ONLY=YES
igor@16 60 shift
igor@16 61 fi
igor@16 62
igor@20 63 NEED_TO_USE_VOCABULARY_WHEN_SORT=NO
igor@20 64 if [ "$1" = "-k" ]
igor@20 65 then
igor@20 66 NEED_TO_USE_VOCABULARY_WHEN_SORT=YES
igor@20 67 shift
igor@20 68 fi
igor@20 69
igor@0 70 get_words()
igor@0 71 {
igor@1 72 tr ' ' '\n' | sed 's/--/ /g' \
igor@3 73 | sed "s/'/__APOSTROPHE__/g" \
igor@3 74 | tr '—·-' '-----' \
igor@3 75 | tr '*\r,.:#@()+=—<>$;"?!|·[]^%&' ' ' \
igor@3 76 | tr ' ' '\n' \
igor@3 77 | grep -x '[[:alpha:]'"'"'-]*' \
igor@3 78 | tee "$1" \
igor@3 79 | grep_v_english_perl \
igor@0 80 | sort | uniq -c | awk '{if ($2!="") print;}' | sort -rn
igor@0 81 }
igor@0 82
igor@3 83 add_stat()
igor@3 84 {
igor@3 85 before="$1"
igor@3 86 after=${before}2
igor@3 87 cat > "$after"
igor@3 88 total="`wc -w $1 | awk '{print $1}'`"
igor@3 89 total_unknown="`cat $after|awk '{s=s+$1}END{print s}'`"
igor@3 90 total_known="`echo $total-$total_unknown|bc`"
igor@3 91 percentage="`echo '100*('$total-$total_unknown')'/$total | bc -l | sed 's/\\.\(.\).*/.\1/'`"
igor@18 92 #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 93 sentences="`cat $ORIGINAL_TEXT | perl -e 'local $/; $_=<>; s/[^.]//msg; print length($_);'`"
igor@18 94
igor@18 95
igor@16 96 if [ "$STAT_ONLY" = "YES" ]
igor@16 97 then
igor@18 98 echo "LANG KNOWN% UNKNOWN% KNOWN TOTAL WPS UWPS*10"
igor@18 99 echo "$LANGUAGE $percentage `echo \(100-$percentage\) | bc -l` $total_known $total `echo $total/$sentences|bc` `echo 10*$total_unknown/$sentences|bc` "
igor@17 100 rm $after
igor@16 101 return 0
igor@16 102 else
igor@16 103 echo "# $LANGUAGE, $percentage, <$total_known/$total>"
igor@16 104 fi
igor@16 105
igor@14 106 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
igor@14 107 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@14 108 my $total=shift(@ARGV);
igor@14 109 my $total_known=shift(@ARGV);
igor@14 110 my $s=0;
igor@16 111 my $mark_line=int($total_known*100/$total/5)*5;
igor@19 112 if ($mark_line>=90) {
igor@19 113 $mark_line=int($total_known*100/$total)+1;
igor@19 114 } else { $mark_line +=5; };
igor@14 115 while(<>)
igor@14 116 {
igor@14 117 print;
igor@14 118 /^\s*([0-9]*)\s*/;
igor@14 119 $s+=$1;
igor@19 120 if (($total_known+$s)*100/$total>=$mark_line) {
igor@14 121 print "# $mark_line\n";
igor@14 122 if ($mark_line>=90) { $mark_line+=1; } else { $mark_line +=5; };
igor@14 123 }
igor@14 124 }
igor@14 125 PERL_SCRIPT
igor@14 126 perl $PERL_SCRIPT_TEMP_NAME "$total" "$total_known" "$after"
igor@14 127 rm $PERL_SCRIPT_TEMP_NAME
igor@3 128 rm $after
igor@3 129 }
igor@3 130
igor@0 131 grep_v_english()
igor@0 132 {
igor@0 133 [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
igor@0 134 eval $(cat $VOCABULARY | tr -d "'" | xargs -n10 echo | tr ' ' '|' | sed 's/^/egrep -xv "RRRRRRR|/' | sed 's/$/"/' | tr '\n' '|')cat
igor@0 135 }
igor@0 136
igor@0 137 grep_v_english_perl()
igor@0 138 {
igor@0 139 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
igor@0 140 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@0 141 open(VOC, $ENV{VOCABULARY})
igor@0 142 or die "Can't open VOCABULARY";
igor@0 143 while (<VOC>){
igor@0 144 chomp;
igor@3 145 #s/'//g;
igor@0 146 $voc{$_}="1";
igor@0 147 }
igor@0 148 while(<>) {
igor@0 149 chomp;
igor@0 150 if (not defined($voc{$_})) { print "$_\n"; }
igor@0 151 }
igor@0 152 PERL_SCRIPT
igor@0 153 [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
igor@0 154 export VOCABULARY
igor@0 155 perl $PERL_SCRIPT_TEMP_NAME
igor@0 156 rm $PERL_SCRIPT_TEMP_NAME
igor@0 157 }
igor@0 158
igor@11 159 group_words()
igor@11 160 {
igor@11 161 if [ "$LANGUAGE" != "en" ]
igor@11 162 then
igor@11 163 cat
igor@11 164 return
igor@11 165 fi
igor@11 166 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-group-words-XXXXXXXX`
igor@11 167 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@11 168 #!/usr/bin/perl
igor@11 169
igor@12 170 eval {
igor@12 171 # http://stackoverflow.com/questions/251694/how-can-i-check-if-i-have-a-perl-module-before-using-it
igor@12 172 require String::Similarity;
igor@12 173 String::Similarity->import();
igor@12 174 };
igor@12 175 unless($@)
igor@12 176 {
igor@12 177 our $HAVE_String_Similarity=1;
igor@12 178 }
igor@12 179
igor@12 180 sub similar($$){
igor@12 181 my $a=shift;
igor@12 182 my $b=shift;
igor@12 183 if ($HAVE_String_Similarity) {
igor@12 184 return $Similarity{"$a $b"};
igor@12 185 }
igor@12 186 else {
igor@12 187 return 0;
igor@12 188 }
igor@12 189 }
igor@12 190
igor@11 191 sub normalize($)
igor@11 192 {
igor@11 193 $_=lc(shift);
igor@12 194
igor@11 195 s/s$//;
igor@11 196 s/ed$//;
igor@12 197 s/en$//;
igor@12 198 s/er$//;
igor@12 199 s/est$//;
igor@11 200 s/ing$//;
igor@12 201
igor@12 202 s/ism$//; s/ist$//; s/ful$//; s/able$//; s/ably$//;
igor@16 203 s/ation$//; s/ness$//; s/ship$//; s/ally$//;
igor@12 204 s/ment$//; s/ify$//; s/ity$//; s/fy$//; s/ly$//;
igor@12 205 s/ise$//; s/ize$//;
igor@12 206
igor@12 207 s/e$//;
igor@11 208 return $_;
igor@11 209 }
igor@11 210
igor@12 211
igor@11 212 sub compare($$)
igor@11 213 {
igor@11 214 my $a=shift;
igor@11 215 my $b=shift;
igor@11 216 $a =~ s/^\s*//;
igor@11 217 $b =~ s/^\s*//;
igor@11 218 my ($a1, $a2)= split /\s+/,$a,2;
igor@11 219 my ($b1, $b2)= split /\s+/,$b,2;
igor@11 220
igor@11 221 my $cmp = $group_weight{normalize($a2)} <=> $group_weight{normalize($b2)};
igor@12 222
igor@11 223 if ($cmp) {
igor@11 224 return $cmp;
igor@11 225 }
igor@11 226 else {
igor@11 227 if (normalize($a2) ne normalize($b2)) {
igor@11 228 return normalize($a2) cmp normalize($b2);
igor@11 229 }
igor@11 230 else {
igor@11 231 return $a1 <=> $b1;
igor@11 232 }
igor@11 233 }
igor@11 234 }
igor@11 235
igor@20 236 our %Vocabulary;
igor@20 237 open(VOC, $ENV{VOCABULARY})
igor@20 238 or die "Can't open VOCABULARY";
igor@20 239 while (<VOC>){
igor@20 240 chomp;
igor@20 241 #s/'//g;
igor@20 242 $Vocabulary{normalize($_)}="1";
igor@20 243 }
igor@20 244 close(VOC);
igor@20 245
igor@20 246 @lines=<STDIN>;
igor@11 247 for $L (@lines) {
igor@11 248 chomp($L);
igor@11 249 $l=$L;
igor@11 250 $l =~ s/^\s*//;
igor@11 251 my ($a, $b)=split(/\s+/,$l,2);
igor@11 252 $group_weight{normalize($b)}+=$a;
igor@11 253 }
igor@20 254 if ($ENV{NEED_TO_USE_VOCABULARY_WHEN_SORT} eq "YES") {
igor@20 255 for $k (keys %group_weight) {
igor@20 256 if (defined($Vocabulary{$k})) {
igor@20 257 $group_weight{$k} *= 2;
igor@20 258 }
igor@20 259 }
igor@20 260 }
igor@11 261 @lines2 = sort { compare($b,$a) } @lines;
igor@11 262 for $l (@lines2) {
igor@11 263 print "$l\n";
igor@11 264 }
igor@11 265 PERL_SCRIPT
igor@20 266 export VOCABULARY
igor@20 267 export NEED_TO_USE_VOCABULARY_WHEN_SORT
igor@11 268 perl $PERL_SCRIPT_TEMP_NAME
igor@11 269 rm $PERL_SCRIPT_TEMP_NAME
igor@11 270 }
igor@11 271
igor@0 272 text_from_url()
igor@0 273 {
igor@0 274 lynx -dump "$1" | perl -p -e 's@http://[a-zA-Z&_.:/0-9%?=,#+()\[\]~-]*@@'
igor@0 275 }
igor@0 276
igor@2 277 add_marks()
igor@2 278 {
igor@2 279 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
igor@2 280 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@2 281 $file = $ARGV[0];
igor@2 282 our $dict;
igor@2 283 if (open(NOTES, $ENV{NOTES_FILE})) {
igor@2 284 while(<NOTES>) {
igor@2 285 chomp;
igor@2 286 s/^\s+//;
igor@2 287 my ($a,$b)=split /\s+/,$_,2;
igor@2 288 $dict{$a}=$b;
igor@2 289 }
igor@2 290 }
igor@2 291 if (open(F, $file)) {
igor@2 292 @lines=<F>;
igor@2 293 close(F);
igor@2 294
igor@2 295 if (open(F, ">$file")) {
igor@2 296 for (@lines) {
igor@2 297 m/\s+\S+\s+(\S+)/;
igor@2 298 $name=$1;
igor@3 299 if (not /^#/ and defined($dict{$name})) {
igor@2 300 chomp;
igor@2 301 $mark=$dict{$name};
igor@2 302 $space=" "x(30-length($_));
igor@2 303 print F "$_$space$mark\n";
igor@2 304 }
igor@2 305 else {
igor@2 306 print F "$_";
igor@2 307 }
igor@2 308 }
igor@2 309 close(F);
igor@2 310 }
igor@2 311 }
igor@2 312 PERL_SCRIPT
igor@2 313 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
igor@2 314 export NOTES_FILE
igor@2 315 perl $PERL_SCRIPT_TEMP_NAME "$1"
igor@2 316 rm $PERL_SCRIPT_TEMP_NAME
igor@2 317 }
igor@2 318
igor@2 319 remove_marks()
igor@2 320 {
igor@2 321 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
igor@2 322 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@2 323 $file = $ARGV[0];
igor@2 324 our %dict;
igor@2 325 if (open(F, $file)) {
igor@2 326 @lines=<F>;
igor@2 327 close(F);
igor@2 328
igor@2 329 if (open(F, ">$file")) {
igor@2 330 for (@lines) {
igor@2 331 chomp;
igor@3 332 if (not /^#/ and m/(\s+)(\S+)(\s+)(\S+)(\s+)(.*)/) {
igor@2 333 my $name=$4;
igor@2 334 my $comment=$6;
igor@2 335 $dict{$name}=$comment;
igor@2 336 print F "$1$2$3$4\n";
igor@2 337 }
igor@2 338 else {
igor@2 339 print F "$_\n";
igor@2 340 }
igor@2 341 }
igor@2 342 }
igor@2 343 }
igor@2 344 if (open(NOTES, $ENV{NOTES_FILE})) {
igor@2 345 @lines=<NOTES>;
igor@2 346 close(NOTES);
igor@2 347
igor@2 348 if (open(NOTES, ">".$ENV{NOTES_FILE})) {
igor@2 349 for (@lines) {
igor@2 350 chomp;
igor@2 351 s/^\s+//;
igor@2 352 my ($a,$b)=split /\s+/,$_,2;
igor@2 353 if (not defined($dict{$a}) || ($dict{$a} eq $b)) {
igor@2 354 print NOTES "$_\n";
igor@2 355 if (defined($dict{$a})) { unset($dict{$a}); }
igor@2 356 }
igor@2 357 }
igor@2 358 for (keys %dict) {
igor@2 359 $mark=$dict{$_};
igor@2 360 $space=" "x(30-length($_));
igor@2 361 print NOTES "$_$space$mark\n";
igor@2 362 }
igor@2 363 }
igor@2 364 }
igor@2 365 PERL_SCRIPT
igor@2 366 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
igor@2 367 export NOTES_FILE
igor@2 368 perl $PERL_SCRIPT_TEMP_NAME "$1"
igor@2 369 rm $PERL_SCRIPT_TEMP_NAME
igor@2 370 }
igor@2 371
igor@0 372 mkdir -p $WORK_DIR
igor@8 373 oldpwd="$PWD"
igor@0 374 cd $WORK_DIR
igor@0 375 if echo "$1" | grep -q http:
igor@0 376 then
igor@11 377 text_from_url "$1"
igor@0 378 elif [ "$#" != 0 ]
igor@0 379 then
igor@13 380 if echo $1 | grep -q ^/
igor@13 381 then
igor@13 382 cat "$1"
igor@13 383 else
igor@13 384 cat "$oldpwd/$1"
igor@13 385 fi
igor@0 386 else
igor@11 387 cat
igor@11 388 fi \
igor@11 389 | tee $ORIGINAL_TEXT \
igor@11 390 | get_words ${TEMP1}-full \
igor@11 391 | group_words \
igor@11 392 | add_stat ${TEMP1}-full \
igor@11 393 | tee "$TEMP1" > "$TEMP2"
igor@0 394
igor@16 395 if [ "$STAT_ONLY" = "YES" ]
igor@0 396 then
igor@16 397 cat "$TEMP1"
igor@0 398 else
igor@16 399 add_marks "$TEMP2"
igor@16 400 if [ "$editor" = vim ]
igor@16 401 then
igor@16 402 vim -c 'set keywordprg='"$LANGUAGE" -c 'set iskeyword=@,48-57,/,.,-,_,+,,,#,$,%,~,=' "$TEMP2" < /dev/tty > /dev/tty
igor@16 403 else
igor@16 404 echo 2
igor@16 405 $editor "$TEMP2"
igor@16 406 fi
igor@16 407 remove_marks "$TEMP2"
igor@0 408 fi
igor@2 409
igor@0 410 diff "$TEMP1" "$TEMP2" | awk '{print $3}' | sort -u >> "$VOCABULARY"
igor@5 411 rm -f "$TEMP1" "$TEMP2" "${TEMP1}-full" "$ORIGINAL_TEXT"