new-words

annotate new-words.sh @ 7:57dc4ef40415

Added tag v0.1 for changeset d1851ee29a1a
author Igor Chubin <igor@chub.in>
date Wed Mar 31 18:56:56 2010 +0300 (2010-03-31)
parents c703b8898696
children 0ff259b8b6a3
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@0 95 text_from_url()
igor@0 96 {
igor@0 97 lynx -dump "$1" | perl -p -e 's@http://[a-zA-Z&_.:/0-9%?=,#+()\[\]~-]*@@'
igor@0 98 }
igor@0 99
igor@2 100 add_marks()
igor@2 101 {
igor@2 102 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
igor@2 103 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@2 104 $file = $ARGV[0];
igor@2 105 our $dict;
igor@2 106 if (open(NOTES, $ENV{NOTES_FILE})) {
igor@2 107 while(<NOTES>) {
igor@2 108 chomp;
igor@2 109 s/^\s+//;
igor@2 110 my ($a,$b)=split /\s+/,$_,2;
igor@2 111 $dict{$a}=$b;
igor@2 112 }
igor@2 113 }
igor@2 114 if (open(F, $file)) {
igor@2 115 @lines=<F>;
igor@2 116 close(F);
igor@2 117
igor@2 118 if (open(F, ">$file")) {
igor@2 119 for (@lines) {
igor@2 120 m/\s+\S+\s+(\S+)/;
igor@2 121 $name=$1;
igor@3 122 if (not /^#/ and defined($dict{$name})) {
igor@2 123 chomp;
igor@2 124 $mark=$dict{$name};
igor@2 125 $space=" "x(30-length($_));
igor@2 126 print F "$_$space$mark\n";
igor@2 127 }
igor@2 128 else {
igor@2 129 print F "$_";
igor@2 130 }
igor@2 131 }
igor@2 132 close(F);
igor@2 133 }
igor@2 134 }
igor@2 135 PERL_SCRIPT
igor@2 136 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
igor@2 137 export NOTES_FILE
igor@2 138 perl $PERL_SCRIPT_TEMP_NAME "$1"
igor@2 139 rm $PERL_SCRIPT_TEMP_NAME
igor@2 140 }
igor@2 141
igor@2 142 remove_marks()
igor@2 143 {
igor@2 144 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
igor@2 145 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
igor@2 146 $file = $ARGV[0];
igor@2 147 our %dict;
igor@2 148 if (open(F, $file)) {
igor@2 149 @lines=<F>;
igor@2 150 close(F);
igor@2 151
igor@2 152 if (open(F, ">$file")) {
igor@2 153 for (@lines) {
igor@2 154 chomp;
igor@3 155 if (not /^#/ and m/(\s+)(\S+)(\s+)(\S+)(\s+)(.*)/) {
igor@2 156 my $name=$4;
igor@2 157 my $comment=$6;
igor@2 158 $dict{$name}=$comment;
igor@2 159 print F "$1$2$3$4\n";
igor@2 160 }
igor@2 161 else {
igor@2 162 print F "$_\n";
igor@2 163 }
igor@2 164 }
igor@2 165 }
igor@2 166 }
igor@2 167 if (open(NOTES, $ENV{NOTES_FILE})) {
igor@2 168 @lines=<NOTES>;
igor@2 169 close(NOTES);
igor@2 170
igor@2 171 if (open(NOTES, ">".$ENV{NOTES_FILE})) {
igor@2 172 for (@lines) {
igor@2 173 chomp;
igor@2 174 s/^\s+//;
igor@2 175 my ($a,$b)=split /\s+/,$_,2;
igor@2 176 if (not defined($dict{$a}) || ($dict{$a} eq $b)) {
igor@2 177 print NOTES "$_\n";
igor@2 178 if (defined($dict{$a})) { unset($dict{$a}); }
igor@2 179 }
igor@2 180 }
igor@2 181 for (keys %dict) {
igor@2 182 $mark=$dict{$_};
igor@2 183 $space=" "x(30-length($_));
igor@2 184 print NOTES "$_$space$mark\n";
igor@2 185 }
igor@2 186 }
igor@2 187 }
igor@2 188 PERL_SCRIPT
igor@2 189 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
igor@2 190 export NOTES_FILE
igor@2 191 perl $PERL_SCRIPT_TEMP_NAME "$1"
igor@2 192 rm $PERL_SCRIPT_TEMP_NAME
igor@2 193 }
igor@2 194
igor@0 195 mkdir -p $WORK_DIR
igor@0 196 cd $WORK_DIR
igor@0 197 if echo "$1" | grep -q http:
igor@0 198 then
igor@5 199 text_from_url "$1" | tee $ORIGINAL_TEXT \
igor@5 200 | get_words ${TEMP1}-full | add_stat ${TEMP1}-full| tee "$TEMP1" > "$TEMP2"
igor@0 201 elif [ "$#" != 0 ]
igor@0 202 then
igor@5 203 cat "$1" | tee $ORIGINAL_TEXT \
igor@5 204 | get_words ${TEMP1}-full | add_stat ${TEMP1}-full | tee "$TEMP1" > "$TEMP2"
igor@0 205 else
igor@5 206 tee $ORIGINAL_TEXT \
igor@5 207 | get_words ${TEMP1}-full | add_stat ${TEMP1}-full | tee "$TEMP1" > "$TEMP2"
igor@0 208 fi
igor@0 209
igor@2 210 add_marks "$TEMP2"
igor@0 211 if [ "$editor" = vim ]
igor@0 212 then
igor@0 213 vim -c 'set keywordprg='"$LANGUAGE" -c 'set iskeyword=@,48-57,/,.,-,_,+,,,#,$,%,~,=' "$TEMP2" < /dev/tty > /dev/tty
igor@0 214 else
igor@0 215 echo 2
igor@0 216 $editor "$TEMP2"
igor@0 217 fi
igor@2 218 remove_marks "$TEMP2"
igor@2 219
igor@0 220 diff "$TEMP1" "$TEMP2" | awk '{print $3}' | sort -u >> "$VOCABULARY"
igor@5 221 rm -f "$TEMP1" "$TEMP2" "${TEMP1}-full" "$ORIGINAL_TEXT"