rev |
line source |
igor@26
|
1 #!/bin/bash
|
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@27
|
17 -a don't add marks (and don't save marks added by user)
|
igor@25
|
18 -p pages work with specified pages only (pages = start-stop/total )
|
igor@20
|
19 -s show the text statistics (percentage of known words and so on) and exit
|
igor@27
|
20 -t tag tag known words with tag
|
igor@27
|
21 -T show list of active tags
|
igor@27
|
22 -m tag merge the words tagged with "tag" into the main vocabulary
|
igor@27
|
23 -M merge the words tagged with any tag into the main vocabulary
|
igor@27
|
24 -r tag remove subvocabulary for the "tag"
|
igor@26
|
25 -2 -3 find 2 and 3 words' sequences
|
igor@16
|
26
|
igor@20
|
27 The language of the text can be specified also
|
igor@20
|
28 by name of the program new-words (correspondent link must be created before).
|
igor@20
|
29 For example, these calls are equivalent:
|
igor@0
|
30
|
igor@20
|
31 de-words URL
|
igor@0
|
32 new-words -l de URL
|
igor@0
|
33
|
igor@20
|
34 HELP
|
igor@20
|
35 }
|
igor@0
|
36
|
igor@20
|
37 if [ "$1" = "-h" ]
|
igor@20
|
38 then
|
igor@20
|
39 show_usage
|
igor@20
|
40 exit 0
|
igor@20
|
41 fi
|
igor@0
|
42
|
igor@0
|
43 WORK_DIR=~/.new-words/
|
igor@28
|
44 TEMP1=`mktemp /tmp/new-words-temp1.XXXXXXXXXX`
|
igor@28
|
45 TEMP2=`mktemp /tmp/new-words-temp2.XXXXXXXXXX`
|
igor@28
|
46 export ORIGINAL_TEXT=`mktemp /tmp/new-words-orig.XXXXXXXXXX`
|
igor@0
|
47 editor=${EDITOR:-vim}
|
igor@0
|
48
|
igor@24
|
49 # language detection
|
igor@21
|
50
|
igor@2
|
51 LANGUAGE=en
|
igor@2
|
52 my_name="`echo $0 | sed s@.*/@@ | sed s/-.*// `"
|
igor@22
|
53 for arg
|
igor@22
|
54 do
|
igor@22
|
55 if echo "$arg" | grep -q http://...wikipedia.org/wiki/
|
igor@22
|
56 then
|
igor@22
|
57 LANGUAGE="`echo $arg | sed s@http://@@ | sed s@.wikipedia.*@@`"
|
igor@22
|
58 fi
|
igor@22
|
59 done
|
igor@2
|
60 [ "${my_name}" = "new" ] || LANGUAGE="$my_name"
|
igor@24
|
61
|
igor@24
|
62 #----------------------------------------------------
|
igor@24
|
63 # command line options processing
|
igor@24
|
64
|
igor@24
|
65 STAT_ONLY=NO
|
igor@24
|
66 NEED_TO_USE_VOCABULARY_WHEN_SORT=NO
|
igor@24
|
67 DONT_ADD_MARKS=NO
|
igor@24
|
68 NON_INTERACTIVE_MODE=NO
|
igor@25
|
69 PART_TO_PROCESS=''
|
igor@26
|
70 GROUP_WORDS_BY_THREE=NO
|
igor@26
|
71 GROUP_WORDS_BY_TWO=NO
|
igor@27
|
72 TAG_NAME=''
|
igor@27
|
73 MERGE_THIS_TAGS=''
|
igor@27
|
74 TAGS_LIST_ONLY=NO
|
igor@27
|
75 MERGE_TAGGED_WORDS=NO
|
igor@27
|
76 MERGE_ALL_TAGGED=NO
|
igor@27
|
77 DONT_ADD_MARKLINES=NO
|
igor@27
|
78 while getopts l:skanp:t:Tm:Mr:23 opt
|
igor@24
|
79 do
|
igor@24
|
80 case "$opt" in
|
igor@24
|
81 s) STAT_ONLY=YES;;
|
igor@24
|
82 k) NEED_TO_USE_VOCABULARY_WHEN_SORT=YES;;
|
igor@24
|
83 l) LANGUAGE="$OPTARG";;
|
igor@27
|
84 a) DONT_ADD_MARKS=YES;;
|
igor@24
|
85 n) NON_INTERACTIVE_MODE=YES;;
|
igor@25
|
86 p) PART_TO_PROCESS="$OPTARG";;
|
igor@27
|
87 t) TAG_NAME="$OPTARG";;
|
igor@27
|
88 T) TAGS_LIST_ONLY="YES";;
|
igor@27
|
89 m) DONT_ADD_MARKLINES="YES"; MERGE_TAGGED_WORDS="YES"; MERGE_THIS_TAGS="$TAG_NAME $OPTARG";;
|
igor@27
|
90 M) DONT_ADD_MARKLINES="YES"; MERGE_ALL_TAGGED="YES";;
|
igor@27
|
91 r) REMOVE_TAG="YES"; TAG_NAME="$TAG_NAME $OPTARG";;
|
igor@26
|
92 2) GROUP_WORDS_BY_TWO=YES;;
|
igor@26
|
93 3) GROUP_WORDS_BY_THREE=YES;;
|
igor@24
|
94 \?) # unknown flag
|
igor@24
|
95 show_usage
|
igor@24
|
96 exit 1;;
|
igor@24
|
97 esac
|
igor@24
|
98 done
|
igor@24
|
99 shift `expr $OPTIND - 1`
|
igor@24
|
100
|
igor@0
|
101 if [ "$1" = "-l" ]
|
igor@0
|
102 then
|
igor@0
|
103 LANGUAGE="$2"
|
igor@0
|
104 shift 2
|
igor@0
|
105 fi
|
igor@24
|
106
|
igor@2
|
107 VOCABULARY=${LANGUAGE}.txt
|
igor@2
|
108 NOTES_FILE=notes-${LANGUAGE}.txt
|
igor@0
|
109
|
igor@21
|
110 #----------------------------------------------------
|
igor@21
|
111
|
igor@0
|
112 get_words()
|
igor@0
|
113 {
|
igor@1
|
114 tr ' ' '\n' | sed 's/--/ /g' \
|
igor@3
|
115 | sed "s/'/__APOSTROPHE__/g" \
|
igor@31
|
116 | perl -MEncode -Mutf8 -n -e '$_ = decode( "utf8", $_);y/*\r,.:#@()+=—<>$;"?!|·[]^%&/ /; binmode STDOUT, ":utf8"; print if /^[[:alpha:] '"'"'_-]*$/'\
|
igor@26
|
117 | sed "s/__APOSTROPHE__/'/g" \
|
igor@3
|
118 | tr ' ' '\n' \
|
igor@3
|
119 | tee "$1" \
|
igor@3
|
120 | grep_v_english_perl \
|
igor@0
|
121 | sort | uniq -c | awk '{if ($2!="") print;}' | sort -rn
|
igor@0
|
122 }
|
igor@0
|
123
|
igor@3
|
124 add_stat()
|
igor@3
|
125 {
|
igor@27
|
126 if [ "$DONT_ADD_MARKLINES" = "YES" ]
|
igor@27
|
127 then
|
igor@27
|
128 cat
|
igor@27
|
129 return
|
igor@27
|
130 fi
|
igor@3
|
131 before="$1"
|
igor@3
|
132 after=${before}2
|
igor@3
|
133 cat > "$after"
|
igor@3
|
134 total="`wc -w $1 | awk '{print $1}'`"
|
igor@3
|
135 total_unknown="`cat $after|awk '{s=s+$1}END{print s}'`"
|
igor@3
|
136 total_known="`echo $total-$total_unknown|bc`"
|
igor@3
|
137 percentage="`echo '100*('$total-$total_unknown')'/$total | bc -l | sed 's/\\.\(.\).*/.\1/'`"
|
igor@18
|
138 #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
|
139 sentences="`cat $ORIGINAL_TEXT | perl -e 'local $/; $_=<>; s/[^.]//msg; print length($_);'`"
|
igor@18
|
140
|
igor@18
|
141
|
igor@16
|
142 if [ "$STAT_ONLY" = "YES" ]
|
igor@16
|
143 then
|
igor@18
|
144 echo "LANG KNOWN% UNKNOWN% KNOWN TOTAL WPS UWPS*10"
|
igor@18
|
145 echo "$LANGUAGE $percentage `echo \(100-$percentage\) | bc -l` $total_known $total `echo $total/$sentences|bc` `echo 10*$total_unknown/$sentences|bc` "
|
igor@17
|
146 rm $after
|
igor@16
|
147 return 0
|
igor@16
|
148 else
|
igor@16
|
149 echo "# $LANGUAGE, $percentage, <$total_known/$total>"
|
igor@16
|
150 fi
|
igor@16
|
151
|
igor@14
|
152 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
|
igor@14
|
153 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
|
igor@14
|
154 my $total=shift(@ARGV);
|
igor@14
|
155 my $total_known=shift(@ARGV);
|
igor@14
|
156 my $s=0;
|
igor@16
|
157 my $mark_line=int($total_known*100/$total/5)*5;
|
igor@19
|
158 if ($mark_line>=90) {
|
igor@19
|
159 $mark_line=int($total_known*100/$total)+1;
|
igor@19
|
160 } else { $mark_line +=5; };
|
igor@14
|
161 while(<>)
|
igor@14
|
162 {
|
igor@14
|
163 print;
|
igor@14
|
164 /^\s*([0-9]*)\s*/;
|
igor@14
|
165 $s+=$1;
|
igor@19
|
166 if (($total_known+$s)*100/$total>=$mark_line) {
|
igor@14
|
167 print "# $mark_line\n";
|
igor@14
|
168 if ($mark_line>=90) { $mark_line+=1; } else { $mark_line +=5; };
|
igor@14
|
169 }
|
igor@14
|
170 }
|
igor@14
|
171 PERL_SCRIPT
|
igor@14
|
172 perl $PERL_SCRIPT_TEMP_NAME "$total" "$total_known" "$after"
|
igor@14
|
173 rm $PERL_SCRIPT_TEMP_NAME
|
igor@3
|
174 rm $after
|
igor@3
|
175 }
|
igor@3
|
176
|
igor@26
|
177 two_and_three_words()
|
igor@26
|
178 {
|
igor@26
|
179 if [ $GROUP_WORDS_BY_THREE = NO -a $GROUP_WORDS_BY_TWO = NO ]
|
igor@26
|
180 then
|
igor@26
|
181 cat
|
igor@26
|
182 else
|
igor@26
|
183 cat
|
igor@26
|
184
|
igor@26
|
185 export GROUP_WORDS_BY_THREE
|
igor@26
|
186 export GROUP_WORDS_BY_TWO
|
igor@26
|
187 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-two-and-three-XXXXXXXX`
|
igor@26
|
188 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
|
igor@26
|
189 #!/usr/bin/perl
|
igor@26
|
190 local $/;
|
igor@26
|
191 $words=<>;
|
igor@26
|
192 $words=~ s@[!?;,:#1-9".]@ @g;
|
igor@26
|
193 $words =~ s@\s+@ @g;
|
igor@26
|
194 @words = split /\s+/, $words;
|
igor@26
|
195 for ($i=0; $i<$#words-3;$i++) {
|
igor@26
|
196 my ($a,$b,$c)= ($words[$i],$words[$i+1],$words[$i+2]);
|
igor@26
|
197 if ($ENV{GROUP_WORDS_BY_THREE} eq "YES" and ($a && $b && $c)) {
|
igor@26
|
198 print "${a}_${b}_${c}\n";
|
igor@26
|
199 };
|
igor@26
|
200 if ($ENV{GROUP_WORDS_BY_TWO} eq "YES" and ($a && $b)) {
|
igor@26
|
201 print "${a}_${b}\n";
|
igor@26
|
202 };
|
igor@26
|
203 }
|
igor@26
|
204 PERL_SCRIPT
|
igor@26
|
205 perl $PERL_SCRIPT_TEMP_NAME "$ORIGINAL_TEXT"
|
igor@26
|
206 rm $PERL_SCRIPT_TEMP_NAME
|
igor@26
|
207 fi
|
igor@26
|
208 }
|
igor@26
|
209
|
igor@0
|
210 grep_v_english()
|
igor@0
|
211 {
|
igor@0
|
212 [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
|
igor@0
|
213 eval $(cat $VOCABULARY | tr -d "'" | xargs -n10 echo | tr ' ' '|' | sed 's/^/egrep -xv "RRRRRRR|/' | sed 's/$/"/' | tr '\n' '|')cat
|
igor@0
|
214 }
|
igor@0
|
215
|
igor@0
|
216 grep_v_english_perl()
|
igor@0
|
217 {
|
igor@0
|
218 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
|
igor@0
|
219 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
|
igor@27
|
220 $voc_files=$ENV{VOC_FILES};
|
igor@27
|
221 $voc_files=~s@^ @@;
|
igor@27
|
222 for $voc_file (split /\s+/,$voc_files) {
|
igor@27
|
223 if (open(VOC, $voc_file)) {
|
igor@27
|
224 while (<VOC>){
|
igor@27
|
225 chomp;
|
igor@27
|
226 #s/'//g;
|
igor@27
|
227 $voc{$_}="1";
|
igor@27
|
228 }
|
igor@27
|
229 }
|
igor@0
|
230 }
|
igor@0
|
231 while(<>) {
|
igor@0
|
232 chomp;
|
igor@0
|
233 if (not defined($voc{$_})) { print "$_\n"; }
|
igor@0
|
234 }
|
igor@0
|
235 PERL_SCRIPT
|
igor@0
|
236 [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
|
igor@27
|
237 export VOCABULARY VOC_FILES
|
igor@27
|
238 VOC_FILES=$VOCABULARY
|
igor@27
|
239 for i in $TAG_NAME
|
igor@27
|
240 do
|
igor@27
|
241 VOC_FILES="${VOC_FILES} `tag_file_name $i`"
|
igor@27
|
242 done
|
igor@0
|
243 perl $PERL_SCRIPT_TEMP_NAME
|
igor@0
|
244 rm $PERL_SCRIPT_TEMP_NAME
|
igor@0
|
245 }
|
igor@0
|
246
|
igor@11
|
247 group_words()
|
igor@11
|
248 {
|
igor@22
|
249 #if [ "$LANGUAGE" != "en" ]
|
igor@22
|
250 #then
|
igor@22
|
251 # cat
|
igor@22
|
252 # return
|
igor@22
|
253 #fi
|
igor@11
|
254 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-group-words-XXXXXXXX`
|
igor@11
|
255 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
|
igor@11
|
256 #!/usr/bin/perl
|
igor@11
|
257
|
igor@29
|
258 use Encode;
|
igor@29
|
259 use utf8;
|
igor@29
|
260
|
igor@12
|
261 eval {
|
igor@12
|
262 # http://stackoverflow.com/questions/251694/how-can-i-check-if-i-have-a-perl-module-before-using-it
|
igor@12
|
263 require String::Similarity;
|
igor@12
|
264 String::Similarity->import();
|
igor@12
|
265 };
|
igor@12
|
266 unless($@)
|
igor@12
|
267 {
|
igor@12
|
268 our $HAVE_String_Similarity=1;
|
igor@12
|
269 }
|
igor@12
|
270
|
igor@12
|
271 sub similar($$){
|
igor@12
|
272 my $a=shift;
|
igor@12
|
273 my $b=shift;
|
igor@12
|
274 if ($HAVE_String_Similarity) {
|
igor@12
|
275 return $Similarity{"$a $b"};
|
igor@12
|
276 }
|
igor@12
|
277 else {
|
igor@12
|
278 return 0;
|
igor@12
|
279 }
|
igor@12
|
280 }
|
igor@12
|
281
|
igor@11
|
282 sub normalize($)
|
igor@11
|
283 {
|
igor@22
|
284 if ( $ENV{LANGUAGE} eq "en" ) { return normalize_english(shift); }
|
igor@22
|
285 elsif ( $ENV{LANGUAGE} eq "de" ) { return normalize_german(shift); }
|
igor@29
|
286 elsif ( $ENV{LANGUAGE} eq "uk" ) { return normalize_ukrainian(shift); }
|
igor@31
|
287 elsif ( $ENV{LANGUAGE} eq "io" ) { return normalize_esperanto(shift); }
|
igor@22
|
288 else { return shift ; }
|
igor@22
|
289 }
|
igor@22
|
290
|
igor@29
|
291 sub normalize_ukrainian($)
|
igor@29
|
292 {
|
igor@29
|
293 $_=lc(shift);
|
igor@29
|
294 s/[юіоеуаи]$//g;
|
igor@29
|
295 return $_;
|
igor@29
|
296 }
|
igor@29
|
297
|
igor@31
|
298 sub normalize_esperanto($)
|
igor@31
|
299 {
|
igor@31
|
300 $_=lc(shift);
|
igor@31
|
301 # verbs
|
igor@31
|
302 s/i$//; s/is$//; s/os$//; s/as$//; s/us$//;
|
igor@31
|
303
|
igor@31
|
304 # nouns
|
igor@31
|
305 s/j?n?$//;
|
igor@31
|
306
|
igor@31
|
307 return $_;
|
igor@31
|
308 }
|
igor@31
|
309
|
igor@22
|
310 sub normalize_german($)
|
igor@22
|
311 {
|
igor@22
|
312 $_=lc(shift);
|
igor@22
|
313
|
igor@22
|
314 s/heit$//; s/keit$//; s/tum$//; s/ung$//; s/nis$//;s/schaft$//; s/ist$//;
|
igor@22
|
315 s/en$//; s/er$//;
|
igor@22
|
316
|
igor@22
|
317 s/lich$//; s/ig$//;
|
igor@22
|
318 s/al$//; s/isch$//;
|
igor@22
|
319 s/ell$//; s/haft$//;
|
igor@22
|
320
|
igor@22
|
321 s/bar$//; s/sam$//; s/lich$//;
|
igor@22
|
322
|
igor@22
|
323 @prefixes=qw(
|
igor@22
|
324 ab an auf aus bei dazwischen ein fest heraus her hinaus hin los mit nach voraus vorbei vor weg weiter zurück zusammen zu
|
igor@22
|
325 be emp ent er ge miss ver zer durch über um unter wieder);
|
igor@24
|
326 @prefixes=();
|
igor@22
|
327 for $pref (@prefixes) {
|
igor@22
|
328 s/^$pref//;
|
igor@22
|
329 }
|
igor@22
|
330
|
igor@22
|
331
|
igor@22
|
332 return $_;
|
igor@22
|
333 }
|
igor@22
|
334
|
igor@22
|
335 sub normalize_english($)
|
igor@22
|
336 {
|
igor@11
|
337 $_=lc(shift);
|
igor@12
|
338
|
igor@11
|
339 s/s$//;
|
igor@22
|
340
|
igor@22
|
341 s/ation$//; s/ness$//; s/ship$//; s/ally$//; s/ance$//;s/ity$//; s/ment$//;
|
igor@22
|
342
|
igor@11
|
343 s/ed$//;
|
igor@12
|
344 s/en$//;
|
igor@12
|
345 s/er$//;
|
igor@12
|
346 s/est$//;
|
igor@11
|
347 s/ing$//;
|
igor@12
|
348
|
igor@12
|
349 s/ism$//; s/ist$//; s/ful$//; s/able$//; s/ably$//;
|
igor@22
|
350 s/ify$//; s/fy$//; s/ly$//;
|
igor@12
|
351 s/ise$//; s/ize$//;
|
igor@12
|
352
|
igor@12
|
353 s/e$//;
|
igor@11
|
354 return $_;
|
igor@11
|
355 }
|
igor@11
|
356
|
igor@12
|
357
|
igor@11
|
358 sub compare($$)
|
igor@11
|
359 {
|
igor@11
|
360 my $a=shift;
|
igor@11
|
361 my $b=shift;
|
igor@11
|
362 $a =~ s/^\s*//;
|
igor@11
|
363 $b =~ s/^\s*//;
|
igor@11
|
364 my ($a1, $a2)= split /\s+/,$a,2;
|
igor@11
|
365 my ($b1, $b2)= split /\s+/,$b,2;
|
igor@11
|
366
|
igor@11
|
367 my $cmp = $group_weight{normalize($a2)} <=> $group_weight{normalize($b2)};
|
igor@12
|
368
|
igor@11
|
369 if ($cmp) {
|
igor@11
|
370 return $cmp;
|
igor@11
|
371 }
|
igor@11
|
372 else {
|
igor@11
|
373 if (normalize($a2) ne normalize($b2)) {
|
igor@11
|
374 return normalize($a2) cmp normalize($b2);
|
igor@11
|
375 }
|
igor@11
|
376 else {
|
igor@11
|
377 return $a1 <=> $b1;
|
igor@11
|
378 }
|
igor@11
|
379 }
|
igor@11
|
380 }
|
igor@11
|
381
|
igor@20
|
382 our %Vocabulary;
|
igor@20
|
383 open(VOC, $ENV{VOCABULARY})
|
igor@20
|
384 or die "Can't open VOCABULARY";
|
igor@20
|
385 while (<VOC>){
|
igor@20
|
386 chomp;
|
igor@20
|
387 #s/'//g;
|
igor@20
|
388 $Vocabulary{normalize($_)}="1";
|
igor@20
|
389 }
|
igor@20
|
390 close(VOC);
|
igor@20
|
391
|
igor@29
|
392 binmode STDIN,":utf8";
|
igor@20
|
393 @lines=<STDIN>;
|
igor@11
|
394 for $L (@lines) {
|
igor@11
|
395 chomp($L);
|
igor@29
|
396 #$L = decode( "utf8", $L);
|
igor@11
|
397 $l=$L;
|
igor@11
|
398 $l =~ s/^\s*//;
|
igor@11
|
399 my ($a, $b)=split(/\s+/,$l,2);
|
igor@11
|
400 $group_weight{normalize($b)}+=$a;
|
igor@11
|
401 }
|
igor@20
|
402 if ($ENV{NEED_TO_USE_VOCABULARY_WHEN_SORT} eq "YES") {
|
igor@20
|
403 for $k (keys %group_weight) {
|
igor@20
|
404 if (defined($Vocabulary{$k})) {
|
igor@20
|
405 $group_weight{$k} *= 2;
|
igor@20
|
406 }
|
igor@20
|
407 }
|
igor@20
|
408 }
|
igor@11
|
409 @lines2 = sort { compare($b,$a) } @lines;
|
igor@29
|
410 binmode STDOUT, ":utf8";
|
igor@11
|
411 for $l (@lines2) {
|
igor@11
|
412 print "$l\n";
|
igor@11
|
413 }
|
igor@11
|
414 PERL_SCRIPT
|
igor@20
|
415 export VOCABULARY
|
igor@20
|
416 export NEED_TO_USE_VOCABULARY_WHEN_SORT
|
igor@22
|
417 export LANGUAGE
|
igor@11
|
418 perl $PERL_SCRIPT_TEMP_NAME
|
igor@11
|
419 rm $PERL_SCRIPT_TEMP_NAME
|
igor@11
|
420 }
|
igor@11
|
421
|
igor@0
|
422 text_from_url()
|
igor@0
|
423 {
|
igor@0
|
424 lynx -dump "$1" | perl -p -e 's@http://[a-zA-Z&_.:/0-9%?=,#+()\[\]~-]*@@'
|
igor@0
|
425 }
|
igor@0
|
426
|
igor@2
|
427 add_marks()
|
igor@2
|
428 {
|
igor@2
|
429 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
|
igor@2
|
430 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
|
igor@29
|
431 use Encode;
|
igor@29
|
432
|
igor@2
|
433 $file = $ARGV[0];
|
igor@2
|
434 our $dict;
|
igor@2
|
435 if (open(NOTES, $ENV{NOTES_FILE})) {
|
igor@2
|
436 while(<NOTES>) {
|
igor@29
|
437 $_ = decode( "utf8", $_);
|
igor@2
|
438 chomp;
|
igor@2
|
439 s/^\s+//;
|
igor@2
|
440 my ($a,$b)=split /\s+/,$_,2;
|
igor@2
|
441 $dict{$a}=$b;
|
igor@2
|
442 }
|
igor@2
|
443 }
|
igor@2
|
444 if (open(F, $file)) {
|
igor@2
|
445 @lines=<F>;
|
igor@2
|
446 close(F);
|
igor@29
|
447 for (@lines) {$_ = decode( "utf8", $_);};
|
igor@2
|
448
|
igor@2
|
449 if (open(F, ">$file")) {
|
igor@29
|
450 binmode F, ":utf8";
|
igor@2
|
451 for (@lines) {
|
igor@2
|
452 m/\s+\S+\s+(\S+)/;
|
igor@2
|
453 $name=$1;
|
igor@3
|
454 if (not /^#/ and defined($dict{$name})) {
|
igor@2
|
455 chomp;
|
igor@2
|
456 $mark=$dict{$name};
|
igor@2
|
457 $space=" "x(30-length($_));
|
igor@2
|
458 print F "$_$space$mark\n";
|
igor@2
|
459 }
|
igor@2
|
460 else {
|
igor@2
|
461 print F "$_";
|
igor@2
|
462 }
|
igor@2
|
463 }
|
igor@2
|
464 close(F);
|
igor@2
|
465 }
|
igor@2
|
466 }
|
igor@2
|
467 PERL_SCRIPT
|
igor@2
|
468 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
|
igor@2
|
469 export NOTES_FILE
|
igor@2
|
470 perl $PERL_SCRIPT_TEMP_NAME "$1"
|
igor@2
|
471 rm $PERL_SCRIPT_TEMP_NAME
|
igor@2
|
472 }
|
igor@2
|
473
|
igor@2
|
474 remove_marks()
|
igor@2
|
475 {
|
igor@2
|
476 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
|
igor@2
|
477 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
|
igor@2
|
478 $file = $ARGV[0];
|
igor@2
|
479 our %dict;
|
igor@2
|
480 if (open(F, $file)) {
|
igor@2
|
481 @lines=<F>;
|
igor@2
|
482 close(F);
|
igor@2
|
483
|
igor@2
|
484 if (open(F, ">$file")) {
|
igor@2
|
485 for (@lines) {
|
igor@2
|
486 chomp;
|
igor@3
|
487 if (not /^#/ and m/(\s+)(\S+)(\s+)(\S+)(\s+)(.*)/) {
|
igor@2
|
488 my $name=$4;
|
igor@2
|
489 my $comment=$6;
|
igor@2
|
490 $dict{$name}=$comment;
|
igor@2
|
491 print F "$1$2$3$4\n";
|
igor@2
|
492 }
|
igor@2
|
493 else {
|
igor@2
|
494 print F "$_\n";
|
igor@2
|
495 }
|
igor@2
|
496 }
|
igor@2
|
497 }
|
igor@2
|
498 }
|
igor@21
|
499 if (($ENV{DONT_ADD_MARKS} ne "YES") and open(NOTES, $ENV{NOTES_FILE})) {
|
igor@2
|
500 @lines=<NOTES>;
|
igor@2
|
501 close(NOTES);
|
igor@2
|
502
|
igor@2
|
503 if (open(NOTES, ">".$ENV{NOTES_FILE})) {
|
igor@2
|
504 for (@lines) {
|
igor@2
|
505 chomp;
|
igor@2
|
506 s/^\s+//;
|
igor@2
|
507 my ($a,$b)=split /\s+/,$_,2;
|
igor@2
|
508 if (not defined($dict{$a}) || ($dict{$a} eq $b)) {
|
igor@2
|
509 print NOTES "$_\n";
|
igor@2
|
510 if (defined($dict{$a})) { unset($dict{$a}); }
|
igor@2
|
511 }
|
igor@2
|
512 }
|
igor@2
|
513 for (keys %dict) {
|
igor@2
|
514 $mark=$dict{$_};
|
igor@2
|
515 $space=" "x(30-length($_));
|
igor@2
|
516 print NOTES "$_$space$mark\n";
|
igor@2
|
517 }
|
igor@2
|
518 }
|
igor@2
|
519 }
|
igor@2
|
520 PERL_SCRIPT
|
igor@2
|
521 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
|
igor@2
|
522 export NOTES_FILE
|
igor@21
|
523 export DONT_ADD_MARKS
|
igor@2
|
524 perl $PERL_SCRIPT_TEMP_NAME "$1"
|
igor@2
|
525 rm $PERL_SCRIPT_TEMP_NAME
|
igor@2
|
526 }
|
igor@2
|
527
|
igor@25
|
528 part()
|
igor@25
|
529 {
|
igor@25
|
530 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-part-XXXXXXXX`
|
igor@25
|
531 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
|
igor@25
|
532 #!/usr/bin/perl
|
igor@25
|
533
|
igor@25
|
534 my @lines=<STDIN>;
|
igor@25
|
535 my $lines=$#lines;
|
igor@25
|
536 my $interval=$ARGV[0];
|
igor@25
|
537 if (not $interval) {
|
igor@25
|
538 print @lines;
|
igor@25
|
539 }
|
igor@25
|
540 else {
|
igor@25
|
541 my ($start,$stop,$total);
|
igor@25
|
542 if ($interval =~ m@(.*)/(.*)@) {
|
igor@25
|
543 $start = $1;
|
igor@25
|
544 $total = $2;
|
igor@25
|
545 }
|
igor@25
|
546 else {
|
igor@25
|
547 $start=$interval;
|
igor@25
|
548 $total=0;
|
igor@25
|
549 }
|
igor@25
|
550 if ($start =~ m@(.*)-(.*)@) {
|
igor@25
|
551 $start = $1;
|
igor@25
|
552 $stop = $2;
|
igor@25
|
553 }
|
igor@25
|
554 if ($start =~ m@(.*)\+(.*)@) {
|
igor@25
|
555 $start = $1;
|
igor@25
|
556 $stop = $start+$2;
|
igor@25
|
557 }
|
igor@25
|
558
|
igor@25
|
559 $start=int($lines/$total*$start);
|
igor@25
|
560 $stop=int($lines/$total*$stop);
|
igor@25
|
561
|
igor@25
|
562 for($i=$start;$i<$stop;$i++){
|
igor@25
|
563 print $lines[$i];
|
igor@25
|
564 }
|
igor@25
|
565 }
|
igor@25
|
566 PERL_SCRIPT
|
igor@25
|
567 perl $PERL_SCRIPT_TEMP_NAME "$1"
|
igor@25
|
568 rm $PERL_SCRIPT_TEMP_NAME
|
igor@25
|
569 }
|
igor@25
|
570
|
igor@27
|
571 if [ "$TAGS_LIST_ONLY" = "YES" ]
|
igor@27
|
572 then
|
igor@27
|
573 cd "${WORK_DIR}"
|
igor@27
|
574 echo ${LANGUAGE}_*.txt | tr ' ' '\n' | grep -v '*' | sed 's/[^_]*_//;s/.txt$//'
|
igor@27
|
575 exit 0
|
igor@27
|
576 fi
|
igor@27
|
577
|
igor@27
|
578 tag_file_name()
|
igor@27
|
579 {
|
igor@27
|
580 echo "${LANGUAGE}_${1}.txt"
|
igor@27
|
581 }
|
igor@27
|
582
|
igor@27
|
583 if [ "$REMOVE_TAG" = "YES" ]
|
igor@27
|
584 then
|
igor@27
|
585 cd "${WORK_DIR}"
|
igor@27
|
586 for i in $TAG_NAME
|
igor@27
|
587 do
|
igor@27
|
588 echo "$TAGNAME" | grep -q '[/*?]' && continue
|
igor@27
|
589 f="`tag_file_name $i`"
|
igor@27
|
590 if [ -e "$f" ]
|
igor@27
|
591 then
|
igor@27
|
592 rm -f "$f" && echo Tag "'$i'" removed
|
igor@27
|
593 else
|
igor@27
|
594 echo Unknown tag "'$i'"
|
igor@27
|
595 fi
|
igor@27
|
596 done
|
igor@27
|
597 exit 0
|
igor@27
|
598 fi
|
igor@27
|
599
|
igor@0
|
600 mkdir -p $WORK_DIR
|
igor@8
|
601 oldpwd="$PWD"
|
igor@0
|
602 cd $WORK_DIR
|
igor@27
|
603 if [ "$MERGE_TAGGED_WORDS" = "YES" ]
|
igor@27
|
604 then
|
igor@27
|
605 VOC_FILES=''
|
igor@27
|
606 for i in $MERGE_THIS_TAGS
|
igor@27
|
607 do
|
igor@27
|
608 f=`tag_file_name $i`
|
igor@27
|
609 [ -e "$f" ] && VOC_FILES="${VOC_FILES} $f"
|
igor@27
|
610 done
|
igor@27
|
611 if [ -z "$VOC_FILES" ]
|
igor@27
|
612 then
|
igor@27
|
613 echo Unknown tags: $MERGE_THIS_TAGS > /dev/stderr
|
igor@27
|
614 else
|
igor@27
|
615 cat $VOC_FILES
|
igor@27
|
616 fi
|
igor@27
|
617 elif [ "$MERGE_ALL_TAGGED" = "YES" ]
|
igor@27
|
618 then
|
igor@27
|
619 cat ${LANGUAGE}_*.txt
|
igor@27
|
620 elif echo "$1" | grep -q http:
|
igor@0
|
621 then
|
igor@11
|
622 text_from_url "$1"
|
igor@0
|
623 elif [ "$#" != 0 ]
|
igor@0
|
624 then
|
igor@13
|
625 if echo $1 | grep -q ^/
|
igor@13
|
626 then
|
igor@13
|
627 cat "$1"
|
igor@13
|
628 else
|
igor@13
|
629 cat "$oldpwd/$1"
|
igor@13
|
630 fi
|
igor@0
|
631 else
|
igor@11
|
632 cat
|
igor@11
|
633 fi \
|
igor@25
|
634 | part $PART_TO_PROCESS \
|
igor@11
|
635 | tee $ORIGINAL_TEXT \
|
igor@26
|
636 | two_and_three_words \
|
igor@11
|
637 | get_words ${TEMP1}-full \
|
igor@11
|
638 | group_words \
|
igor@11
|
639 | add_stat ${TEMP1}-full \
|
igor@11
|
640 | tee "$TEMP1" > "$TEMP2"
|
igor@0
|
641
|
igor@16
|
642 if [ "$STAT_ONLY" = "YES" ]
|
igor@0
|
643 then
|
igor@16
|
644 cat "$TEMP1"
|
igor@21
|
645 elif [ "$NON_INTERACTIVE_MODE" = "YES" ]
|
igor@21
|
646 then
|
igor@21
|
647 cat "$TEMP1"
|
igor@0
|
648 else
|
igor@27
|
649 if [ `wc -l "$TEMP2" | awk '{print $1}'` != 0 ]
|
igor@16
|
650 then
|
igor@27
|
651 [ "$DONT_ADD_MARKS" = "YES" ] || add_marks "$TEMP2"
|
igor@27
|
652 if [ "$editor" = vim ]
|
igor@27
|
653 then
|
igor@27
|
654 vim -c 'set keywordprg='"$LANGUAGE" -c 'set iskeyword=@,48-57,/,.,-,_,+,,,#,$,%,~,=,48-255' "$TEMP2" < /dev/tty > /dev/tty
|
igor@27
|
655 else
|
igor@27
|
656 $editor "$TEMP2"
|
igor@27
|
657 fi
|
igor@27
|
658 remove_marks "$TEMP2"
|
igor@27
|
659
|
igor@27
|
660 vocabulary="$VOCABULARY"
|
igor@27
|
661 [ -n "$TAG_NAME" ] && vocabulary="`tag_file_name $TAG_NAME`"
|
igor@27
|
662 diff "$TEMP1" "$TEMP2" | awk '{print $3}' | sort -u >> "$vocabulary"
|
igor@16
|
663 fi
|
igor@0
|
664 fi
|
igor@2
|
665
|
igor@5
|
666 rm -f "$TEMP1" "$TEMP2" "${TEMP1}-full" "$ORIGINAL_TEXT"
|