new-words

annotate new-words.sh @ 18:7e3a52db54ad

Среднее количество слов и неизвестных слов (*10) в предложении в статистике.

Пример использования:

LANG KNOWN% UNKNOWN% KNOWN TOTAL WPS UWPS*10
en 89.8 10.2 167021 185840 21 21
author Igor Chubin <igor@chub.in>
date Mon Apr 05 21:34:55 2010 +0300 (2010-04-05)
parents 35eeaf2620ce
children 416394a87d9f
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@16 98 if ($mark_line>=90) { $mark_line+=1; } else { $mark_line +=5; };
igor@14 99 while(<>)
igor@14 100 {
igor@14 101 print;
igor@14 102 /^\s*([0-9]*)\s*/;
igor@14 103 $s+=$1;
igor@14 104 if (int(($total_known+$s)*100/$total)>=$mark_line) {
igor@14 105 print "# $mark_line\n";
igor@14 106 if ($mark_line>=90) { $mark_line+=1; } else { $mark_line +=5; };
igor@14 107 }
igor@14 108 }
igor@14 109 PERL_SCRIPT
igor@14 110 perl $PERL_SCRIPT_TEMP_NAME "$total" "$total_known" "$after"
igor@14 111 rm $PERL_SCRIPT_TEMP_NAME
igor@3 112 rm $after
igor@3 113 }
igor@3 114
igor@0 115 grep_v_english()
igor@0 116 {
igor@0 117 [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
igor@0 118 eval $(cat $VOCABULARY | tr -d "'" | xargs -n10 echo | tr ' ' '|' | sed 's/^/egrep -xv "RRRRRRR|/' | sed 's/$/"/' | tr '\n' '|')cat
igor@0 119 }
igor@0 120
igor@0 121 grep_v_english_perl()
igor@0 122 {
igor@0 123 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
igor@0 124 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@0 125 open(VOC, $ENV{VOCABULARY})
igor@0 126 or die "Can't open VOCABULARY";
igor@0 127 while (<VOC>){
igor@0 128 chomp;
igor@3 129 #s/'//g;
igor@0 130 $voc{$_}="1";
igor@0 131 }
igor@0 132 while(<>) {
igor@0 133 chomp;
igor@0 134 if (not defined($voc{$_})) { print "$_\n"; }
igor@0 135 }
igor@0 136 PERL_SCRIPT
igor@0 137 [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
igor@0 138 export VOCABULARY
igor@0 139 perl $PERL_SCRIPT_TEMP_NAME
igor@0 140 rm $PERL_SCRIPT_TEMP_NAME
igor@0 141 }
igor@0 142
igor@11 143 group_words()
igor@11 144 {
igor@11 145 if [ "$LANGUAGE" != "en" ]
igor@11 146 then
igor@11 147 cat
igor@11 148 return
igor@11 149 fi
igor@11 150 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-group-words-XXXXXXXX`
igor@11 151 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@11 152 #!/usr/bin/perl
igor@11 153
igor@12 154 eval {
igor@12 155 # http://stackoverflow.com/questions/251694/how-can-i-check-if-i-have-a-perl-module-before-using-it
igor@12 156 require String::Similarity;
igor@12 157 String::Similarity->import();
igor@12 158 };
igor@12 159 unless($@)
igor@12 160 {
igor@12 161 our $HAVE_String_Similarity=1;
igor@12 162 }
igor@12 163
igor@12 164 sub similar($$){
igor@12 165 my $a=shift;
igor@12 166 my $b=shift;
igor@12 167 if ($HAVE_String_Similarity) {
igor@12 168 return $Similarity{"$a $b"};
igor@12 169 }
igor@12 170 else {
igor@12 171 return 0;
igor@12 172 }
igor@12 173 }
igor@12 174
igor@11 175 sub normalize($)
igor@11 176 {
igor@11 177 $_=lc(shift);
igor@12 178
igor@11 179 s/s$//;
igor@11 180 s/ed$//;
igor@12 181 s/en$//;
igor@12 182 s/er$//;
igor@12 183 s/est$//;
igor@11 184 s/ing$//;
igor@12 185
igor@12 186 s/ism$//; s/ist$//; s/ful$//; s/able$//; s/ably$//;
igor@16 187 s/ation$//; s/ness$//; s/ship$//; s/ally$//;
igor@12 188 s/ment$//; s/ify$//; s/ity$//; s/fy$//; s/ly$//;
igor@12 189 s/ise$//; s/ize$//;
igor@12 190
igor@12 191 s/e$//;
igor@11 192 return $_;
igor@11 193 }
igor@11 194
igor@12 195
igor@11 196 sub compare($$)
igor@11 197 {
igor@11 198 my $a=shift;
igor@11 199 my $b=shift;
igor@11 200 $a =~ s/^\s*//;
igor@11 201 $b =~ s/^\s*//;
igor@11 202 my ($a1, $a2)= split /\s+/,$a,2;
igor@11 203 my ($b1, $b2)= split /\s+/,$b,2;
igor@11 204
igor@11 205 my $cmp = $group_weight{normalize($a2)} <=> $group_weight{normalize($b2)};
igor@12 206
igor@11 207 if ($cmp) {
igor@11 208 return $cmp;
igor@11 209 }
igor@11 210 else {
igor@11 211 if (normalize($a2) ne normalize($b2)) {
igor@11 212 return normalize($a2) cmp normalize($b2);
igor@11 213 }
igor@11 214 else {
igor@11 215 return $a1 <=> $b1;
igor@11 216 }
igor@11 217 }
igor@11 218 }
igor@11 219
igor@11 220 @lines=<>;
igor@11 221 for $L (@lines) {
igor@11 222 chomp($L);
igor@11 223 $l=$L;
igor@11 224 $l =~ s/^\s*//;
igor@11 225 my ($a, $b)=split(/\s+/,$l,2);
igor@11 226 $group_weight{normalize($b)}+=$a;
igor@11 227 }
igor@11 228 @lines2 = sort { compare($b,$a) } @lines;
igor@11 229 for $l (@lines2) {
igor@11 230 print "$l\n";
igor@11 231 }
igor@11 232 PERL_SCRIPT
igor@11 233 perl $PERL_SCRIPT_TEMP_NAME
igor@11 234 rm $PERL_SCRIPT_TEMP_NAME
igor@11 235 }
igor@11 236
igor@0 237 text_from_url()
igor@0 238 {
igor@0 239 lynx -dump "$1" | perl -p -e 's@http://[a-zA-Z&_.:/0-9%?=,#+()\[\]~-]*@@'
igor@0 240 }
igor@0 241
igor@2 242 add_marks()
igor@2 243 {
igor@2 244 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
igor@2 245 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@2 246 $file = $ARGV[0];
igor@2 247 our $dict;
igor@2 248 if (open(NOTES, $ENV{NOTES_FILE})) {
igor@2 249 while(<NOTES>) {
igor@2 250 chomp;
igor@2 251 s/^\s+//;
igor@2 252 my ($a,$b)=split /\s+/,$_,2;
igor@2 253 $dict{$a}=$b;
igor@2 254 }
igor@2 255 }
igor@2 256 if (open(F, $file)) {
igor@2 257 @lines=<F>;
igor@2 258 close(F);
igor@2 259
igor@2 260 if (open(F, ">$file")) {
igor@2 261 for (@lines) {
igor@2 262 m/\s+\S+\s+(\S+)/;
igor@2 263 $name=$1;
igor@3 264 if (not /^#/ and defined($dict{$name})) {
igor@2 265 chomp;
igor@2 266 $mark=$dict{$name};
igor@2 267 $space=" "x(30-length($_));
igor@2 268 print F "$_$space$mark\n";
igor@2 269 }
igor@2 270 else {
igor@2 271 print F "$_";
igor@2 272 }
igor@2 273 }
igor@2 274 close(F);
igor@2 275 }
igor@2 276 }
igor@2 277 PERL_SCRIPT
igor@2 278 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
igor@2 279 export NOTES_FILE
igor@2 280 perl $PERL_SCRIPT_TEMP_NAME "$1"
igor@2 281 rm $PERL_SCRIPT_TEMP_NAME
igor@2 282 }
igor@2 283
igor@2 284 remove_marks()
igor@2 285 {
igor@2 286 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
igor@2 287 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@2 288 $file = $ARGV[0];
igor@2 289 our %dict;
igor@2 290 if (open(F, $file)) {
igor@2 291 @lines=<F>;
igor@2 292 close(F);
igor@2 293
igor@2 294 if (open(F, ">$file")) {
igor@2 295 for (@lines) {
igor@2 296 chomp;
igor@3 297 if (not /^#/ and m/(\s+)(\S+)(\s+)(\S+)(\s+)(.*)/) {
igor@2 298 my $name=$4;
igor@2 299 my $comment=$6;
igor@2 300 $dict{$name}=$comment;
igor@2 301 print F "$1$2$3$4\n";
igor@2 302 }
igor@2 303 else {
igor@2 304 print F "$_\n";
igor@2 305 }
igor@2 306 }
igor@2 307 }
igor@2 308 }
igor@2 309 if (open(NOTES, $ENV{NOTES_FILE})) {
igor@2 310 @lines=<NOTES>;
igor@2 311 close(NOTES);
igor@2 312
igor@2 313 if (open(NOTES, ">".$ENV{NOTES_FILE})) {
igor@2 314 for (@lines) {
igor@2 315 chomp;
igor@2 316 s/^\s+//;
igor@2 317 my ($a,$b)=split /\s+/,$_,2;
igor@2 318 if (not defined($dict{$a}) || ($dict{$a} eq $b)) {
igor@2 319 print NOTES "$_\n";
igor@2 320 if (defined($dict{$a})) { unset($dict{$a}); }
igor@2 321 }
igor@2 322 }
igor@2 323 for (keys %dict) {
igor@2 324 $mark=$dict{$_};
igor@2 325 $space=" "x(30-length($_));
igor@2 326 print NOTES "$_$space$mark\n";
igor@2 327 }
igor@2 328 }
igor@2 329 }
igor@2 330 PERL_SCRIPT
igor@2 331 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
igor@2 332 export NOTES_FILE
igor@2 333 perl $PERL_SCRIPT_TEMP_NAME "$1"
igor@2 334 rm $PERL_SCRIPT_TEMP_NAME
igor@2 335 }
igor@2 336
igor@0 337 mkdir -p $WORK_DIR
igor@8 338 oldpwd="$PWD"
igor@0 339 cd $WORK_DIR
igor@0 340 if echo "$1" | grep -q http:
igor@0 341 then
igor@11 342 text_from_url "$1"
igor@0 343 elif [ "$#" != 0 ]
igor@0 344 then
igor@13 345 if echo $1 | grep -q ^/
igor@13 346 then
igor@13 347 cat "$1"
igor@13 348 else
igor@13 349 cat "$oldpwd/$1"
igor@13 350 fi
igor@0 351 else
igor@11 352 cat
igor@11 353 fi \
igor@11 354 | tee $ORIGINAL_TEXT \
igor@11 355 | get_words ${TEMP1}-full \
igor@11 356 | group_words \
igor@11 357 | add_stat ${TEMP1}-full \
igor@11 358 | tee "$TEMP1" > "$TEMP2"
igor@0 359
igor@16 360 if [ "$STAT_ONLY" = "YES" ]
igor@0 361 then
igor@16 362 cat "$TEMP1"
igor@0 363 else
igor@16 364 add_marks "$TEMP2"
igor@16 365 if [ "$editor" = vim ]
igor@16 366 then
igor@16 367 vim -c 'set keywordprg='"$LANGUAGE" -c 'set iskeyword=@,48-57,/,.,-,_,+,,,#,$,%,~,=' "$TEMP2" < /dev/tty > /dev/tty
igor@16 368 else
igor@16 369 echo 2
igor@16 370 $editor "$TEMP2"
igor@16 371 fi
igor@16 372 remove_marks "$TEMP2"
igor@0 373 fi
igor@2 374
igor@0 375 diff "$TEMP1" "$TEMP2" | awk '{print $3}' | sort -u >> "$VOCABULARY"
igor@5 376 rm -f "$TEMP1" "$TEMP2" "${TEMP1}-full" "$ORIGINAL_TEXT"