new-words

annotate new-words.sh @ 3:c703b8898696

Комментарий-министатистика, дефис в словах, автоматический выбор языка для википедии.

* Комментарий с процентом известных слов,
* Автоматическое определение языка по названию страницы в википедии (только двухбуквенный),
* Слова с дефисом.
author igor@book.xt.vpn
date Tue Mar 02 22:28:27 2010 +0200 (2010-03-02)
parents 68722cd6faff
children 9345cc05fdd1
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@0 21 editor=${EDITOR:-vim}
igor@0 22
igor@2 23 LANGUAGE=en
igor@2 24 my_name="`echo $0 | sed s@.*/@@ | sed s/-.*// `"
igor@3 25 if echo "$1" | grep -q http://...wikipedia.org/wiki/
igor@3 26 then
igor@3 27 LANGUAGE="`echo $1 | sed s@http://@@ | sed s@.wikipedia.*@@`"
igor@3 28 fi
igor@2 29 [ "${my_name}" = "new" ] || LANGUAGE="$my_name"
igor@0 30 if [ "$1" = "-l" ]
igor@0 31 then
igor@0 32 LANGUAGE="$2"
igor@0 33 VOCABULARY="$LANGUAGE".txt
igor@0 34 shift 2
igor@0 35 fi
igor@2 36 VOCABULARY=${LANGUAGE}.txt
igor@2 37 NOTES_FILE=notes-${LANGUAGE}.txt
igor@0 38
igor@0 39 get_words()
igor@0 40 {
igor@1 41 tr ' ' '\n' | sed 's/--/ /g' \
igor@3 42 | sed "s/'/__APOSTROPHE__/g" \
igor@3 43 | tr '—·-' '-----' \
igor@3 44 | tr '*\r,.:#@()+=—<>$;"?!|·[]^%&' ' ' \
igor@3 45 | tr ' ' '\n' \
igor@3 46 | grep -x '[[:alpha:]'"'"'-]*' \
igor@3 47 | tee "$1" \
igor@3 48 | grep_v_english_perl \
igor@0 49 | sort | uniq -c | awk '{if ($2!="") print;}' | sort -rn
igor@0 50 }
igor@0 51
igor@3 52 add_stat()
igor@3 53 {
igor@3 54 before="$1"
igor@3 55 after=${before}2
igor@3 56 cat > "$after"
igor@3 57 total="`wc -w $1 | awk '{print $1}'`"
igor@3 58 total_unknown="`cat $after|awk '{s=s+$1}END{print s}'`"
igor@3 59 total_known="`echo $total-$total_unknown|bc`"
igor@3 60 percentage="`echo '100*('$total-$total_unknown')'/$total | bc -l | sed 's/\\.\(.\).*/.\1/'`"
igor@3 61 echo "# $LANGUAGE, $percentage, <$total_known/$total>"
igor@3 62 cat "$after"
igor@3 63 rm $after
igor@3 64 }
igor@3 65
igor@0 66 grep_v_english()
igor@0 67 {
igor@0 68 [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
igor@0 69 eval $(cat $VOCABULARY | tr -d "'" | xargs -n10 echo | tr ' ' '|' | sed 's/^/egrep -xv "RRRRRRR|/' | sed 's/$/"/' | tr '\n' '|')cat
igor@0 70 }
igor@0 71
igor@0 72 grep_v_english_perl()
igor@0 73 {
igor@0 74 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
igor@0 75 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@0 76 open(VOC, $ENV{VOCABULARY})
igor@0 77 or die "Can't open VOCABULARY";
igor@0 78 while (<VOC>){
igor@0 79 chomp;
igor@3 80 #s/'//g;
igor@0 81 $voc{$_}="1";
igor@0 82 }
igor@0 83 while(<>) {
igor@0 84 chomp;
igor@0 85 if (not defined($voc{$_})) { print "$_\n"; }
igor@0 86 }
igor@0 87 PERL_SCRIPT
igor@0 88 [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
igor@0 89 export VOCABULARY
igor@0 90 perl $PERL_SCRIPT_TEMP_NAME
igor@0 91 rm $PERL_SCRIPT_TEMP_NAME
igor@0 92 }
igor@0 93
igor@0 94 text_from_url()
igor@0 95 {
igor@0 96 lynx -dump "$1" | perl -p -e 's@http://[a-zA-Z&_.:/0-9%?=,#+()\[\]~-]*@@'
igor@0 97 }
igor@0 98
igor@2 99 add_marks()
igor@2 100 {
igor@2 101 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
igor@2 102 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@2 103 $file = $ARGV[0];
igor@2 104 our $dict;
igor@2 105 if (open(NOTES, $ENV{NOTES_FILE})) {
igor@2 106 while(<NOTES>) {
igor@2 107 chomp;
igor@2 108 s/^\s+//;
igor@2 109 my ($a,$b)=split /\s+/,$_,2;
igor@2 110 $dict{$a}=$b;
igor@2 111 }
igor@2 112 }
igor@2 113 if (open(F, $file)) {
igor@2 114 @lines=<F>;
igor@2 115 close(F);
igor@2 116
igor@2 117 if (open(F, ">$file")) {
igor@2 118 for (@lines) {
igor@2 119 m/\s+\S+\s+(\S+)/;
igor@2 120 $name=$1;
igor@3 121 if (not /^#/ and defined($dict{$name})) {
igor@2 122 chomp;
igor@2 123 $mark=$dict{$name};
igor@2 124 $space=" "x(30-length($_));
igor@2 125 print F "$_$space$mark\n";
igor@2 126 }
igor@2 127 else {
igor@2 128 print F "$_";
igor@2 129 }
igor@2 130 }
igor@2 131 close(F);
igor@2 132 }
igor@2 133 }
igor@2 134 PERL_SCRIPT
igor@2 135 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
igor@2 136 export NOTES_FILE
igor@2 137 perl $PERL_SCRIPT_TEMP_NAME "$1"
igor@2 138 rm $PERL_SCRIPT_TEMP_NAME
igor@2 139 }
igor@2 140
igor@2 141 remove_marks()
igor@2 142 {
igor@2 143 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
igor@2 144 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@2 145 $file = $ARGV[0];
igor@2 146 our %dict;
igor@2 147 if (open(F, $file)) {
igor@2 148 @lines=<F>;
igor@2 149 close(F);
igor@2 150
igor@2 151 if (open(F, ">$file")) {
igor@2 152 for (@lines) {
igor@2 153 chomp;
igor@3 154 if (not /^#/ and m/(\s+)(\S+)(\s+)(\S+)(\s+)(.*)/) {
igor@2 155 my $name=$4;
igor@2 156 my $comment=$6;
igor@2 157 $dict{$name}=$comment;
igor@2 158 print F "$1$2$3$4\n";
igor@2 159 }
igor@2 160 else {
igor@2 161 print F "$_\n";
igor@2 162 }
igor@2 163 }
igor@2 164 }
igor@2 165 }
igor@2 166 if (open(NOTES, $ENV{NOTES_FILE})) {
igor@2 167 @lines=<NOTES>;
igor@2 168 close(NOTES);
igor@2 169
igor@2 170 if (open(NOTES, ">".$ENV{NOTES_FILE})) {
igor@2 171 for (@lines) {
igor@2 172 chomp;
igor@2 173 s/^\s+//;
igor@2 174 my ($a,$b)=split /\s+/,$_,2;
igor@2 175 if (not defined($dict{$a}) || ($dict{$a} eq $b)) {
igor@2 176 print NOTES "$_\n";
igor@2 177 if (defined($dict{$a})) { unset($dict{$a}); }
igor@2 178 }
igor@2 179 }
igor@2 180 for (keys %dict) {
igor@2 181 $mark=$dict{$_};
igor@2 182 $space=" "x(30-length($_));
igor@2 183 print NOTES "$_$space$mark\n";
igor@2 184 }
igor@2 185 }
igor@2 186 }
igor@2 187 PERL_SCRIPT
igor@2 188 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
igor@2 189 export NOTES_FILE
igor@2 190 perl $PERL_SCRIPT_TEMP_NAME "$1"
igor@2 191 rm $PERL_SCRIPT_TEMP_NAME
igor@2 192 }
igor@2 193
igor@0 194 mkdir -p $WORK_DIR
igor@0 195 cd $WORK_DIR
igor@0 196 if echo "$1" | grep -q http:
igor@0 197 then
igor@3 198 text_from_url "$1" | get_words ${TEMP1}-full | add_stat ${TEMP1}-full| tee "$TEMP1" > "$TEMP2"
igor@0 199 elif [ "$#" != 0 ]
igor@0 200 then
igor@3 201 cat "$1" | get_words ${TEMP1}-full | add_stat ${TEMP1}-full | tee "$TEMP1" > "$TEMP2"
igor@0 202 else
igor@3 203 get_words ${TEMP1}-full| add_stat ${TEMP1}-full | tee "$TEMP1" > "$TEMP2"
igor@0 204 fi
igor@0 205
igor@2 206 add_marks "$TEMP2"
igor@0 207 if [ "$editor" = vim ]
igor@0 208 then
igor@0 209 vim -c 'set keywordprg='"$LANGUAGE" -c 'set iskeyword=@,48-57,/,.,-,_,+,,,#,$,%,~,=' "$TEMP2" < /dev/tty > /dev/tty
igor@0 210 else
igor@0 211 echo 2
igor@0 212 $editor "$TEMP2"
igor@0 213 fi
igor@2 214 remove_marks "$TEMP2"
igor@2 215
igor@0 216 diff "$TEMP1" "$TEMP2" | awk '{print $3}' | sort -u >> "$VOCABULARY"
igor@3 217 rm -f "$TEMP1" "$TEMP2" "${TEMP1}-full"