new-words

annotate new-words.sh @ 19:416394a87d9f

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