new-words

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