new-words

annotate new-words.sh @ 16:c65ffd60cc18

ключ -s: Информация об известных словах в тексте

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