rev |
line source |
igor@0
|
1 #!/bin/sh
|
igor@20
|
2
|
igor@20
|
3 show_usage()
|
igor@20
|
4 {
|
igor@20
|
5 cat <<HELP > /dev/stderr
|
igor@0
|
6
|
igor@16
|
7 USAGE:
|
igor@16
|
8
|
igor@16
|
9 new-words [ -l lang ] [ -s ] [ ARG ]
|
igor@16
|
10
|
igor@16
|
11 SWITCHES:
|
igor@16
|
12
|
igor@20
|
13 -h print this screen
|
igor@20
|
14 -k put higher words that are similar to the known words (only for English)
|
igor@16
|
15 -l lang override language settings
|
igor@21
|
16 -n non-interactive mode (don't run vi)
|
igor@21
|
17 -m don't add marks (and don't save marks added by user)
|
igor@20
|
18 -s show the text statistics (percentage of known words and so on) and exit
|
igor@16
|
19
|
igor@20
|
20 The language of the text can be specified also
|
igor@20
|
21 by name of the program new-words (correspondent link must be created before).
|
igor@20
|
22 For example, these calls are equivalent:
|
igor@0
|
23
|
igor@20
|
24 de-words URL
|
igor@0
|
25 new-words -l de URL
|
igor@0
|
26
|
igor@20
|
27 HELP
|
igor@20
|
28 }
|
igor@0
|
29
|
igor@20
|
30 if [ "$1" = "-h" ]
|
igor@20
|
31 then
|
igor@20
|
32 show_usage
|
igor@20
|
33 exit 0
|
igor@20
|
34 fi
|
igor@0
|
35
|
igor@0
|
36 WORK_DIR=~/.new-words/
|
igor@17
|
37 TEMP1=`mktemp /tmp/new-words-XXXXXXXXXX-temp1`
|
igor@17
|
38 TEMP2=`mktemp /tmp/new-words-XXXXXXXXXX-temp2`
|
igor@17
|
39 export ORIGINAL_TEXT=`mktemp /tmp/new-words-XXXXXXXXXX-orig`
|
igor@0
|
40 editor=${EDITOR:-vim}
|
igor@0
|
41
|
igor@21
|
42 #----------------------------------------------------
|
igor@21
|
43 # command line options processing
|
igor@21
|
44
|
igor@2
|
45 LANGUAGE=en
|
igor@2
|
46 my_name="`echo $0 | sed s@.*/@@ | sed s/-.*// `"
|
igor@3
|
47 if echo "$1" | grep -q http://...wikipedia.org/wiki/
|
igor@3
|
48 then
|
igor@3
|
49 LANGUAGE="`echo $1 | sed s@http://@@ | sed s@.wikipedia.*@@`"
|
igor@3
|
50 fi
|
igor@2
|
51 [ "${my_name}" = "new" ] || LANGUAGE="$my_name"
|
igor@0
|
52 if [ "$1" = "-l" ]
|
igor@0
|
53 then
|
igor@0
|
54 LANGUAGE="$2"
|
igor@0
|
55 VOCABULARY="$LANGUAGE".txt
|
igor@0
|
56 shift 2
|
igor@0
|
57 fi
|
igor@2
|
58 VOCABULARY=${LANGUAGE}.txt
|
igor@2
|
59 NOTES_FILE=notes-${LANGUAGE}.txt
|
igor@0
|
60
|
igor@16
|
61 STAT_ONLY=NO
|
igor@16
|
62 if [ "$1" = "-s" ]
|
igor@16
|
63 then
|
igor@16
|
64 STAT_ONLY=YES
|
igor@16
|
65 shift
|
igor@16
|
66 fi
|
igor@16
|
67
|
igor@20
|
68 NEED_TO_USE_VOCABULARY_WHEN_SORT=NO
|
igor@20
|
69 if [ "$1" = "-k" ]
|
igor@20
|
70 then
|
igor@20
|
71 NEED_TO_USE_VOCABULARY_WHEN_SORT=YES
|
igor@20
|
72 shift
|
igor@20
|
73 fi
|
igor@20
|
74
|
igor@21
|
75 DONT_ADD_MARKS=NO
|
igor@21
|
76 if [ "$1" = "-m" ]
|
igor@21
|
77 then
|
igor@21
|
78 DONT_ADD_MARKS=YES
|
igor@21
|
79 shift
|
igor@21
|
80 fi
|
igor@21
|
81
|
igor@21
|
82 NON_INTERACTIVE_MODE=NO
|
igor@21
|
83 if [ "$1" = "-n" ]
|
igor@21
|
84 then
|
igor@21
|
85 NON_INTERACTIVE_MODE=YES
|
igor@21
|
86 shift
|
igor@21
|
87 fi
|
igor@21
|
88
|
igor@21
|
89 #----------------------------------------------------
|
igor@21
|
90
|
igor@0
|
91 get_words()
|
igor@0
|
92 {
|
igor@1
|
93 tr ' ' '\n' | sed 's/--/ /g' \
|
igor@3
|
94 | sed "s/'/__APOSTROPHE__/g" \
|
igor@3
|
95 | tr '—·-' '-----' \
|
igor@3
|
96 | tr '*\r,.:#@()+=—<>$;"?!|·[]^%&' ' ' \
|
igor@3
|
97 | tr ' ' '\n' \
|
igor@3
|
98 | grep -x '[[:alpha:]'"'"'-]*' \
|
igor@3
|
99 | tee "$1" \
|
igor@3
|
100 | grep_v_english_perl \
|
igor@0
|
101 | sort | uniq -c | awk '{if ($2!="") print;}' | sort -rn
|
igor@0
|
102 }
|
igor@0
|
103
|
igor@3
|
104 add_stat()
|
igor@3
|
105 {
|
igor@3
|
106 before="$1"
|
igor@3
|
107 after=${before}2
|
igor@3
|
108 cat > "$after"
|
igor@3
|
109 total="`wc -w $1 | awk '{print $1}'`"
|
igor@3
|
110 total_unknown="`cat $after|awk '{s=s+$1}END{print s}'`"
|
igor@3
|
111 total_known="`echo $total-$total_unknown|bc`"
|
igor@3
|
112 percentage="`echo '100*('$total-$total_unknown')'/$total | bc -l | sed 's/\\.\(.\).*/.\1/'`"
|
igor@18
|
113 #sentences="`cat $after | perl -e 'local $/; $_=<>; s@http://[a-zA-Z&_.:/0-9%?=,\#+()\[\]~-]*@@g; s@\n@@g; s@(Mr|Mrs)\.@\1POINT@g; @sentences=split /\\./;print $#sentences;'`"
|
igor@18
|
114 sentences="`cat $ORIGINAL_TEXT | perl -e 'local $/; $_=<>; s/[^.]//msg; print length($_);'`"
|
igor@18
|
115
|
igor@18
|
116
|
igor@16
|
117 if [ "$STAT_ONLY" = "YES" ]
|
igor@16
|
118 then
|
igor@18
|
119 echo "LANG KNOWN% UNKNOWN% KNOWN TOTAL WPS UWPS*10"
|
igor@18
|
120 echo "$LANGUAGE $percentage `echo \(100-$percentage\) | bc -l` $total_known $total `echo $total/$sentences|bc` `echo 10*$total_unknown/$sentences|bc` "
|
igor@17
|
121 rm $after
|
igor@16
|
122 return 0
|
igor@16
|
123 else
|
igor@16
|
124 echo "# $LANGUAGE, $percentage, <$total_known/$total>"
|
igor@16
|
125 fi
|
igor@16
|
126
|
igor@14
|
127 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
|
igor@14
|
128 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
|
igor@14
|
129 my $total=shift(@ARGV);
|
igor@14
|
130 my $total_known=shift(@ARGV);
|
igor@14
|
131 my $s=0;
|
igor@16
|
132 my $mark_line=int($total_known*100/$total/5)*5;
|
igor@19
|
133 if ($mark_line>=90) {
|
igor@19
|
134 $mark_line=int($total_known*100/$total)+1;
|
igor@19
|
135 } else { $mark_line +=5; };
|
igor@14
|
136 while(<>)
|
igor@14
|
137 {
|
igor@14
|
138 print;
|
igor@14
|
139 /^\s*([0-9]*)\s*/;
|
igor@14
|
140 $s+=$1;
|
igor@19
|
141 if (($total_known+$s)*100/$total>=$mark_line) {
|
igor@14
|
142 print "# $mark_line\n";
|
igor@14
|
143 if ($mark_line>=90) { $mark_line+=1; } else { $mark_line +=5; };
|
igor@14
|
144 }
|
igor@14
|
145 }
|
igor@14
|
146 PERL_SCRIPT
|
igor@14
|
147 perl $PERL_SCRIPT_TEMP_NAME "$total" "$total_known" "$after"
|
igor@14
|
148 rm $PERL_SCRIPT_TEMP_NAME
|
igor@3
|
149 rm $after
|
igor@3
|
150 }
|
igor@3
|
151
|
igor@0
|
152 grep_v_english()
|
igor@0
|
153 {
|
igor@0
|
154 [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
|
igor@0
|
155 eval $(cat $VOCABULARY | tr -d "'" | xargs -n10 echo | tr ' ' '|' | sed 's/^/egrep -xv "RRRRRRR|/' | sed 's/$/"/' | tr '\n' '|')cat
|
igor@0
|
156 }
|
igor@0
|
157
|
igor@0
|
158 grep_v_english_perl()
|
igor@0
|
159 {
|
igor@0
|
160 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
|
igor@0
|
161 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
|
igor@0
|
162 open(VOC, $ENV{VOCABULARY})
|
igor@0
|
163 or die "Can't open VOCABULARY";
|
igor@0
|
164 while (<VOC>){
|
igor@0
|
165 chomp;
|
igor@3
|
166 #s/'//g;
|
igor@0
|
167 $voc{$_}="1";
|
igor@0
|
168 }
|
igor@0
|
169 while(<>) {
|
igor@0
|
170 chomp;
|
igor@0
|
171 if (not defined($voc{$_})) { print "$_\n"; }
|
igor@0
|
172 }
|
igor@0
|
173 PERL_SCRIPT
|
igor@0
|
174 [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
|
igor@0
|
175 export VOCABULARY
|
igor@0
|
176 perl $PERL_SCRIPT_TEMP_NAME
|
igor@0
|
177 rm $PERL_SCRIPT_TEMP_NAME
|
igor@0
|
178 }
|
igor@0
|
179
|
igor@11
|
180 group_words()
|
igor@11
|
181 {
|
igor@11
|
182 if [ "$LANGUAGE" != "en" ]
|
igor@11
|
183 then
|
igor@11
|
184 cat
|
igor@11
|
185 return
|
igor@11
|
186 fi
|
igor@11
|
187 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-group-words-XXXXXXXX`
|
igor@11
|
188 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
|
igor@11
|
189 #!/usr/bin/perl
|
igor@11
|
190
|
igor@12
|
191 eval {
|
igor@12
|
192 # http://stackoverflow.com/questions/251694/how-can-i-check-if-i-have-a-perl-module-before-using-it
|
igor@12
|
193 require String::Similarity;
|
igor@12
|
194 String::Similarity->import();
|
igor@12
|
195 };
|
igor@12
|
196 unless($@)
|
igor@12
|
197 {
|
igor@12
|
198 our $HAVE_String_Similarity=1;
|
igor@12
|
199 }
|
igor@12
|
200
|
igor@12
|
201 sub similar($$){
|
igor@12
|
202 my $a=shift;
|
igor@12
|
203 my $b=shift;
|
igor@12
|
204 if ($HAVE_String_Similarity) {
|
igor@12
|
205 return $Similarity{"$a $b"};
|
igor@12
|
206 }
|
igor@12
|
207 else {
|
igor@12
|
208 return 0;
|
igor@12
|
209 }
|
igor@12
|
210 }
|
igor@12
|
211
|
igor@11
|
212 sub normalize($)
|
igor@11
|
213 {
|
igor@11
|
214 $_=lc(shift);
|
igor@12
|
215
|
igor@11
|
216 s/s$//;
|
igor@11
|
217 s/ed$//;
|
igor@12
|
218 s/en$//;
|
igor@12
|
219 s/er$//;
|
igor@12
|
220 s/est$//;
|
igor@11
|
221 s/ing$//;
|
igor@12
|
222
|
igor@12
|
223 s/ism$//; s/ist$//; s/ful$//; s/able$//; s/ably$//;
|
igor@16
|
224 s/ation$//; s/ness$//; s/ship$//; s/ally$//;
|
igor@12
|
225 s/ment$//; s/ify$//; s/ity$//; s/fy$//; s/ly$//;
|
igor@12
|
226 s/ise$//; s/ize$//;
|
igor@12
|
227
|
igor@12
|
228 s/e$//;
|
igor@11
|
229 return $_;
|
igor@11
|
230 }
|
igor@11
|
231
|
igor@12
|
232
|
igor@11
|
233 sub compare($$)
|
igor@11
|
234 {
|
igor@11
|
235 my $a=shift;
|
igor@11
|
236 my $b=shift;
|
igor@11
|
237 $a =~ s/^\s*//;
|
igor@11
|
238 $b =~ s/^\s*//;
|
igor@11
|
239 my ($a1, $a2)= split /\s+/,$a,2;
|
igor@11
|
240 my ($b1, $b2)= split /\s+/,$b,2;
|
igor@11
|
241
|
igor@11
|
242 my $cmp = $group_weight{normalize($a2)} <=> $group_weight{normalize($b2)};
|
igor@12
|
243
|
igor@11
|
244 if ($cmp) {
|
igor@11
|
245 return $cmp;
|
igor@11
|
246 }
|
igor@11
|
247 else {
|
igor@11
|
248 if (normalize($a2) ne normalize($b2)) {
|
igor@11
|
249 return normalize($a2) cmp normalize($b2);
|
igor@11
|
250 }
|
igor@11
|
251 else {
|
igor@11
|
252 return $a1 <=> $b1;
|
igor@11
|
253 }
|
igor@11
|
254 }
|
igor@11
|
255 }
|
igor@11
|
256
|
igor@20
|
257 our %Vocabulary;
|
igor@20
|
258 open(VOC, $ENV{VOCABULARY})
|
igor@20
|
259 or die "Can't open VOCABULARY";
|
igor@20
|
260 while (<VOC>){
|
igor@20
|
261 chomp;
|
igor@20
|
262 #s/'//g;
|
igor@20
|
263 $Vocabulary{normalize($_)}="1";
|
igor@20
|
264 }
|
igor@20
|
265 close(VOC);
|
igor@20
|
266
|
igor@20
|
267 @lines=<STDIN>;
|
igor@11
|
268 for $L (@lines) {
|
igor@11
|
269 chomp($L);
|
igor@11
|
270 $l=$L;
|
igor@11
|
271 $l =~ s/^\s*//;
|
igor@11
|
272 my ($a, $b)=split(/\s+/,$l,2);
|
igor@11
|
273 $group_weight{normalize($b)}+=$a;
|
igor@11
|
274 }
|
igor@20
|
275 if ($ENV{NEED_TO_USE_VOCABULARY_WHEN_SORT} eq "YES") {
|
igor@20
|
276 for $k (keys %group_weight) {
|
igor@20
|
277 if (defined($Vocabulary{$k})) {
|
igor@20
|
278 $group_weight{$k} *= 2;
|
igor@20
|
279 }
|
igor@20
|
280 }
|
igor@20
|
281 }
|
igor@11
|
282 @lines2 = sort { compare($b,$a) } @lines;
|
igor@11
|
283 for $l (@lines2) {
|
igor@11
|
284 print "$l\n";
|
igor@11
|
285 }
|
igor@11
|
286 PERL_SCRIPT
|
igor@20
|
287 export VOCABULARY
|
igor@20
|
288 export NEED_TO_USE_VOCABULARY_WHEN_SORT
|
igor@11
|
289 perl $PERL_SCRIPT_TEMP_NAME
|
igor@11
|
290 rm $PERL_SCRIPT_TEMP_NAME
|
igor@11
|
291 }
|
igor@11
|
292
|
igor@0
|
293 text_from_url()
|
igor@0
|
294 {
|
igor@0
|
295 lynx -dump "$1" | perl -p -e 's@http://[a-zA-Z&_.:/0-9%?=,#+()\[\]~-]*@@'
|
igor@0
|
296 }
|
igor@0
|
297
|
igor@2
|
298 add_marks()
|
igor@2
|
299 {
|
igor@2
|
300 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
|
igor@2
|
301 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
|
igor@2
|
302 $file = $ARGV[0];
|
igor@2
|
303 our $dict;
|
igor@2
|
304 if (open(NOTES, $ENV{NOTES_FILE})) {
|
igor@2
|
305 while(<NOTES>) {
|
igor@2
|
306 chomp;
|
igor@2
|
307 s/^\s+//;
|
igor@2
|
308 my ($a,$b)=split /\s+/,$_,2;
|
igor@2
|
309 $dict{$a}=$b;
|
igor@2
|
310 }
|
igor@2
|
311 }
|
igor@2
|
312 if (open(F, $file)) {
|
igor@2
|
313 @lines=<F>;
|
igor@2
|
314 close(F);
|
igor@2
|
315
|
igor@2
|
316 if (open(F, ">$file")) {
|
igor@2
|
317 for (@lines) {
|
igor@2
|
318 m/\s+\S+\s+(\S+)/;
|
igor@2
|
319 $name=$1;
|
igor@3
|
320 if (not /^#/ and defined($dict{$name})) {
|
igor@2
|
321 chomp;
|
igor@2
|
322 $mark=$dict{$name};
|
igor@2
|
323 $space=" "x(30-length($_));
|
igor@2
|
324 print F "$_$space$mark\n";
|
igor@2
|
325 }
|
igor@2
|
326 else {
|
igor@2
|
327 print F "$_";
|
igor@2
|
328 }
|
igor@2
|
329 }
|
igor@2
|
330 close(F);
|
igor@2
|
331 }
|
igor@2
|
332 }
|
igor@2
|
333 PERL_SCRIPT
|
igor@2
|
334 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
|
igor@2
|
335 export NOTES_FILE
|
igor@2
|
336 perl $PERL_SCRIPT_TEMP_NAME "$1"
|
igor@2
|
337 rm $PERL_SCRIPT_TEMP_NAME
|
igor@2
|
338 }
|
igor@2
|
339
|
igor@2
|
340 remove_marks()
|
igor@2
|
341 {
|
igor@2
|
342 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
|
igor@2
|
343 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
|
igor@2
|
344 $file = $ARGV[0];
|
igor@2
|
345 our %dict;
|
igor@2
|
346 if (open(F, $file)) {
|
igor@2
|
347 @lines=<F>;
|
igor@2
|
348 close(F);
|
igor@2
|
349
|
igor@2
|
350 if (open(F, ">$file")) {
|
igor@2
|
351 for (@lines) {
|
igor@2
|
352 chomp;
|
igor@3
|
353 if (not /^#/ and m/(\s+)(\S+)(\s+)(\S+)(\s+)(.*)/) {
|
igor@2
|
354 my $name=$4;
|
igor@2
|
355 my $comment=$6;
|
igor@2
|
356 $dict{$name}=$comment;
|
igor@2
|
357 print F "$1$2$3$4\n";
|
igor@2
|
358 }
|
igor@2
|
359 else {
|
igor@2
|
360 print F "$_\n";
|
igor@2
|
361 }
|
igor@2
|
362 }
|
igor@2
|
363 }
|
igor@2
|
364 }
|
igor@21
|
365 if (($ENV{DONT_ADD_MARKS} ne "YES") and open(NOTES, $ENV{NOTES_FILE})) {
|
igor@2
|
366 @lines=<NOTES>;
|
igor@2
|
367 close(NOTES);
|
igor@2
|
368
|
igor@2
|
369 if (open(NOTES, ">".$ENV{NOTES_FILE})) {
|
igor@2
|
370 for (@lines) {
|
igor@2
|
371 chomp;
|
igor@2
|
372 s/^\s+//;
|
igor@2
|
373 my ($a,$b)=split /\s+/,$_,2;
|
igor@2
|
374 if (not defined($dict{$a}) || ($dict{$a} eq $b)) {
|
igor@2
|
375 print NOTES "$_\n";
|
igor@2
|
376 if (defined($dict{$a})) { unset($dict{$a}); }
|
igor@2
|
377 }
|
igor@2
|
378 }
|
igor@2
|
379 for (keys %dict) {
|
igor@2
|
380 $mark=$dict{$_};
|
igor@2
|
381 $space=" "x(30-length($_));
|
igor@2
|
382 print NOTES "$_$space$mark\n";
|
igor@2
|
383 }
|
igor@2
|
384 }
|
igor@2
|
385 }
|
igor@2
|
386 PERL_SCRIPT
|
igor@2
|
387 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
|
igor@2
|
388 export NOTES_FILE
|
igor@21
|
389 export DONT_ADD_MARKS
|
igor@2
|
390 perl $PERL_SCRIPT_TEMP_NAME "$1"
|
igor@2
|
391 rm $PERL_SCRIPT_TEMP_NAME
|
igor@2
|
392 }
|
igor@2
|
393
|
igor@0
|
394 mkdir -p $WORK_DIR
|
igor@8
|
395 oldpwd="$PWD"
|
igor@0
|
396 cd $WORK_DIR
|
igor@0
|
397 if echo "$1" | grep -q http:
|
igor@0
|
398 then
|
igor@11
|
399 text_from_url "$1"
|
igor@0
|
400 elif [ "$#" != 0 ]
|
igor@0
|
401 then
|
igor@13
|
402 if echo $1 | grep -q ^/
|
igor@13
|
403 then
|
igor@13
|
404 cat "$1"
|
igor@13
|
405 else
|
igor@13
|
406 cat "$oldpwd/$1"
|
igor@13
|
407 fi
|
igor@0
|
408 else
|
igor@11
|
409 cat
|
igor@11
|
410 fi \
|
igor@11
|
411 | tee $ORIGINAL_TEXT \
|
igor@11
|
412 | get_words ${TEMP1}-full \
|
igor@11
|
413 | group_words \
|
igor@11
|
414 | add_stat ${TEMP1}-full \
|
igor@11
|
415 | tee "$TEMP1" > "$TEMP2"
|
igor@0
|
416
|
igor@16
|
417 if [ "$STAT_ONLY" = "YES" ]
|
igor@0
|
418 then
|
igor@16
|
419 cat "$TEMP1"
|
igor@21
|
420 elif [ "$NON_INTERACTIVE_MODE" = "YES" ]
|
igor@21
|
421 then
|
igor@21
|
422 cat "$TEMP1"
|
igor@0
|
423 else
|
igor@21
|
424 [ "$DONT_ADD_MARKS" = "YES" ] || add_marks "$TEMP2"
|
igor@16
|
425 if [ "$editor" = vim ]
|
igor@16
|
426 then
|
igor@16
|
427 vim -c 'set keywordprg='"$LANGUAGE" -c 'set iskeyword=@,48-57,/,.,-,_,+,,,#,$,%,~,=' "$TEMP2" < /dev/tty > /dev/tty
|
igor@16
|
428 else
|
igor@16
|
429 echo 2
|
igor@16
|
430 $editor "$TEMP2"
|
igor@16
|
431 fi
|
igor@16
|
432 remove_marks "$TEMP2"
|
igor@0
|
433 fi
|
igor@2
|
434
|
igor@0
|
435 diff "$TEMP1" "$TEMP2" | awk '{print $3}' | sort -u >> "$VOCABULARY"
|
igor@5
|
436 rm -f "$TEMP1" "$TEMP2" "${TEMP1}-full" "$ORIGINAL_TEXT"
|