new-words
view new-words.sh @ 17:35eeaf2620ce
minifix
author | Igor Chubin <igor@chub.in> |
---|---|
date | Sun Apr 04 20:11:22 2010 +0300 (2010-04-04) |
parents | c65ffd60cc18 |
children | 7e3a52db54ad |
line source
1 #!/bin/sh
2 cat <<HELP > /dev/null
4 USAGE:
6 new-words [ -l lang ] [ -s ] [ ARG ]
8 SWITCHES:
10 -s show text statistics and exit
11 -l lang override language settings
13 Поддержка нескольких языков:
15 new-words -l lang URL
17 Например, для немецких текстов:
19 new-words -l de URL
21 Или, предварительно создав соответствующую ссылку:
23 de-words URL
25 HELP
27 WORK_DIR=~/.new-words/
28 TEMP1=`mktemp /tmp/new-words-XXXXXXXXXX-temp1`
29 TEMP2=`mktemp /tmp/new-words-XXXXXXXXXX-temp2`
30 export ORIGINAL_TEXT=`mktemp /tmp/new-words-XXXXXXXXXX-orig`
31 editor=${EDITOR:-vim}
33 LANGUAGE=en
34 my_name="`echo $0 | sed s@.*/@@ | sed s/-.*// `"
35 if echo "$1" | grep -q http://...wikipedia.org/wiki/
36 then
37 LANGUAGE="`echo $1 | sed s@http://@@ | sed s@.wikipedia.*@@`"
38 fi
39 [ "${my_name}" = "new" ] || LANGUAGE="$my_name"
40 if [ "$1" = "-l" ]
41 then
42 LANGUAGE="$2"
43 VOCABULARY="$LANGUAGE".txt
44 shift 2
45 fi
46 VOCABULARY=${LANGUAGE}.txt
47 NOTES_FILE=notes-${LANGUAGE}.txt
49 STAT_ONLY=NO
50 if [ "$1" = "-s" ]
51 then
52 STAT_ONLY=YES
53 shift
54 fi
56 get_words()
57 {
58 tr ' ' '\n' | sed 's/--/ /g' \
59 | sed "s/'/__APOSTROPHE__/g" \
60 | tr '—·-' '-----' \
61 | tr '*\r,.:#@()+=—<>$;"?!|·[]^%&' ' ' \
62 | tr ' ' '\n' \
63 | grep -x '[[:alpha:]'"'"'-]*' \
64 | tee "$1" \
65 | grep_v_english_perl \
66 | sort | uniq -c | awk '{if ($2!="") print;}' | sort -rn
67 }
69 add_stat()
70 {
71 before="$1"
72 after=${before}2
73 cat > "$after"
74 total="`wc -w $1 | awk '{print $1}'`"
75 total_unknown="`cat $after|awk '{s=s+$1}END{print s}'`"
76 total_known="`echo $total-$total_unknown|bc`"
77 percentage="`echo '100*('$total-$total_unknown')'/$total | bc -l | sed 's/\\.\(.\).*/.\1/'`"
78 if [ "$STAT_ONLY" = "YES" ]
79 then
80 echo "LANG KNOWN% UNKNOWN% KNOWN TOTAL"
81 echo "$LANGUAGE $percentage `echo \(100-$percentage\) | bc -l` $total_known $total"
82 rm $after
83 return 0
84 else
85 echo "# $LANGUAGE, $percentage, <$total_known/$total>"
86 fi
88 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
89 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
90 my $total=shift(@ARGV);
91 my $total_known=shift(@ARGV);
92 my $s=0;
93 my $mark_line=int($total_known*100/$total/5)*5;
94 if ($mark_line>=90) { $mark_line+=1; } else { $mark_line +=5; };
95 while(<>)
96 {
97 print;
98 /^\s*([0-9]*)\s*/;
99 $s+=$1;
100 if (int(($total_known+$s)*100/$total)>=$mark_line) {
101 print "# $mark_line\n";
102 if ($mark_line>=90) { $mark_line+=1; } else { $mark_line +=5; };
103 }
104 }
105 PERL_SCRIPT
106 perl $PERL_SCRIPT_TEMP_NAME "$total" "$total_known" "$after"
107 rm $PERL_SCRIPT_TEMP_NAME
108 rm $after
109 }
111 grep_v_english()
112 {
113 [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
114 eval $(cat $VOCABULARY | tr -d "'" | xargs -n10 echo | tr ' ' '|' | sed 's/^/egrep -xv "RRRRRRR|/' | sed 's/$/"/' | tr '\n' '|')cat
115 }
117 grep_v_english_perl()
118 {
119 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
120 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
121 open(VOC, $ENV{VOCABULARY})
122 or die "Can't open VOCABULARY";
123 while (<VOC>){
124 chomp;
125 #s/'//g;
126 $voc{$_}="1";
127 }
128 while(<>) {
129 chomp;
130 if (not defined($voc{$_})) { print "$_\n"; }
131 }
132 PERL_SCRIPT
133 [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
134 export VOCABULARY
135 perl $PERL_SCRIPT_TEMP_NAME
136 rm $PERL_SCRIPT_TEMP_NAME
137 }
139 group_words()
140 {
141 if [ "$LANGUAGE" != "en" ]
142 then
143 cat
144 return
145 fi
146 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-group-words-XXXXXXXX`
147 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
148 #!/usr/bin/perl
150 eval {
151 # http://stackoverflow.com/questions/251694/how-can-i-check-if-i-have-a-perl-module-before-using-it
152 require String::Similarity;
153 String::Similarity->import();
154 };
155 unless($@)
156 {
157 our $HAVE_String_Similarity=1;
158 }
160 sub similar($$){
161 my $a=shift;
162 my $b=shift;
163 if ($HAVE_String_Similarity) {
164 return $Similarity{"$a $b"};
165 }
166 else {
167 return 0;
168 }
169 }
171 sub normalize($)
172 {
173 $_=lc(shift);
175 s/s$//;
176 s/ed$//;
177 s/en$//;
178 s/er$//;
179 s/est$//;
180 s/ing$//;
182 s/ism$//; s/ist$//; s/ful$//; s/able$//; s/ably$//;
183 s/ation$//; s/ness$//; s/ship$//; s/ally$//;
184 s/ment$//; s/ify$//; s/ity$//; s/fy$//; s/ly$//;
185 s/ise$//; s/ize$//;
187 s/e$//;
188 return $_;
189 }
192 sub compare($$)
193 {
194 my $a=shift;
195 my $b=shift;
196 $a =~ s/^\s*//;
197 $b =~ s/^\s*//;
198 my ($a1, $a2)= split /\s+/,$a,2;
199 my ($b1, $b2)= split /\s+/,$b,2;
201 my $cmp = $group_weight{normalize($a2)} <=> $group_weight{normalize($b2)};
203 if ($cmp) {
204 return $cmp;
205 }
206 else {
207 if (normalize($a2) ne normalize($b2)) {
208 return normalize($a2) cmp normalize($b2);
209 }
210 else {
211 return $a1 <=> $b1;
212 }
213 }
214 }
216 @lines=<>;
217 for $L (@lines) {
218 chomp($L);
219 $l=$L;
220 $l =~ s/^\s*//;
221 my ($a, $b)=split(/\s+/,$l,2);
222 $group_weight{normalize($b)}+=$a;
223 }
224 @lines2 = sort { compare($b,$a) } @lines;
225 for $l (@lines2) {
226 print "$l\n";
227 }
228 PERL_SCRIPT
229 perl $PERL_SCRIPT_TEMP_NAME
230 rm $PERL_SCRIPT_TEMP_NAME
231 }
233 text_from_url()
234 {
235 lynx -dump "$1" | perl -p -e 's@http://[a-zA-Z&_.:/0-9%?=,#+()\[\]~-]*@@'
236 }
238 add_marks()
239 {
240 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
241 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
242 $file = $ARGV[0];
243 our $dict;
244 if (open(NOTES, $ENV{NOTES_FILE})) {
245 while(<NOTES>) {
246 chomp;
247 s/^\s+//;
248 my ($a,$b)=split /\s+/,$_,2;
249 $dict{$a}=$b;
250 }
251 }
252 if (open(F, $file)) {
253 @lines=<F>;
254 close(F);
256 if (open(F, ">$file")) {
257 for (@lines) {
258 m/\s+\S+\s+(\S+)/;
259 $name=$1;
260 if (not /^#/ and defined($dict{$name})) {
261 chomp;
262 $mark=$dict{$name};
263 $space=" "x(30-length($_));
264 print F "$_$space$mark\n";
265 }
266 else {
267 print F "$_";
268 }
269 }
270 close(F);
271 }
272 }
273 PERL_SCRIPT
274 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
275 export NOTES_FILE
276 perl $PERL_SCRIPT_TEMP_NAME "$1"
277 rm $PERL_SCRIPT_TEMP_NAME
278 }
280 remove_marks()
281 {
282 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
283 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
284 $file = $ARGV[0];
285 our %dict;
286 if (open(F, $file)) {
287 @lines=<F>;
288 close(F);
290 if (open(F, ">$file")) {
291 for (@lines) {
292 chomp;
293 if (not /^#/ and m/(\s+)(\S+)(\s+)(\S+)(\s+)(.*)/) {
294 my $name=$4;
295 my $comment=$6;
296 $dict{$name}=$comment;
297 print F "$1$2$3$4\n";
298 }
299 else {
300 print F "$_\n";
301 }
302 }
303 }
304 }
305 if (open(NOTES, $ENV{NOTES_FILE})) {
306 @lines=<NOTES>;
307 close(NOTES);
309 if (open(NOTES, ">".$ENV{NOTES_FILE})) {
310 for (@lines) {
311 chomp;
312 s/^\s+//;
313 my ($a,$b)=split /\s+/,$_,2;
314 if (not defined($dict{$a}) || ($dict{$a} eq $b)) {
315 print NOTES "$_\n";
316 if (defined($dict{$a})) { unset($dict{$a}); }
317 }
318 }
319 for (keys %dict) {
320 $mark=$dict{$_};
321 $space=" "x(30-length($_));
322 print NOTES "$_$space$mark\n";
323 }
324 }
325 }
326 PERL_SCRIPT
327 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
328 export NOTES_FILE
329 perl $PERL_SCRIPT_TEMP_NAME "$1"
330 rm $PERL_SCRIPT_TEMP_NAME
331 }
333 mkdir -p $WORK_DIR
334 oldpwd="$PWD"
335 cd $WORK_DIR
336 if echo "$1" | grep -q http:
337 then
338 text_from_url "$1"
339 elif [ "$#" != 0 ]
340 then
341 if echo $1 | grep -q ^/
342 then
343 cat "$1"
344 else
345 cat "$oldpwd/$1"
346 fi
347 else
348 cat
349 fi \
350 | tee $ORIGINAL_TEXT \
351 | get_words ${TEMP1}-full \
352 | group_words \
353 | add_stat ${TEMP1}-full \
354 | tee "$TEMP1" > "$TEMP2"
356 if [ "$STAT_ONLY" = "YES" ]
357 then
358 cat "$TEMP1"
359 else
360 add_marks "$TEMP2"
361 if [ "$editor" = vim ]
362 then
363 vim -c 'set keywordprg='"$LANGUAGE" -c 'set iskeyword=@,48-57,/,.,-,_,+,,,#,$,%,~,=' "$TEMP2" < /dev/tty > /dev/tty
364 else
365 echo 2
366 $editor "$TEMP2"
367 fi
368 remove_marks "$TEMP2"
369 fi
371 diff "$TEMP1" "$TEMP2" | awk '{print $3}' | sort -u >> "$VOCABULARY"
372 rm -f "$TEMP1" "$TEMP2" "${TEMP1}-full" "$ORIGINAL_TEXT"