new-words
annotate new-words.sh @ 2:68722cd6faff
Добавление пометок к словам.
Справа от слова можно поставить пометку,
которая попадает в файл ~/.new-words/notes-LANG.txt.
Потом, при появлении этого слова,
она вспоминается и выводится вместе с ним.
Справа от слова можно поставить пометку,
которая попадает в файл ~/.new-words/notes-LANG.txt.
Потом, при появлении этого слова,
она вспоминается и выводится вместе с ним.
author | igor@book.xt.vpn |
---|---|
date | Fri Feb 26 21:41:27 2010 +0200 (2010-02-26) |
parents | 4c9076f87241 |
children | c703b8898696 |
rev | line source |
---|---|
igor@0 | 1 #!/bin/sh |
igor@0 | 2 |
igor@0 | 3 cat <<HELP > /dev/null |
igor@0 | 4 |
igor@0 | 5 Поддержка нескольких языков: |
igor@0 | 6 |
igor@0 | 7 new-words -l lang URL |
igor@0 | 8 |
igor@0 | 9 Например, для немецких текстов: |
igor@0 | 10 |
igor@0 | 11 new-words -l de URL |
igor@0 | 12 |
igor@0 | 13 Или, предварительно создав соответствующую ссылку: |
igor@0 | 14 |
igor@0 | 15 de-words URL |
igor@0 | 16 |
igor@0 | 17 HELP |
igor@0 | 18 |
igor@0 | 19 WORK_DIR=~/.new-words/ |
igor@0 | 20 TEMP1=`mktemp /tmp/news-words-XXXXXXXXXX` |
igor@0 | 21 TEMP2=`mktemp /tmp/news-words-XXXXXXXXXX` |
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@2 | 26 [ "${my_name}" = "new" ] || LANGUAGE="$my_name" |
igor@0 | 27 if [ "$1" = "-l" ] |
igor@0 | 28 then |
igor@0 | 29 LANGUAGE="$2" |
igor@0 | 30 VOCABULARY="$LANGUAGE".txt |
igor@0 | 31 shift 2 |
igor@0 | 32 fi |
igor@2 | 33 VOCABULARY=${LANGUAGE}.txt |
igor@2 | 34 NOTES_FILE=notes-${LANGUAGE}.txt |
igor@0 | 35 |
igor@0 | 36 get_words() |
igor@0 | 37 { |
igor@1 | 38 tr ' ' '\n' | sed 's/--/ /g' \ |
igor@0 | 39 | tr -d '*\r,.-:#@()+=—<>$;"?!|·[]^%&'"'" \ |
igor@0 | 40 | tr ' ' '\n' | grep_v_english_perl \ |
igor@1 | 41 | grep -x '[[:alnum:]]*' \ |
igor@0 | 42 | sort | uniq -c | awk '{if ($2!="") print;}' | sort -rn |
igor@0 | 43 } |
igor@0 | 44 |
igor@0 | 45 grep_v_english() |
igor@0 | 46 { |
igor@0 | 47 [ -e "$VOCABULARY" ] || touch "$VOCABULARY" |
igor@0 | 48 eval $(cat $VOCABULARY | tr -d "'" | xargs -n10 echo | tr ' ' '|' | sed 's/^/egrep -xv "RRRRRRR|/' | sed 's/$/"/' | tr '\n' '|')cat |
igor@0 | 49 } |
igor@0 | 50 |
igor@0 | 51 grep_v_english_perl() |
igor@0 | 52 { |
igor@0 | 53 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX` |
igor@0 | 54 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME |
igor@0 | 55 open(VOC, $ENV{VOCABULARY}) |
igor@0 | 56 or die "Can't open VOCABULARY"; |
igor@0 | 57 while (<VOC>){ |
igor@0 | 58 chomp; |
igor@0 | 59 s/'//g; |
igor@0 | 60 $voc{$_}="1"; |
igor@0 | 61 } |
igor@0 | 62 while(<>) { |
igor@0 | 63 chomp; |
igor@0 | 64 if (not defined($voc{$_})) { print "$_\n"; } |
igor@0 | 65 } |
igor@0 | 66 PERL_SCRIPT |
igor@0 | 67 [ -e "$VOCABULARY" ] || touch "$VOCABULARY" |
igor@0 | 68 export VOCABULARY |
igor@0 | 69 perl $PERL_SCRIPT_TEMP_NAME |
igor@0 | 70 rm $PERL_SCRIPT_TEMP_NAME |
igor@0 | 71 } |
igor@0 | 72 |
igor@0 | 73 text_from_url() |
igor@0 | 74 { |
igor@0 | 75 lynx -dump "$1" | perl -p -e 's@http://[a-zA-Z&_.:/0-9%?=,#+()\[\]~-]*@@' |
igor@0 | 76 } |
igor@0 | 77 |
igor@2 | 78 add_marks() |
igor@2 | 79 { |
igor@2 | 80 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX` |
igor@2 | 81 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME |
igor@2 | 82 $file = $ARGV[0]; |
igor@2 | 83 our $dict; |
igor@2 | 84 if (open(NOTES, $ENV{NOTES_FILE})) { |
igor@2 | 85 while(<NOTES>) { |
igor@2 | 86 chomp; |
igor@2 | 87 s/^\s+//; |
igor@2 | 88 my ($a,$b)=split /\s+/,$_,2; |
igor@2 | 89 $dict{$a}=$b; |
igor@2 | 90 } |
igor@2 | 91 } |
igor@2 | 92 if (open(F, $file)) { |
igor@2 | 93 @lines=<F>; |
igor@2 | 94 close(F); |
igor@2 | 95 |
igor@2 | 96 if (open(F, ">$file")) { |
igor@2 | 97 for (@lines) { |
igor@2 | 98 m/\s+\S+\s+(\S+)/; |
igor@2 | 99 $name=$1; |
igor@2 | 100 if (defined($dict{$name})) { |
igor@2 | 101 chomp; |
igor@2 | 102 $mark=$dict{$name}; |
igor@2 | 103 $space=" "x(30-length($_)); |
igor@2 | 104 print F "$_$space$mark\n"; |
igor@2 | 105 } |
igor@2 | 106 else { |
igor@2 | 107 print F "$_"; |
igor@2 | 108 } |
igor@2 | 109 } |
igor@2 | 110 close(F); |
igor@2 | 111 } |
igor@2 | 112 } |
igor@2 | 113 PERL_SCRIPT |
igor@2 | 114 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE" |
igor@2 | 115 export NOTES_FILE |
igor@2 | 116 perl $PERL_SCRIPT_TEMP_NAME "$1" |
igor@2 | 117 rm $PERL_SCRIPT_TEMP_NAME |
igor@2 | 118 } |
igor@2 | 119 |
igor@2 | 120 remove_marks() |
igor@2 | 121 { |
igor@2 | 122 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX` |
igor@2 | 123 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME |
igor@2 | 124 $file = $ARGV[0]; |
igor@2 | 125 our %dict; |
igor@2 | 126 if (open(F, $file)) { |
igor@2 | 127 @lines=<F>; |
igor@2 | 128 close(F); |
igor@2 | 129 |
igor@2 | 130 if (open(F, ">$file")) { |
igor@2 | 131 for (@lines) { |
igor@2 | 132 chomp; |
igor@2 | 133 if (m/(\s+)(\S+)(\s+)(\S+)(\s+)(.*)/) { |
igor@2 | 134 my $name=$4; |
igor@2 | 135 my $comment=$6; |
igor@2 | 136 $dict{$name}=$comment; |
igor@2 | 137 print F "$1$2$3$4\n"; |
igor@2 | 138 } |
igor@2 | 139 else { |
igor@2 | 140 print F "$_\n"; |
igor@2 | 141 } |
igor@2 | 142 } |
igor@2 | 143 } |
igor@2 | 144 } |
igor@2 | 145 if (open(NOTES, $ENV{NOTES_FILE})) { |
igor@2 | 146 @lines=<NOTES>; |
igor@2 | 147 close(NOTES); |
igor@2 | 148 |
igor@2 | 149 if (open(NOTES, ">".$ENV{NOTES_FILE})) { |
igor@2 | 150 for (@lines) { |
igor@2 | 151 chomp; |
igor@2 | 152 s/^\s+//; |
igor@2 | 153 my ($a,$b)=split /\s+/,$_,2; |
igor@2 | 154 if (not defined($dict{$a}) || ($dict{$a} eq $b)) { |
igor@2 | 155 print NOTES "$_\n"; |
igor@2 | 156 if (defined($dict{$a})) { unset($dict{$a}); } |
igor@2 | 157 } |
igor@2 | 158 } |
igor@2 | 159 for (keys %dict) { |
igor@2 | 160 $mark=$dict{$_}; |
igor@2 | 161 $space=" "x(30-length($_)); |
igor@2 | 162 print NOTES "$_$space$mark\n"; |
igor@2 | 163 } |
igor@2 | 164 } |
igor@2 | 165 } |
igor@2 | 166 PERL_SCRIPT |
igor@2 | 167 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE" |
igor@2 | 168 export NOTES_FILE |
igor@2 | 169 perl $PERL_SCRIPT_TEMP_NAME "$1" |
igor@2 | 170 rm $PERL_SCRIPT_TEMP_NAME |
igor@2 | 171 } |
igor@2 | 172 |
igor@0 | 173 mkdir -p $WORK_DIR |
igor@0 | 174 cd $WORK_DIR |
igor@0 | 175 if echo "$1" | grep -q http: |
igor@0 | 176 then |
igor@0 | 177 text_from_url "$1" | get_words | tee "$TEMP1" > "$TEMP2" |
igor@0 | 178 elif [ "$#" != 0 ] |
igor@0 | 179 then |
igor@0 | 180 cat "$1" | get_words | tee "$TEMP1" > "$TEMP2" |
igor@0 | 181 else |
igor@0 | 182 get_words | tee "$TEMP1" > "$TEMP2" |
igor@0 | 183 fi |
igor@0 | 184 |
igor@2 | 185 add_marks "$TEMP2" |
igor@0 | 186 if [ "$editor" = vim ] |
igor@0 | 187 then |
igor@0 | 188 vim -c 'set keywordprg='"$LANGUAGE" -c 'set iskeyword=@,48-57,/,.,-,_,+,,,#,$,%,~,=' "$TEMP2" < /dev/tty > /dev/tty |
igor@0 | 189 else |
igor@0 | 190 echo 2 |
igor@0 | 191 $editor "$TEMP2" |
igor@0 | 192 fi |
igor@2 | 193 remove_marks "$TEMP2" |
igor@2 | 194 |
igor@0 | 195 diff "$TEMP1" "$TEMP2" | awk '{print $3}' | sort -u >> "$VOCABULARY" |
igor@0 | 196 rm -f "$TEMP1" "$TEMP2" |