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@26
|
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@12
|
258 eval {
|
igor@12
|
259 # http://stackoverflow.com/questions/251694/how-can-i-check-if-i-have-a-perl-module-before-using-it
|
igor@12
|
260 require String::Similarity;
|
igor@12
|
261 String::Similarity->import();
|
igor@12
|
262 };
|
igor@12
|
263 unless($@)
|
igor@12
|
264 {
|
igor@12
|
265 our $HAVE_String_Similarity=1;
|
igor@12
|
266 }
|
igor@12
|
267
|
igor@12
|
268 sub similar($$){
|
igor@12
|
269 my $a=shift;
|
igor@12
|
270 my $b=shift;
|
igor@12
|
271 if ($HAVE_String_Similarity) {
|
igor@12
|
272 return $Similarity{"$a $b"};
|
igor@12
|
273 }
|
igor@12
|
274 else {
|
igor@12
|
275 return 0;
|
igor@12
|
276 }
|
igor@12
|
277 }
|
igor@12
|
278
|
igor@11
|
279 sub normalize($)
|
igor@11
|
280 {
|
igor@22
|
281 if ( $ENV{LANGUAGE} eq "en" ) { return normalize_english(shift); }
|
igor@22
|
282 elsif ( $ENV{LANGUAGE} eq "de" ) { return normalize_german(shift); }
|
igor@22
|
283 else { return shift ; }
|
igor@22
|
284 }
|
igor@22
|
285
|
igor@22
|
286 sub normalize_german($)
|
igor@22
|
287 {
|
igor@22
|
288 $_=lc(shift);
|
igor@22
|
289
|
igor@22
|
290 s/heit$//; s/keit$//; s/tum$//; s/ung$//; s/nis$//;s/schaft$//; s/ist$//;
|
igor@22
|
291 s/en$//; s/er$//;
|
igor@22
|
292
|
igor@22
|
293 s/lich$//; s/ig$//;
|
igor@22
|
294 s/al$//; s/isch$//;
|
igor@22
|
295 s/ell$//; s/haft$//;
|
igor@22
|
296
|
igor@22
|
297 s/bar$//; s/sam$//; s/lich$//;
|
igor@22
|
298
|
igor@22
|
299 @prefixes=qw(
|
igor@22
|
300 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
|
301 be emp ent er ge miss ver zer durch über um unter wieder);
|
igor@24
|
302 @prefixes=();
|
igor@22
|
303 for $pref (@prefixes) {
|
igor@22
|
304 s/^$pref//;
|
igor@22
|
305 }
|
igor@22
|
306
|
igor@22
|
307
|
igor@22
|
308 return $_;
|
igor@22
|
309 }
|
igor@22
|
310
|
igor@22
|
311 sub normalize_english($)
|
igor@22
|
312 {
|
igor@11
|
313 $_=lc(shift);
|
igor@12
|
314
|
igor@11
|
315 s/s$//;
|
igor@22
|
316
|
igor@22
|
317 s/ation$//; s/ness$//; s/ship$//; s/ally$//; s/ance$//;s/ity$//; s/ment$//;
|
igor@22
|
318
|
igor@11
|
319 s/ed$//;
|
igor@12
|
320 s/en$//;
|
igor@12
|
321 s/er$//;
|
igor@12
|
322 s/est$//;
|
igor@11
|
323 s/ing$//;
|
igor@12
|
324
|
igor@12
|
325 s/ism$//; s/ist$//; s/ful$//; s/able$//; s/ably$//;
|
igor@22
|
326 s/ify$//; s/fy$//; s/ly$//;
|
igor@12
|
327 s/ise$//; s/ize$//;
|
igor@12
|
328
|
igor@12
|
329 s/e$//;
|
igor@11
|
330 return $_;
|
igor@11
|
331 }
|
igor@11
|
332
|
igor@12
|
333
|
igor@11
|
334 sub compare($$)
|
igor@11
|
335 {
|
igor@11
|
336 my $a=shift;
|
igor@11
|
337 my $b=shift;
|
igor@11
|
338 $a =~ s/^\s*//;
|
igor@11
|
339 $b =~ s/^\s*//;
|
igor@11
|
340 my ($a1, $a2)= split /\s+/,$a,2;
|
igor@11
|
341 my ($b1, $b2)= split /\s+/,$b,2;
|
igor@11
|
342
|
igor@11
|
343 my $cmp = $group_weight{normalize($a2)} <=> $group_weight{normalize($b2)};
|
igor@12
|
344
|
igor@11
|
345 if ($cmp) {
|
igor@11
|
346 return $cmp;
|
igor@11
|
347 }
|
igor@11
|
348 else {
|
igor@11
|
349 if (normalize($a2) ne normalize($b2)) {
|
igor@11
|
350 return normalize($a2) cmp normalize($b2);
|
igor@11
|
351 }
|
igor@11
|
352 else {
|
igor@11
|
353 return $a1 <=> $b1;
|
igor@11
|
354 }
|
igor@11
|
355 }
|
igor@11
|
356 }
|
igor@11
|
357
|
igor@20
|
358 our %Vocabulary;
|
igor@20
|
359 open(VOC, $ENV{VOCABULARY})
|
igor@20
|
360 or die "Can't open VOCABULARY";
|
igor@20
|
361 while (<VOC>){
|
igor@20
|
362 chomp;
|
igor@20
|
363 #s/'//g;
|
igor@20
|
364 $Vocabulary{normalize($_)}="1";
|
igor@20
|
365 }
|
igor@20
|
366 close(VOC);
|
igor@20
|
367
|
igor@20
|
368 @lines=<STDIN>;
|
igor@11
|
369 for $L (@lines) {
|
igor@11
|
370 chomp($L);
|
igor@11
|
371 $l=$L;
|
igor@11
|
372 $l =~ s/^\s*//;
|
igor@11
|
373 my ($a, $b)=split(/\s+/,$l,2);
|
igor@11
|
374 $group_weight{normalize($b)}+=$a;
|
igor@11
|
375 }
|
igor@20
|
376 if ($ENV{NEED_TO_USE_VOCABULARY_WHEN_SORT} eq "YES") {
|
igor@20
|
377 for $k (keys %group_weight) {
|
igor@20
|
378 if (defined($Vocabulary{$k})) {
|
igor@20
|
379 $group_weight{$k} *= 2;
|
igor@20
|
380 }
|
igor@20
|
381 }
|
igor@20
|
382 }
|
igor@11
|
383 @lines2 = sort { compare($b,$a) } @lines;
|
igor@11
|
384 for $l (@lines2) {
|
igor@11
|
385 print "$l\n";
|
igor@11
|
386 }
|
igor@11
|
387 PERL_SCRIPT
|
igor@20
|
388 export VOCABULARY
|
igor@20
|
389 export NEED_TO_USE_VOCABULARY_WHEN_SORT
|
igor@22
|
390 export LANGUAGE
|
igor@11
|
391 perl $PERL_SCRIPT_TEMP_NAME
|
igor@11
|
392 rm $PERL_SCRIPT_TEMP_NAME
|
igor@11
|
393 }
|
igor@11
|
394
|
igor@0
|
395 text_from_url()
|
igor@0
|
396 {
|
igor@0
|
397 lynx -dump "$1" | perl -p -e 's@http://[a-zA-Z&_.:/0-9%?=,#+()\[\]~-]*@@'
|
igor@0
|
398 }
|
igor@0
|
399
|
igor@2
|
400 add_marks()
|
igor@2
|
401 {
|
igor@2
|
402 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
|
igor@2
|
403 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
|
igor@2
|
404 $file = $ARGV[0];
|
igor@2
|
405 our $dict;
|
igor@2
|
406 if (open(NOTES, $ENV{NOTES_FILE})) {
|
igor@2
|
407 while(<NOTES>) {
|
igor@2
|
408 chomp;
|
igor@2
|
409 s/^\s+//;
|
igor@2
|
410 my ($a,$b)=split /\s+/,$_,2;
|
igor@2
|
411 $dict{$a}=$b;
|
igor@2
|
412 }
|
igor@2
|
413 }
|
igor@2
|
414 if (open(F, $file)) {
|
igor@2
|
415 @lines=<F>;
|
igor@2
|
416 close(F);
|
igor@2
|
417
|
igor@2
|
418 if (open(F, ">$file")) {
|
igor@2
|
419 for (@lines) {
|
igor@2
|
420 m/\s+\S+\s+(\S+)/;
|
igor@2
|
421 $name=$1;
|
igor@3
|
422 if (not /^#/ and defined($dict{$name})) {
|
igor@2
|
423 chomp;
|
igor@2
|
424 $mark=$dict{$name};
|
igor@2
|
425 $space=" "x(30-length($_));
|
igor@2
|
426 print F "$_$space$mark\n";
|
igor@2
|
427 }
|
igor@2
|
428 else {
|
igor@2
|
429 print F "$_";
|
igor@2
|
430 }
|
igor@2
|
431 }
|
igor@2
|
432 close(F);
|
igor@2
|
433 }
|
igor@2
|
434 }
|
igor@2
|
435 PERL_SCRIPT
|
igor@2
|
436 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
|
igor@2
|
437 export NOTES_FILE
|
igor@2
|
438 perl $PERL_SCRIPT_TEMP_NAME "$1"
|
igor@2
|
439 rm $PERL_SCRIPT_TEMP_NAME
|
igor@2
|
440 }
|
igor@2
|
441
|
igor@2
|
442 remove_marks()
|
igor@2
|
443 {
|
igor@2
|
444 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
|
igor@2
|
445 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
|
igor@2
|
446 $file = $ARGV[0];
|
igor@2
|
447 our %dict;
|
igor@2
|
448 if (open(F, $file)) {
|
igor@2
|
449 @lines=<F>;
|
igor@2
|
450 close(F);
|
igor@2
|
451
|
igor@2
|
452 if (open(F, ">$file")) {
|
igor@2
|
453 for (@lines) {
|
igor@2
|
454 chomp;
|
igor@3
|
455 if (not /^#/ and m/(\s+)(\S+)(\s+)(\S+)(\s+)(.*)/) {
|
igor@2
|
456 my $name=$4;
|
igor@2
|
457 my $comment=$6;
|
igor@2
|
458 $dict{$name}=$comment;
|
igor@2
|
459 print F "$1$2$3$4\n";
|
igor@2
|
460 }
|
igor@2
|
461 else {
|
igor@2
|
462 print F "$_\n";
|
igor@2
|
463 }
|
igor@2
|
464 }
|
igor@2
|
465 }
|
igor@2
|
466 }
|
igor@21
|
467 if (($ENV{DONT_ADD_MARKS} ne "YES") and open(NOTES, $ENV{NOTES_FILE})) {
|
igor@2
|
468 @lines=<NOTES>;
|
igor@2
|
469 close(NOTES);
|
igor@2
|
470
|
igor@2
|
471 if (open(NOTES, ">".$ENV{NOTES_FILE})) {
|
igor@2
|
472 for (@lines) {
|
igor@2
|
473 chomp;
|
igor@2
|
474 s/^\s+//;
|
igor@2
|
475 my ($a,$b)=split /\s+/,$_,2;
|
igor@2
|
476 if (not defined($dict{$a}) || ($dict{$a} eq $b)) {
|
igor@2
|
477 print NOTES "$_\n";
|
igor@2
|
478 if (defined($dict{$a})) { unset($dict{$a}); }
|
igor@2
|
479 }
|
igor@2
|
480 }
|
igor@2
|
481 for (keys %dict) {
|
igor@2
|
482 $mark=$dict{$_};
|
igor@2
|
483 $space=" "x(30-length($_));
|
igor@2
|
484 print NOTES "$_$space$mark\n";
|
igor@2
|
485 }
|
igor@2
|
486 }
|
igor@2
|
487 }
|
igor@2
|
488 PERL_SCRIPT
|
igor@2
|
489 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
|
igor@2
|
490 export NOTES_FILE
|
igor@21
|
491 export DONT_ADD_MARKS
|
igor@2
|
492 perl $PERL_SCRIPT_TEMP_NAME "$1"
|
igor@2
|
493 rm $PERL_SCRIPT_TEMP_NAME
|
igor@2
|
494 }
|
igor@2
|
495
|
igor@25
|
496 part()
|
igor@25
|
497 {
|
igor@25
|
498 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-part-XXXXXXXX`
|
igor@25
|
499 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
|
igor@25
|
500 #!/usr/bin/perl
|
igor@25
|
501
|
igor@25
|
502 my @lines=<STDIN>;
|
igor@25
|
503 my $lines=$#lines;
|
igor@25
|
504 my $interval=$ARGV[0];
|
igor@25
|
505 if (not $interval) {
|
igor@25
|
506 print @lines;
|
igor@25
|
507 }
|
igor@25
|
508 else {
|
igor@25
|
509 my ($start,$stop,$total);
|
igor@25
|
510 if ($interval =~ m@(.*)/(.*)@) {
|
igor@25
|
511 $start = $1;
|
igor@25
|
512 $total = $2;
|
igor@25
|
513 }
|
igor@25
|
514 else {
|
igor@25
|
515 $start=$interval;
|
igor@25
|
516 $total=0;
|
igor@25
|
517 }
|
igor@25
|
518 if ($start =~ m@(.*)-(.*)@) {
|
igor@25
|
519 $start = $1;
|
igor@25
|
520 $stop = $2;
|
igor@25
|
521 }
|
igor@25
|
522 if ($start =~ m@(.*)\+(.*)@) {
|
igor@25
|
523 $start = $1;
|
igor@25
|
524 $stop = $start+$2;
|
igor@25
|
525 }
|
igor@25
|
526
|
igor@25
|
527 $start=int($lines/$total*$start);
|
igor@25
|
528 $stop=int($lines/$total*$stop);
|
igor@25
|
529
|
igor@25
|
530 for($i=$start;$i<$stop;$i++){
|
igor@25
|
531 print $lines[$i];
|
igor@25
|
532 }
|
igor@25
|
533 }
|
igor@25
|
534 PERL_SCRIPT
|
igor@25
|
535 perl $PERL_SCRIPT_TEMP_NAME "$1"
|
igor@25
|
536 rm $PERL_SCRIPT_TEMP_NAME
|
igor@25
|
537 }
|
igor@25
|
538
|
igor@27
|
539 if [ "$TAGS_LIST_ONLY" = "YES" ]
|
igor@27
|
540 then
|
igor@27
|
541 cd "${WORK_DIR}"
|
igor@27
|
542 echo ${LANGUAGE}_*.txt | tr ' ' '\n' | grep -v '*' | sed 's/[^_]*_//;s/.txt$//'
|
igor@27
|
543 exit 0
|
igor@27
|
544 fi
|
igor@27
|
545
|
igor@27
|
546 tag_file_name()
|
igor@27
|
547 {
|
igor@27
|
548 echo "${LANGUAGE}_${1}.txt"
|
igor@27
|
549 }
|
igor@27
|
550
|
igor@27
|
551 if [ "$REMOVE_TAG" = "YES" ]
|
igor@27
|
552 then
|
igor@27
|
553 cd "${WORK_DIR}"
|
igor@27
|
554 for i in $TAG_NAME
|
igor@27
|
555 do
|
igor@27
|
556 echo "$TAGNAME" | grep -q '[/*?]' && continue
|
igor@27
|
557 f="`tag_file_name $i`"
|
igor@27
|
558 if [ -e "$f" ]
|
igor@27
|
559 then
|
igor@27
|
560 rm -f "$f" && echo Tag "'$i'" removed
|
igor@27
|
561 else
|
igor@27
|
562 echo Unknown tag "'$i'"
|
igor@27
|
563 fi
|
igor@27
|
564 done
|
igor@27
|
565 exit 0
|
igor@27
|
566 fi
|
igor@27
|
567
|
igor@0
|
568 mkdir -p $WORK_DIR
|
igor@8
|
569 oldpwd="$PWD"
|
igor@0
|
570 cd $WORK_DIR
|
igor@27
|
571 if [ "$MERGE_TAGGED_WORDS" = "YES" ]
|
igor@27
|
572 then
|
igor@27
|
573 VOC_FILES=''
|
igor@27
|
574 for i in $MERGE_THIS_TAGS
|
igor@27
|
575 do
|
igor@27
|
576 f=`tag_file_name $i`
|
igor@27
|
577 [ -e "$f" ] && VOC_FILES="${VOC_FILES} $f"
|
igor@27
|
578 done
|
igor@27
|
579 if [ -z "$VOC_FILES" ]
|
igor@27
|
580 then
|
igor@27
|
581 echo Unknown tags: $MERGE_THIS_TAGS > /dev/stderr
|
igor@27
|
582 else
|
igor@27
|
583 cat $VOC_FILES
|
igor@27
|
584 fi
|
igor@27
|
585 elif [ "$MERGE_ALL_TAGGED" = "YES" ]
|
igor@27
|
586 then
|
igor@27
|
587 cat ${LANGUAGE}_*.txt
|
igor@27
|
588 elif echo "$1" | grep -q http:
|
igor@0
|
589 then
|
igor@11
|
590 text_from_url "$1"
|
igor@0
|
591 elif [ "$#" != 0 ]
|
igor@0
|
592 then
|
igor@13
|
593 if echo $1 | grep -q ^/
|
igor@13
|
594 then
|
igor@13
|
595 cat "$1"
|
igor@13
|
596 else
|
igor@13
|
597 cat "$oldpwd/$1"
|
igor@13
|
598 fi
|
igor@0
|
599 else
|
igor@11
|
600 cat
|
igor@11
|
601 fi \
|
igor@25
|
602 | part $PART_TO_PROCESS \
|
igor@11
|
603 | tee $ORIGINAL_TEXT \
|
igor@26
|
604 | two_and_three_words \
|
igor@11
|
605 | get_words ${TEMP1}-full \
|
igor@11
|
606 | group_words \
|
igor@11
|
607 | add_stat ${TEMP1}-full \
|
igor@11
|
608 | tee "$TEMP1" > "$TEMP2"
|
igor@0
|
609
|
igor@16
|
610 if [ "$STAT_ONLY" = "YES" ]
|
igor@0
|
611 then
|
igor@16
|
612 cat "$TEMP1"
|
igor@21
|
613 elif [ "$NON_INTERACTIVE_MODE" = "YES" ]
|
igor@21
|
614 then
|
igor@21
|
615 cat "$TEMP1"
|
igor@0
|
616 else
|
igor@27
|
617 if [ `wc -l "$TEMP2" | awk '{print $1}'` != 0 ]
|
igor@16
|
618 then
|
igor@27
|
619 [ "$DONT_ADD_MARKS" = "YES" ] || add_marks "$TEMP2"
|
igor@27
|
620 if [ "$editor" = vim ]
|
igor@27
|
621 then
|
igor@27
|
622 vim -c 'set keywordprg='"$LANGUAGE" -c 'set iskeyword=@,48-57,/,.,-,_,+,,,#,$,%,~,=,48-255' "$TEMP2" < /dev/tty > /dev/tty
|
igor@27
|
623 else
|
igor@27
|
624 $editor "$TEMP2"
|
igor@27
|
625 fi
|
igor@27
|
626 remove_marks "$TEMP2"
|
igor@27
|
627
|
igor@27
|
628 vocabulary="$VOCABULARY"
|
igor@27
|
629 [ -n "$TAG_NAME" ] && vocabulary="`tag_file_name $TAG_NAME`"
|
igor@27
|
630 diff "$TEMP1" "$TEMP2" | awk '{print $3}' | sort -u >> "$vocabulary"
|
igor@16
|
631 fi
|
igor@0
|
632 fi
|
igor@2
|
633
|
igor@5
|
634 rm -f "$TEMP1" "$TEMP2" "${TEMP1}-full" "$ORIGINAL_TEXT"
|