new-words

view new-words.sh @ 20:23f949c03f95

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