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@36
|
14 -c show compressed wordlist: one word per group
|
igor@50
|
15 -f file show only words that are related to the words from the file
|
igor@20
|
16 -k put higher words that are similar to the known words (only for English)
|
igor@16
|
17 -l lang override language settings
|
igor@21
|
18 -n non-interactive mode (don't run vi)
|
igor@33
|
19 -N turn off known words filtering
|
igor@27
|
20 -a don't add marks (and don't save marks added by user)
|
igor@25
|
21 -p pages work with specified pages only (pages = start-stop/total )
|
igor@20
|
22 -s show the text statistics (percentage of known words and so on) and exit
|
igor@33
|
23 -S show your vocabulary statistics (number of words and word groups)
|
igor@27
|
24 -t tag tag known words with tag
|
igor@27
|
25 -T show list of active tags
|
igor@27
|
26 -m tag merge the words tagged with "tag" into the main vocabulary
|
igor@27
|
27 -M merge the words tagged with any tag into the main vocabulary
|
igor@27
|
28 -r tag remove subvocabulary for the "tag"
|
igor@26
|
29 -2 -3 find 2 and 3 words' sequences
|
igor@16
|
30
|
igor@20
|
31 The language of the text can be specified also
|
igor@20
|
32 by name of the program new-words (correspondent link must be created before).
|
igor@20
|
33 For example, these calls are equivalent:
|
igor@0
|
34
|
igor@20
|
35 de-words URL
|
igor@0
|
36 new-words -l de URL
|
igor@0
|
37
|
igor@20
|
38 HELP
|
igor@20
|
39 }
|
igor@0
|
40
|
igor@20
|
41 if [ "$1" = "-h" ]
|
igor@20
|
42 then
|
igor@20
|
43 show_usage
|
igor@20
|
44 exit 0
|
igor@20
|
45 fi
|
igor@0
|
46
|
igor@0
|
47 WORK_DIR=~/.new-words/
|
igor@28
|
48 TEMP1=`mktemp /tmp/new-words-temp1.XXXXXXXXXX`
|
igor@28
|
49 TEMP2=`mktemp /tmp/new-words-temp2.XXXXXXXXXX`
|
igor@28
|
50 export ORIGINAL_TEXT=`mktemp /tmp/new-words-orig.XXXXXXXXXX`
|
igor@0
|
51 editor=${EDITOR:-vim}
|
igor@0
|
52
|
igor@24
|
53 # language detection
|
igor@21
|
54
|
igor@2
|
55 LANGUAGE=en
|
igor@2
|
56 my_name="`echo $0 | sed s@.*/@@ | sed s/-.*// `"
|
igor@22
|
57 for arg
|
igor@22
|
58 do
|
igor@22
|
59 if echo "$arg" | grep -q http://...wikipedia.org/wiki/
|
igor@22
|
60 then
|
igor@22
|
61 LANGUAGE="`echo $arg | sed s@http://@@ | sed s@.wikipedia.*@@`"
|
igor@22
|
62 fi
|
igor@22
|
63 done
|
igor@2
|
64 [ "${my_name}" = "new" ] || LANGUAGE="$my_name"
|
igor@24
|
65
|
igor@24
|
66 #----------------------------------------------------
|
igor@24
|
67 # command line options processing
|
igor@24
|
68
|
igor@24
|
69 STAT_ONLY=NO
|
igor@24
|
70 NEED_TO_USE_VOCABULARY_WHEN_SORT=NO
|
igor@24
|
71 DONT_ADD_MARKS=NO
|
igor@24
|
72 NON_INTERACTIVE_MODE=NO
|
igor@25
|
73 PART_TO_PROCESS=''
|
igor@26
|
74 GROUP_WORDS_BY_THREE=NO
|
igor@26
|
75 GROUP_WORDS_BY_TWO=NO
|
igor@27
|
76 TAG_NAME=''
|
igor@27
|
77 MERGE_THIS_TAGS=''
|
igor@27
|
78 TAGS_LIST_ONLY=NO
|
igor@27
|
79 MERGE_TAGGED_WORDS=NO
|
igor@27
|
80 MERGE_ALL_TAGGED=NO
|
igor@27
|
81 DONT_ADD_MARKLINES=NO
|
igor@33
|
82 FILTER_WORDS=YES
|
igor@33
|
83 SHOW_VOC_STAT=NO
|
igor@36
|
84 COMPRESSED_WORDLIST=NO
|
igor@50
|
85 ALLOWED_WORDS_FILENAME=''
|
igor@36
|
86 while getopts cl:sSkanNp:t:Tm:Mr:23 opt
|
igor@24
|
87 do
|
igor@24
|
88 case "$opt" in
|
igor@36
|
89 c) COMPRESSED_WORDLIST=YES;;
|
igor@24
|
90 s) STAT_ONLY=YES;;
|
igor@33
|
91 S) SHOW_VOC_STAT=YES;;
|
igor@24
|
92 k) NEED_TO_USE_VOCABULARY_WHEN_SORT=YES;;
|
igor@24
|
93 l) LANGUAGE="$OPTARG";;
|
igor@27
|
94 a) DONT_ADD_MARKS=YES;;
|
igor@24
|
95 n) NON_INTERACTIVE_MODE=YES;;
|
igor@33
|
96 N) FILTER_WORDS=NO;;
|
igor@25
|
97 p) PART_TO_PROCESS="$OPTARG";;
|
igor@27
|
98 t) TAG_NAME="$OPTARG";;
|
igor@27
|
99 T) TAGS_LIST_ONLY="YES";;
|
igor@27
|
100 m) DONT_ADD_MARKLINES="YES"; MERGE_TAGGED_WORDS="YES"; MERGE_THIS_TAGS="$TAG_NAME $OPTARG";;
|
igor@27
|
101 M) DONT_ADD_MARKLINES="YES"; MERGE_ALL_TAGGED="YES";;
|
igor@27
|
102 r) REMOVE_TAG="YES"; TAG_NAME="$TAG_NAME $OPTARG";;
|
igor@26
|
103 2) GROUP_WORDS_BY_TWO=YES;;
|
igor@26
|
104 3) GROUP_WORDS_BY_THREE=YES;;
|
igor@24
|
105 \?) # unknown flag
|
igor@24
|
106 show_usage
|
igor@24
|
107 exit 1;;
|
igor@24
|
108 esac
|
igor@24
|
109 done
|
igor@24
|
110 shift `expr $OPTIND - 1`
|
igor@24
|
111
|
igor@0
|
112 if [ "$1" = "-l" ]
|
igor@0
|
113 then
|
igor@0
|
114 LANGUAGE="$2"
|
igor@0
|
115 shift 2
|
igor@0
|
116 fi
|
igor@24
|
117
|
igor@2
|
118 VOCABULARY=${LANGUAGE}.txt
|
igor@2
|
119 NOTES_FILE=notes-${LANGUAGE}.txt
|
igor@0
|
120
|
igor@33
|
121 if [ "${SHOW_VOC_STAT}" = "YES" ]
|
igor@33
|
122 then
|
igor@33
|
123 $0 -l "${LANGUAGE}" -N -n ${WORK_DIR}/${VOCABULARY} | head -1 | awk '{print $5}' | tr -d "<>"
|
igor@33
|
124 exit 0
|
igor@33
|
125 fi
|
igor@33
|
126
|
igor@21
|
127 #----------------------------------------------------
|
igor@21
|
128
|
igor@0
|
129 get_words()
|
igor@0
|
130 {
|
igor@33
|
131 export FILTER_WORDS
|
igor@1
|
132 tr ' ' '\n' | sed 's/--/ /g' \
|
igor@3
|
133 | sed "s/'/__APOSTROPHE__/g" \
|
igor@31
|
134 | perl -MEncode -Mutf8 -n -e '$_ = decode( "utf8", $_);y/*\r,.:#@()+=—<>$;"?!|·[]^%&/ /; binmode STDOUT, ":utf8"; print if /^[[:alpha:] '"'"'_-]*$/'\
|
igor@26
|
135 | sed "s/__APOSTROPHE__/'/g" \
|
igor@3
|
136 | tr ' ' '\n' \
|
igor@3
|
137 | tee "$1" \
|
igor@3
|
138 | grep_v_english_perl \
|
igor@0
|
139 | sort | uniq -c | awk '{if ($2!="") print;}' | sort -rn
|
igor@0
|
140 }
|
igor@0
|
141
|
igor@3
|
142 add_stat()
|
igor@3
|
143 {
|
igor@27
|
144 if [ "$DONT_ADD_MARKLINES" = "YES" ]
|
igor@27
|
145 then
|
igor@27
|
146 cat
|
igor@27
|
147 return
|
igor@27
|
148 fi
|
igor@3
|
149 before="$1"
|
igor@3
|
150 after=${before}2
|
igor@3
|
151 cat > "$after"
|
igor@3
|
152 total="`wc -w $1 | awk '{print $1}'`"
|
igor@3
|
153 total_unknown="`cat $after|awk '{s=s+$1}END{print s}'`"
|
igor@3
|
154 total_known="`echo $total-$total_unknown|bc`"
|
igor@3
|
155 percentage="`echo '100*('$total-$total_unknown')'/$total | bc -l | sed 's/\\.\(.\).*/.\1/'`"
|
igor@18
|
156 #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
|
157 sentences="`cat $ORIGINAL_TEXT | perl -e 'local $/; $_=<>; s/[^.]//msg; print length($_);'`"
|
igor@18
|
158
|
igor@18
|
159
|
igor@16
|
160 if [ "$STAT_ONLY" = "YES" ]
|
igor@16
|
161 then
|
igor@18
|
162 echo "LANG KNOWN% UNKNOWN% KNOWN TOTAL WPS UWPS*10"
|
igor@18
|
163 echo "$LANGUAGE $percentage `echo \(100-$percentage\) | bc -l` $total_known $total `echo $total/$sentences|bc` `echo 10*$total_unknown/$sentences|bc` "
|
igor@17
|
164 rm $after
|
igor@16
|
165 return 0
|
igor@16
|
166 else
|
igor@34
|
167 groups="`echo $(grep '# groups' $after | awk '{print $3}')`"
|
igor@34
|
168 words="`echo $(grep -v '^#' $after | wc -l)`"
|
igor@33
|
169 echo "# $LANGUAGE, $percentage, <$total_known/$total>, <$groups/$words>"
|
igor@16
|
170 fi
|
igor@16
|
171
|
igor@14
|
172 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
|
igor@14
|
173 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
|
igor@14
|
174 my $total=shift(@ARGV);
|
igor@14
|
175 my $total_known=shift(@ARGV);
|
igor@14
|
176 my $s=0;
|
igor@16
|
177 my $mark_line=int($total_known*100/$total/5)*5;
|
igor@19
|
178 if ($mark_line>=90) {
|
igor@19
|
179 $mark_line=int($total_known*100/$total)+1;
|
igor@19
|
180 } else { $mark_line +=5; };
|
igor@14
|
181 while(<>)
|
igor@14
|
182 {
|
igor@33
|
183 next if /^#\s*groups\s*/;
|
igor@14
|
184 print;
|
igor@14
|
185 /^\s*([0-9]*)\s*/;
|
igor@14
|
186 $s+=$1;
|
igor@19
|
187 if (($total_known+$s)*100/$total>=$mark_line) {
|
igor@14
|
188 print "# $mark_line\n";
|
igor@14
|
189 if ($mark_line>=90) { $mark_line+=1; } else { $mark_line +=5; };
|
igor@14
|
190 }
|
igor@14
|
191 }
|
igor@14
|
192 PERL_SCRIPT
|
igor@14
|
193 perl $PERL_SCRIPT_TEMP_NAME "$total" "$total_known" "$after"
|
igor@14
|
194 rm $PERL_SCRIPT_TEMP_NAME
|
igor@3
|
195 rm $after
|
igor@3
|
196 }
|
igor@3
|
197
|
igor@26
|
198 two_and_three_words()
|
igor@26
|
199 {
|
igor@26
|
200 if [ $GROUP_WORDS_BY_THREE = NO -a $GROUP_WORDS_BY_TWO = NO ]
|
igor@26
|
201 then
|
igor@26
|
202 cat
|
igor@26
|
203 else
|
igor@26
|
204 cat
|
igor@26
|
205
|
igor@26
|
206 export GROUP_WORDS_BY_THREE
|
igor@26
|
207 export GROUP_WORDS_BY_TWO
|
igor@26
|
208 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-two-and-three-XXXXXXXX`
|
igor@26
|
209 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
|
igor@26
|
210 #!/usr/bin/perl
|
igor@26
|
211 local $/;
|
igor@26
|
212 $words=<>;
|
igor@26
|
213 $words=~ s@[!?;,:#1-9".]@ @g;
|
igor@26
|
214 $words =~ s@\s+@ @g;
|
igor@26
|
215 @words = split /\s+/, $words;
|
igor@26
|
216 for ($i=0; $i<$#words-3;$i++) {
|
igor@26
|
217 my ($a,$b,$c)= ($words[$i],$words[$i+1],$words[$i+2]);
|
igor@26
|
218 if ($ENV{GROUP_WORDS_BY_THREE} eq "YES" and ($a && $b && $c)) {
|
igor@26
|
219 print "${a}_${b}_${c}\n";
|
igor@26
|
220 };
|
igor@26
|
221 if ($ENV{GROUP_WORDS_BY_TWO} eq "YES" and ($a && $b)) {
|
igor@26
|
222 print "${a}_${b}\n";
|
igor@26
|
223 };
|
igor@26
|
224 }
|
igor@26
|
225 PERL_SCRIPT
|
igor@26
|
226 perl $PERL_SCRIPT_TEMP_NAME "$ORIGINAL_TEXT"
|
igor@26
|
227 rm $PERL_SCRIPT_TEMP_NAME
|
igor@26
|
228 fi
|
igor@26
|
229 }
|
igor@26
|
230
|
igor@0
|
231 grep_v_english()
|
igor@0
|
232 {
|
igor@0
|
233 [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
|
igor@0
|
234 eval $(cat $VOCABULARY | tr -d "'" | xargs -n10 echo | tr ' ' '|' | sed 's/^/egrep -xv "RRRRRRR|/' | sed 's/$/"/' | tr '\n' '|')cat
|
igor@0
|
235 }
|
igor@0
|
236
|
igor@0
|
237 grep_v_english_perl()
|
igor@0
|
238 {
|
igor@0
|
239 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
|
igor@0
|
240 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
|
igor@33
|
241 if ($ENV{FILTER_WORDS} eq "NO") {
|
igor@33
|
242 while(<>) { print; }
|
igor@33
|
243 exit(0);
|
igor@33
|
244 }
|
igor@27
|
245 $voc_files=$ENV{VOC_FILES};
|
igor@27
|
246 $voc_files=~s@^ @@;
|
igor@27
|
247 for $voc_file (split /\s+/,$voc_files) {
|
igor@27
|
248 if (open(VOC, $voc_file)) {
|
igor@27
|
249 while (<VOC>){
|
igor@27
|
250 chomp;
|
igor@27
|
251 #s/'//g;
|
igor@27
|
252 $voc{$_}="1";
|
igor@27
|
253 }
|
igor@27
|
254 }
|
igor@0
|
255 }
|
igor@0
|
256 while(<>) {
|
igor@0
|
257 chomp;
|
igor@0
|
258 if (not defined($voc{$_})) { print "$_\n"; }
|
igor@0
|
259 }
|
igor@0
|
260 PERL_SCRIPT
|
igor@0
|
261 [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
|
igor@27
|
262 export VOCABULARY VOC_FILES
|
igor@27
|
263 VOC_FILES=$VOCABULARY
|
igor@27
|
264 for i in $TAG_NAME
|
igor@27
|
265 do
|
igor@27
|
266 VOC_FILES="${VOC_FILES} `tag_file_name $i`"
|
igor@27
|
267 done
|
igor@0
|
268 perl $PERL_SCRIPT_TEMP_NAME
|
igor@0
|
269 rm $PERL_SCRIPT_TEMP_NAME
|
igor@0
|
270 }
|
igor@0
|
271
|
igor@11
|
272 group_words()
|
igor@11
|
273 {
|
igor@22
|
274 #if [ "$LANGUAGE" != "en" ]
|
igor@22
|
275 #then
|
igor@22
|
276 # cat
|
igor@22
|
277 # return
|
igor@22
|
278 #fi
|
igor@11
|
279 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-group-words-XXXXXXXX`
|
igor@11
|
280 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
|
igor@11
|
281 #!/usr/bin/perl
|
igor@11
|
282
|
igor@29
|
283 use Encode;
|
igor@29
|
284 use utf8;
|
igor@32
|
285 use Lingua::Stem::Snowball qw(stem);
|
igor@29
|
286
|
igor@12
|
287 eval {
|
igor@12
|
288 # http://stackoverflow.com/questions/251694/how-can-i-check-if-i-have-a-perl-module-before-using-it
|
igor@12
|
289 require String::Similarity;
|
igor@12
|
290 String::Similarity->import();
|
igor@12
|
291 };
|
igor@12
|
292 unless($@)
|
igor@12
|
293 {
|
igor@12
|
294 our $HAVE_String_Similarity=1;
|
igor@12
|
295 }
|
igor@12
|
296
|
igor@32
|
297
|
igor@32
|
298 sub load_notes_dict()
|
igor@32
|
299 {
|
igor@32
|
300 my %dict;
|
igor@32
|
301 if (open(NOTES, $ENV{NOTES_FILE})) {
|
igor@32
|
302 while(<NOTES>) {
|
igor@32
|
303 $_ = decode( "utf8", $_);
|
igor@32
|
304 chomp;
|
igor@32
|
305 s/^\s+//;
|
igor@32
|
306 my ($a,$b)=split /\s+/,$_,2;
|
igor@32
|
307 $dict{$a}=$b;
|
igor@32
|
308 }
|
igor@32
|
309 }
|
igor@32
|
310 return %dict;
|
igor@32
|
311 }
|
igor@32
|
312
|
igor@12
|
313 sub similar($$){
|
igor@12
|
314 my $a=shift;
|
igor@12
|
315 my $b=shift;
|
igor@12
|
316 if ($HAVE_String_Similarity) {
|
igor@12
|
317 return $Similarity{"$a $b"};
|
igor@12
|
318 }
|
igor@12
|
319 else {
|
igor@12
|
320 return 0;
|
igor@12
|
321 }
|
igor@12
|
322 }
|
igor@12
|
323
|
igor@32
|
324
|
igor@32
|
325 sub normalize_without_linked($)
|
igor@11
|
326 {
|
igor@22
|
327 if ( $ENV{LANGUAGE} eq "en" ) { return normalize_english(shift); }
|
igor@22
|
328 elsif ( $ENV{LANGUAGE} eq "de" ) { return normalize_german(shift); }
|
igor@29
|
329 elsif ( $ENV{LANGUAGE} eq "uk" ) { return normalize_ukrainian(shift); }
|
igor@31
|
330 elsif ( $ENV{LANGUAGE} eq "io" ) { return normalize_esperanto(shift); }
|
igor@22
|
331 else { return shift ; }
|
igor@22
|
332 }
|
igor@22
|
333
|
igor@32
|
334 sub normalize_with_linked($)
|
igor@32
|
335 {
|
igor@32
|
336 my $word = normalize_without_linked(shift);
|
igor@32
|
337 #return $word;
|
igor@32
|
338 if ($linked_words{$word}) {
|
igor@32
|
339 return $linked_words{$word};
|
igor@32
|
340 }
|
igor@32
|
341 else {
|
igor@32
|
342 return $word;
|
|