new-words

annotate new-words.sh @ 11:34d0332f238c

Группировка похожих слов (пока что только для английского языка).

Выполняется группировка похожих слов,
и слова сортируются не отдельно, а группами,
исходя из количества слов в группе.
В пределах группы сортировка выполняется по количеству вхождений
для каждого слова.
author Igor Chubin <igor@chub.in>
date Fri Apr 02 19:46:44 2010 +0300 (2010-04-02)
parents 0ff259b8b6a3
children 4bbe553c1ee2
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@11 106 sub normalize($)
igor@11 107 {
igor@11 108 $_=lc(shift);
igor@11 109 s///;
igor@11 110 s/s$//;
igor@11 111 s/ed$//;
igor@11 112 s/ing$//;
igor@11 113 return $_;
igor@11 114 }
igor@11 115
igor@11 116 sub compare($$)
igor@11 117 {
igor@11 118 my $a=shift;
igor@11 119 my $b=shift;
igor@11 120 $a =~ s/^\s*//;
igor@11 121 $b =~ s/^\s*//;
igor@11 122 my ($a1, $a2)= split /\s+/,$a,2;
igor@11 123 my ($b1, $b2)= split /\s+/,$b,2;
igor@11 124
igor@11 125 my $cmp = $group_weight{normalize($a2)} <=> $group_weight{normalize($b2)};
igor@11 126 if ($cmp) {
igor@11 127 return $cmp;
igor@11 128 }
igor@11 129 else {
igor@11 130 if (normalize($a2) ne normalize($b2)) {
igor@11 131 return normalize($a2) cmp normalize($b2);
igor@11 132 }
igor@11 133 else {
igor@11 134 return $a1 <=> $b1;
igor@11 135 }
igor@11 136 }
igor@11 137 }
igor@11 138
igor@11 139 @lines=<>;
igor@11 140 for $L (@lines) {
igor@11 141 chomp($L);
igor@11 142 $l=$L;
igor@11 143 $l =~ s/^\s*//;
igor@11 144 my ($a, $b)=split(/\s+/,$l,2);
igor@11 145 $group_weight{normalize($b)}+=$a;
igor@11 146 }
igor@11 147 @lines2 = sort { compare($b,$a) } @lines;
igor@11 148 for $l (@lines2) {
igor@11 149 print "$l\n";
igor@11 150 }
igor@11 151 PERL_SCRIPT
igor@11 152 perl $PERL_SCRIPT_TEMP_NAME
igor@11 153 rm $PERL_SCRIPT_TEMP_NAME
igor@11 154 }
igor@11 155
igor@0 156 text_from_url()
igor@0 157 {
igor@0 158 lynx -dump "$1" | perl -p -e 's@http://[a-zA-Z&_.:/0-9%?=,#+()\[\]~-]*@@'
igor@0 159 }
igor@0 160
igor@2 161 add_marks()
igor@2 162 {
igor@2 163 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
igor@2 164 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@2 165 $file = $ARGV[0];
igor@2 166 our $dict;
igor@2 167 if (open(NOTES, $ENV{NOTES_FILE})) {
igor@2 168 while(<NOTES>) {
igor@2 169 chomp;
igor@2 170 s/^\s+//;
igor@2 171 my ($a,$b)=split /\s+/,$_,2;
igor@2 172 $dict{$a}=$b;
igor@2 173 }
igor@2 174 }
igor@2 175 if (open(F, $file)) {
igor@2 176 @lines=<F>;
igor@2 177 close(F);
igor@2 178
igor@2 179 if (open(F, ">$file")) {
igor@2 180 for (@lines) {
igor@2 181 m/\s+\S+\s+(\S+)/;
igor@2 182 $name=$1;
igor@3 183 if (not /^#/ and defined($dict{$name})) {
igor@2 184 chomp;
igor@2 185 $mark=$dict{$name};
igor@2 186 $space=" "x(30-length($_));
igor@2 187 print F "$_$space$mark\n";
igor@2 188 }
igor@2 189 else {
igor@2 190 print F "$_";
igor@2 191 }
igor@2 192 }
igor@2 193 close(F);
igor@2 194 }
igor@2 195 }
igor@2 196 PERL_SCRIPT
igor@2 197 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
igor@2 198 export NOTES_FILE
igor@2 199 perl $PERL_SCRIPT_TEMP_NAME "$1"
igor@2 200 rm $PERL_SCRIPT_TEMP_NAME
igor@2 201 }
igor@2 202
igor@2 203 remove_marks()
igor@2 204 {
igor@2 205 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
igor@2 206 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@2 207 $file = $ARGV[0];
igor@2 208 our %dict;
igor@2 209 if (open(F, $file)) {
igor@2 210 @lines=<F>;
igor@2 211 close(F);
igor@2 212
igor@2 213 if (open(F, ">$file")) {
igor@2 214 for (@lines) {
igor@2 215 chomp;
igor@3 216 if (not /^#/ and m/(\s+)(\S+)(\s+)(\S+)(\s+)(.*)/) {
igor@2 217 my $name=$4;
igor@2 218 my $comment=$6;
igor@2 219 $dict{$name}=$comment;
igor@2 220 print F "$1$2$3$4\n";
igor@2 221 }
igor@2 222 else {
igor@2 223 print F "$_\n";
igor@2 224 }
igor@2 225 }
igor@2 226 }
igor@2 227 }
igor@2 228 if (open(NOTES, $ENV{NOTES_FILE})) {
igor@2 229 @lines=<NOTES>;
igor@2 230 close(NOTES);
igor@2 231
igor@2 232 if (open(NOTES, ">".$ENV{NOTES_FILE})) {
igor@2 233 for (@lines) {
igor@2 234 chomp;
igor@2 235 s/^\s+//;
igor@2 236 my ($a,$b)=split /\s+/,$_,2;
igor@2 237 if (not defined($dict{$a}) || ($dict{$a} eq $b)) {
igor@2 238 print NOTES "$_\n";
igor@2 239 if (defined($dict{$a})) { unset($dict{$a}); }
igor@2 240 }
igor@2 241 }
igor@2 242 for (keys %dict) {
igor@2 243 $mark=$dict{$_};
igor@2 244 $space=" "x(30-length($_));
igor@2 245 print NOTES "$_$space$mark\n";
igor@2 246 }
igor@2 247 }
igor@2 248 }
igor@2 249 PERL_SCRIPT
igor@2 250 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
igor@2 251 export NOTES_FILE
igor@2 252 perl $PERL_SCRIPT_TEMP_NAME "$1"
igor@2 253 rm $PERL_SCRIPT_TEMP_NAME
igor@2 254 }
igor@2 255
igor@0 256 mkdir -p $WORK_DIR
igor@8 257 oldpwd="$PWD"
igor@0 258 cd $WORK_DIR
igor@0 259 if echo "$1" | grep -q http:
igor@0 260 then
igor@11 261 text_from_url "$1"
igor@0 262 elif [ "$#" != 0 ]
igor@0 263 then
igor@11 264 cat "$oldpwd/$1"
igor@0 265 else
igor@11 266 cat
igor@11 267 fi \
igor@11 268 | tee $ORIGINAL_TEXT \
igor@11 269 | get_words ${TEMP1}-full \
igor@11 270 | group_words \
igor@11 271 | add_stat ${TEMP1}-full \
igor@11 272 | tee "$TEMP1" > "$TEMP2"
igor@0 273
igor@2 274 add_marks "$TEMP2"
igor@0 275 if [ "$editor" = vim ]
igor@0 276 then
igor@0 277 vim -c 'set keywordprg='"$LANGUAGE" -c 'set iskeyword=@,48-57,/,.,-,_,+,,,#,$,%,~,=' "$TEMP2" < /dev/tty > /dev/tty
igor@0 278 else
igor@0 279 echo 2
igor@0 280 $editor "$TEMP2"
igor@0 281 fi
igor@2 282 remove_marks "$TEMP2"
igor@2 283
igor@0 284 diff "$TEMP1" "$TEMP2" | awk '{print $3}' | sort -u >> "$VOCABULARY"
igor@5 285 rm -f "$TEMP1" "$TEMP2" "${TEMP1}-full" "$ORIGINAL_TEXT"