new-words

annotate new-words.sh @ 12:4bbe553c1ee2

Удаление суффиксов в английских словах.

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