new-words
view new-words.sh @ 18:7e3a52db54ad
Среднее количество слов и неизвестных слов (*10) в предложении в статистике.
Пример использования:
LANG KNOWN% UNKNOWN% KNOWN TOTAL WPS UWPS*10
en 89.8 10.2 167021 185840 21 21
Пример использования:
LANG KNOWN% UNKNOWN% KNOWN TOTAL WPS UWPS*10
en 89.8 10.2 167021 185840 21 21
author | Igor Chubin <igor@chub.in> |
---|---|
date | Mon Apr 05 21:34:55 2010 +0300 (2010-04-05) |
parents | 35eeaf2620ce |
children | 416394a87d9f |
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 #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;'`"
79 sentences="`cat $ORIGINAL_TEXT | perl -e 'local $/; $_=<>; s/[^.]//msg; print length($_);'`"
82 if [ "$STAT_ONLY" = "YES" ]
83 then
84 echo "LANG KNOWN% UNKNOWN% KNOWN TOTAL WPS UWPS*10"
85 echo "$LANGUAGE $percentage `echo \(100-$percentage\) | bc -l` $total_known $total `echo $total/$sentences|bc` `echo 10*$total_unknown/$sentences|bc` "
86 rm $after
87 return 0
88 else
89 echo "# $LANGUAGE, $percentage, <$total_known/$total>"
90 fi
92 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
93 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
94 my $total=shift(@ARGV);
95 my $total_known=shift(@ARGV);
96 my $s=0;
97 my $mark_line=int($total_known*100/$total/5)*5;
98 if ($mark_line>=90) { $mark_line+=1; } else { $mark_line +=5; };
99 while(<>)
100 {
101 print;
102 /^\s*([0-9]*)\s*/;
103 $s+=$1;
104 if (int(($total_known+$s)*100/$total)>=$mark_line) {
105 print "# $mark_line\n";
106 if ($mark_line>=90) { $mark_line+=1; } else { $mark_line +=5; };
107 }
108 }
109 PERL_SCRIPT
110 perl $PERL_SCRIPT_TEMP_NAME "$total" "$total_known" "$after"
111 rm $PERL_SCRIPT_TEMP_NAME
112 rm $after
113 }
115 grep_v_english()
116 {
117 [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
118 eval $(cat $VOCABULARY | tr -d "'" | xargs -n10 echo | tr ' ' '|' | sed 's/^/egrep -xv "RRRRRRR|/' | sed 's/$/"/' | tr '\n' '|')cat
119 }
121 grep_v_english_perl()
122 {
123 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
124 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
125 open(VOC, $ENV{VOCABULARY})
126 or die "Can't open VOCABULARY";
127 while (<VOC>){
128 chomp;
129 #s/'//g;
130 $voc{$_}="1";
131 }
132 while(<>) {
133 chomp;
134 if (not defined($voc{$_})) { print "$_\n"; }
135 }
136 PERL_SCRIPT
137 [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
138 export VOCABULARY
139 perl $PERL_SCRIPT_TEMP_NAME
140 rm $PERL_SCRIPT_TEMP_NAME
141 }
143 group_words()
144 {
145 if [ "$LANGUAGE" != "en" ]
146 then
147 cat
148 return
149 fi
150 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-group-words-XXXXXXXX`
151 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
152 #!/usr/bin/perl
154 eval {
155 # http://stackoverflow.com/questions/251694/how-can-i-check-if-i-have-a-perl-module-before-using-it
156 require String::Similarity;
157 String::Similarity->import();
158 };
159 unless($@)
160 {
161 our $HAVE_String_Similarity=1;
162 }
164 sub similar($$){
165 my $a=shift;
166 my $b=shift;
167 if ($HAVE_String_Similarity) {
168 return $Similarity{"$a $b"};
169 }
170 else {
171 return 0;
172 }
173 }
175 sub normalize($)
176 {
177 $_=lc(shift);
179 s/s$//;
180 s/ed$//;
181 s/en$//;
182 s/er$//;
183 s/est$//;
184 s/ing$//;
186 s/ism$//; s/ist$//; s/ful$//; s/able$//; s/ably$//;
187 s/ation$//; s/ness$//; s/ship$//; s/ally$//;
188 s/ment$//; s/ify$//; s/ity$//; s/fy$//; s/ly$//;
189 s/ise$//; s/ize$//;
191 s/e$//;
192 return $_;
193 }
196 sub compare($$)
197 {
198 my $a=shift;
199 my $b=shift;
200 $a =~ s/^\s*//;
201 $b =~ s/^\s*//;
202 my ($a1, $a2)= split /\s+/,$a,2;
203 my ($b1, $b2)= split /\s+/,$b,2;
205 my $cmp = $group_weight{normalize($a2)} <=> $group_weight{normalize($b2)};
207 if ($cmp) {
208 return $cmp;
209 }
210 else {
211 if (normalize($a2) ne normalize($b2)) {
212 return normalize($a2) cmp normalize($b2);
213 }
214 else {
215 return $a1 <=> $b1;
216 }
217 }
218 }
220 @lines=<>;
221 for $L (@lines) {
222 chomp($L);
223 $l=$L;
224 $l =~ s/^\s*//;
225 my ($a, $b)=split(/\s+/,$l,2);
226 $group_weight{normalize($b)}+=$a;
227 }
228 @lines2 = sort { compare($b,$a) } @lines;
229 for $l (@lines2) {
230 print "$l\n";
231 }
232 PERL_SCRIPT
233 perl $PERL_SCRIPT_TEMP_NAME
234 rm $PERL_SCRIPT_TEMP_NAME
235 }
237 text_from_url()
238 {
239 lynx -dump "$1" | perl -p -e 's@http://[a-zA-Z&_.:/0-9%?=,#+()\[\]~-]*@@'
240 }
242 add_marks()
243 {
244 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
245 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
246 $file = $ARGV[0];
247 our $dict;
248 if (open(NOTES, $ENV{NOTES_FILE})) {
249 while(<NOTES>) {
250 chomp;
251 s/^\s+//;
252 my ($a,$b)=split /\s+/,$_,2;
253 $dict{$a}=$b;
254 }
255 }
256 if (open(F, $file)) {
257 @lines=<F>;
258 close(F);
260 if (open(F, ">$file")) {
261 for (@lines) {
262 m/\s+\S+\s+(\S+)/;
263 $name=$1;
264 if (not /^#/ and defined($dict{$name})) {
265 chomp;
266 $mark=$dict{$name};
267 $space=" "x(30-length($_));
268 print F "$_$space$mark\n";
269 }
270 else {
271 print F "$_";
272 }
273 }
274 close(F);
275 }
276 }
277 PERL_SCRIPT
278 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
279 export NOTES_FILE
280 perl $PERL_SCRIPT_TEMP_NAME "$1"
281 rm $PERL_SCRIPT_TEMP_NAME
282 }
284 remove_marks()
285 {
286 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
287 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
288 $file = $ARGV[0];
289 our %dict;
290 if (open(F, $file)) {
291 @lines=<F>;
292 close(F);
294 if (open(F, ">$file")) {
295 for (@lines) {
296 chomp;
297 if (not /^#/ and m/(\s+)(\S+)(\s+)(\S+)(\s+)(.*)/) {
298 my $name=$4;
299 my $comment=$6;
300 $dict{$name}=$comment;
301 print F "$1$2$3$4\n";
302 }
303 else {
304 print F "$_\n";
305 }
306 }
307 }
308 }
309 if (open(NOTES, $ENV{NOTES_FILE})) {
310 @lines=<NOTES>;
311 close(NOTES);
313 if (open(NOTES, ">".$ENV{NOTES_FILE})) {
314 for (@lines) {
315 chomp;
316 s/^\s+//;
317 my ($a,$b)=split /\s+/,$_,2;
318 if (not defined($dict{$a}) || ($dict{$a} eq $b)) {
319 print NOTES "$_\n";
320 if (defined($dict{$a})) { unset($dict{$a}); }
321 }
322 }
323 for (keys %dict) {
324 $mark=$dict{$_};
325 $space=" "x(30-length($_));
326 print NOTES "$_$space$mark\n";
327 }
328 }
329 }
330 PERL_SCRIPT
331 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
332 export NOTES_FILE
333 perl $PERL_SCRIPT_TEMP_NAME "$1"
334 rm $PERL_SCRIPT_TEMP_NAME
335 }
337 mkdir -p $WORK_DIR
338 oldpwd="$PWD"
339 cd $WORK_DIR
340 if echo "$1" | grep -q http:
341 then
342 text_from_url "$1"
343 elif [ "$#" != 0 ]
344 then
345 if echo $1 | grep -q ^/
346 then
347 cat "$1"
348 else
349 cat "$oldpwd/$1"
350 fi
351 else
352 cat
353 fi \
354 | tee $ORIGINAL_TEXT \
355 | get_words ${TEMP1}-full \
356 | group_words \
357 | add_stat ${TEMP1}-full \
358 | tee "$TEMP1" > "$TEMP2"
360 if [ "$STAT_ONLY" = "YES" ]
361 then
362 cat "$TEMP1"
363 else
364 add_marks "$TEMP2"
365 if [ "$editor" = vim ]
366 then
367 vim -c 'set keywordprg='"$LANGUAGE" -c 'set iskeyword=@,48-57,/,.,-,_,+,,,#,$,%,~,=' "$TEMP2" < /dev/tty > /dev/tty
368 else
369 echo 2
370 $editor "$TEMP2"
371 fi
372 remove_marks "$TEMP2"
373 fi
375 diff "$TEMP1" "$TEMP2" | awk '{print $3}' | sort -u >> "$VOCABULARY"
376 rm -f "$TEMP1" "$TEMP2" "${TEMP1}-full" "$ORIGINAL_TEXT"