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