new-words

annotate new-words.sh @ 14:9b18c7efe31c

Километровые столбы — процентные отметки.

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