rev |
line source |
igor@38
|
1 #!/bin/bash
|
igor@38
|
2
|
igor@38
|
3 show_usage()
|
igor@38
|
4 {
|
igor@38
|
5 cat <<HELP > /dev/stderr
|
igor@38
|
6
|
igor@38
|
7 USAGE:
|
igor@38
|
8
|
igor@38
|
9 new-words [ -l lang ] [ -s ] [ ARG ]
|
igor@38
|
10
|
igor@38
|
11 SWITCHES:
|
igor@38
|
12
|
igor@38
|
13 -h print this screen
|
igor@38
|
14 -c show compressed wordlist: one word per group
|
igor@45
|
15 -G turn off word grouping
|
igor@38
|
16 -k put higher words that are similar to the known words (only for English)
|
igor@38
|
17 -l lang override language settings
|
igor@38
|
18 -n non-interactive mode (don't run vi)
|
igor@38
|
19 -N turn off known words filtering
|
igor@38
|
20 -a don't add marks (and don't save marks added by user)
|
igor@38
|
21 -p pages work with specified pages only (pages = start-stop/total )
|
igor@38
|
22 -s show the text statistics (percentage of known words and so on) and exit
|
igor@38
|
23 -S show your vocabulary statistics (number of words and word groups)
|
igor@38
|
24 -t tag tag known words with tag
|
igor@38
|
25 -T show list of active tags
|
igor@38
|
26 -m tag merge the words tagged with "tag" into the main vocabulary
|
igor@38
|
27 -M merge the words tagged with any tag into the main vocabulary
|
igor@48
|
28 -d tag delete subvocabulary for the "tag"
|
igor@48
|
29 -r RANGE show only first RANGE words
|
igor@48
|
30 -R RANGE show only words lower than RANGE percent
|
igor@38
|
31 -2 -3 find 2 and 3 words' sequences
|
igor@38
|
32
|
igor@38
|
33 The language of the text can be specified also
|
igor@38
|
34 by name of the program new-words (correspondent link must be created before).
|
igor@38
|
35 For example, these calls are equivalent:
|
igor@38
|
36
|
igor@38
|
37 de-words URL
|
igor@38
|
38 new-words -l de URL
|
igor@38
|
39
|
igor@38
|
40 HELP
|
igor@38
|
41 }
|
igor@38
|
42
|
igor@38
|
43 if [ "$1" = "-h" ]
|
igor@38
|
44 then
|
igor@38
|
45 show_usage
|
igor@38
|
46 exit 0
|
igor@38
|
47 fi
|
igor@38
|
48
|
igor@38
|
49 NEW_WORDS_PY=/home/igor/hg/new-words/new-words.py
|
igor@38
|
50 WORK_DIR=~/.new-words/
|
igor@38
|
51 TEMP1=`mktemp /tmp/new-words-temp1.XXXXXXXXXX`
|
igor@38
|
52 TEMP2=`mktemp /tmp/new-words-temp2.XXXXXXXXXX`
|
igor@38
|
53 export ORIGINAL_TEXT=`mktemp /tmp/new-words-orig.XXXXXXXXXX`
|
igor@38
|
54 editor=${EDITOR:-vim}
|
igor@38
|
55
|
igor@38
|
56 # language detection
|
igor@38
|
57
|
igor@38
|
58 LANGUAGE=en
|
igor@38
|
59 my_name="`echo $0 | sed s@.*/@@ | sed s/-.*// `"
|
igor@38
|
60 for arg
|
igor@38
|
61 do
|
igor@38
|
62 if echo "$arg" | grep -q http://...wikipedia.org/wiki/
|
igor@38
|
63 then
|
igor@38
|
64 LANGUAGE="`echo $arg | sed s@http://@@ | sed s@.wikipedia.*@@`"
|
igor@38
|
65 fi
|
igor@38
|
66 done
|
igor@38
|
67 [ "${my_name}" = "new" ] || LANGUAGE="$my_name"
|
igor@38
|
68
|
igor@38
|
69 #----------------------------------------------------
|
igor@38
|
70 # command line options processing
|
igor@38
|
71
|
igor@38
|
72 STAT_ONLY=NO
|
igor@38
|
73 NEED_TO_USE_VOCABULARY_WHEN_SORT=NO
|
igor@38
|
74 DONT_ADD_MARKS=NO
|
igor@38
|
75 NON_INTERACTIVE_MODE=NO
|
igor@38
|
76 PART_TO_PROCESS=''
|
igor@38
|
77 GROUP_WORDS_BY_THREE=NO
|
igor@38
|
78 GROUP_WORDS_BY_TWO=NO
|
igor@38
|
79 TAG_NAME=''
|
igor@38
|
80 MERGE_THIS_TAGS=''
|
igor@38
|
81 TAGS_LIST_ONLY=NO
|
igor@38
|
82 MERGE_TAGGED_WORDS=NO
|
igor@38
|
83 MERGE_ALL_TAGGED=NO
|
igor@38
|
84 DONT_ADD_MARKLINES=NO
|
igor@38
|
85 FILTER_WORDS=YES
|
igor@38
|
86 SHOW_VOC_STAT=NO
|
igor@38
|
87 COMPRESSED_WORDLIST=NO
|
igor@45
|
88 WORDS_GROUPING=YES
|
igor@48
|
89 while getopts Gcl:sSkanNp:t:Tm:Md:r:R:23 opt
|
igor@38
|
90 do
|
igor@38
|
91 case "$opt" in
|
igor@38
|
92 c) COMPRESSED_WORDLIST=YES;;
|
igor@45
|
93 G) WORDS_GROUPING=NO;;
|
igor@38
|
94 s) STAT_ONLY=YES;;
|
igor@38
|
95 S) SHOW_VOC_STAT=YES;;
|
igor@38
|
96 k) NEED_TO_USE_VOCABULARY_WHEN_SORT=YES;;
|
igor@38
|
97 l) LANGUAGE="$OPTARG";;
|
igor@38
|
98 a) DONT_ADD_MARKS=YES;;
|
igor@38
|
99 n) NON_INTERACTIVE_MODE=YES;;
|
igor@38
|
100 N) FILTER_WORDS=NO;;
|
igor@38
|
101 p) PART_TO_PROCESS="$OPTARG";;
|
igor@38
|
102 t) TAG_NAME="$OPTARG";;
|
igor@38
|
103 T) TAGS_LIST_ONLY="YES";;
|
igor@38
|
104 m) DONT_ADD_MARKLINES="YES"; MERGE_TAGGED_WORDS="YES"; MERGE_THIS_TAGS="$TAG_NAME $OPTARG";;
|
igor@38
|
105 M) DONT_ADD_MARKLINES="YES"; MERGE_ALL_TAGGED="YES";;
|
igor@48
|
106 d) REMOVE_TAG="YES"; TAG_NAME="$TAG_NAME $OPTARG";;
|
igor@48
|
107 r) SHOW_RANGE="$OPTARG";;
|
igor@48
|
108 R) SHOW_RANGE_PERCENTAGE="$OPTARG";;
|
igor@38
|
109 2) GROUP_WORDS_BY_TWO=YES;;
|
igor@38
|
110 3) GROUP_WORDS_BY_THREE=YES;;
|
igor@38
|
111 \?) # unknown flag
|
igor@38
|
112 show_usage
|
igor@38
|
113 exit 1;;
|
igor@38
|
114 esac
|
igor@38
|
115 done
|
igor@38
|
116 shift `expr $OPTIND - 1`
|
igor@38
|
117
|
igor@38
|
118 if [ "$1" = "-l" ]
|
igor@38
|
119 then
|
igor@38
|
120 LANGUAGE="$2"
|
igor@38
|
121 shift 2
|
igor@38
|
122 fi
|
igor@38
|
123
|
igor@38
|
124 VOCABULARY=${LANGUAGE}.txt
|
igor@38
|
125 NOTES_FILE=notes-${LANGUAGE}.txt
|
igor@38
|
126
|
igor@38
|
127 if [ "${SHOW_VOC_STAT}" = "YES" ]
|
igor@38
|
128 then
|
igor@38
|
129 $0 -l "${LANGUAGE}" -N -n ${WORK_DIR}/${VOCABULARY} | head -1 | awk '{print $5}' | tr -d "<>"
|
igor@38
|
130 exit 0
|
igor@38
|
131 fi
|
igor@38
|
132
|
igor@38
|
133 text_from_url()
|
igor@38
|
134 {
|
igor@38
|
135 lynx -dump "$1" | perl -p -e 's@http://[a-zA-Z&_.:/0-9%?=,#+()\[\]~-]*@@'
|
igor@38
|
136 }
|
igor@38
|
137
|
igor@38
|
138 add_marks()
|
igor@38
|
139 {
|
igor@40
|
140 $NEW_WORDS_PY -l "$LANGUAGE" -f add_notes "$1"
|
igor@39
|
141 }
|
igor@40
|
142 remove_marks()
|
igor@39
|
143 {
|
igor@40
|
144 $NEW_WORDS_PY -l "$LANGUAGE" -f remove_notes "$1"
|
igor@40
|
145 }
|
igor@40
|
146 get_words_group_words_add_stat()
|
igor@38
|
147 {
|
igor@48
|
148 SHOW_RANGE="$SHOW_RANGE" \
|
igor@48
|
149 SHOW_RANGE_PERCENTAGE="$SHOW_RANGE_PERCENTAGE" \
|
igor@47
|
150 COMPRESSED_WORDLIST="$COMPRESSED_WORDLIST" \
|
igor@44
|
151 GROUP_WORDS_BY_TWO="$GROUP_WORDS_BY_TWO" \
|
igor@44
|
152 GROUP_WORDS_BY_THREE="$GROUP_WORDS_BY_THREE" \
|
igor@47
|
153 STAT_ONLY="$STAT_ONLY" \
|
igor@45
|
154 WORDS_GROUPING="$WORDS_GROUPING" \
|
igor@45
|
155 FILTER_WORDS="$FILTER_WORDS" \
|
igor@40
|
156 $NEW_WORDS_PY -l "$LANGUAGE" -f get_words_group_words_add_stat "$1"
|
igor@38
|
157 }
|
igor@38
|
158
|
igor@38
|
159 part()
|
igor@38
|
160 {
|
igor@38
|
161 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-part-XXXXXXXX`
|
igor@38
|
162 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
|
igor@38
|
163 #!/usr/bin/perl
|
igor@38
|
164
|
igor@38
|
165 my @lines=<STDIN>;
|
igor@38
|
166 my $lines=$#lines;
|
igor@38
|
167 my $interval=$ARGV[0];
|
igor@38
|
168 if (not $interval) {
|
igor@38
|
169 print @lines;
|
igor@38
|
170 }
|
igor@38
|
171 else {
|
igor@38
|
172 my ($start,$stop,$total);
|
igor@38
|
173 if ($interval =~ m@(.*)/(.*)@) {
|
igor@38
|
174 $start = $1;
|
igor@38
|
175 $total = $2;
|
igor@38
|
176 }
|
igor@38
|
177 else {
|
igor@38
|
178 $start=$interval;
|
igor@38
|
179 $total=0;
|
igor@38
|
180 }
|
igor@38
|
181 if ($start =~ m@(.*)-(.*)@) {
|
igor@38
|
182 $start = $1;
|
igor@38
|
183 $stop = $2;
|
igor@38
|
184 }
|
igor@38
|
185 if ($start =~ m@(.*)\+(.*)@) {
|
igor@38
|
186 $start = $1;
|
igor@38
|
187 $stop = $start+$2;
|
igor@38
|
188 }
|
igor@38
|
189
|
igor@38
|
190 $start=int($lines/$total*$start);
|
igor@38
|
191 $stop=int($lines/$total*$stop);
|
igor@38
|
192
|
igor@38
|
193 for($i=$start;$i<$stop;$i++){
|
igor@38
|
194 print $lines[$i];
|
igor@38
|
195 }
|
igor@38
|
196 }
|
igor@38
|
197 PERL_SCRIPT
|
igor@38
|
198 perl $PERL_SCRIPT_TEMP_NAME "$1"
|
igor@38
|
199 rm $PERL_SCRIPT_TEMP_NAME
|
igor@38
|
200 }
|
igor@38
|
201
|
igor@38
|
202 if [ "$TAGS_LIST_ONLY" = "YES" ]
|
igor@38
|
203 then
|
igor@38
|
204 cd "${WORK_DIR}"
|
igor@38
|
205 echo ${LANGUAGE}_*.txt | tr ' ' '\n' | grep -v '*' | sed 's/[^_]*_//;s/.txt$//'
|
igor@38
|
206 exit 0
|
igor@38
|
207 fi
|
igor@38
|
208
|
igor@38
|
209 tag_file_name()
|
igor@38
|
210 {
|
igor@38
|
211 echo "${LANGUAGE}_${1}.txt"
|
igor@38
|
212 }
|
igor@38
|
213
|
igor@38
|
214 if [ "$REMOVE_TAG" = "YES" ]
|
igor@38
|
215 then
|
igor@38
|
216 cd "${WORK_DIR}"
|
igor@38
|
217 for i in $TAG_NAME
|
igor@38
|
218 do
|
igor@38
|
219 echo "$TAGNAME" | grep -q '[/*?]' && continue
|
igor@38
|
220 f="`tag_file_name $i`"
|
igor@38
|
221 if [ -e "$f" ]
|
igor@38
|
222 then
|
igor@38
|
223 rm -f "$f" && echo Tag "'$i'" removed
|
igor@38
|
224 else
|
igor@38
|
225 echo Unknown tag "'$i'"
|
igor@38
|
226 fi
|
igor@38
|
227 done
|
igor@38
|
228 exit 0
|
igor@38
|
229 fi
|
igor@38
|
230
|
igor@38
|
231 mkdir -p $WORK_DIR
|
igor@38
|
232 oldpwd="$PWD"
|
igor@38
|
233 cd $WORK_DIR
|
igor@38
|
234 if [ "$MERGE_TAGGED_WORDS" = "YES" ]
|
igor@38
|
235 then
|
igor@38
|
236 VOC_FILES=''
|
igor@38
|
237 for i in $MERGE_THIS_TAGS
|
igor@38
|
238 do
|
igor@38
|
239 f=`tag_file_name $i`
|
igor@38
|
240 [ -e "$f" ] && VOC_FILES="${VOC_FILES} $f"
|
igor@38
|
241 done
|
igor@38
|
242 if [ -z "$VOC_FILES" ]
|
igor@38
|
243 then
|
igor@38
|
244 echo Unknown tags: $MERGE_THIS_TAGS > /dev/stderr
|
igor@38
|
245 else
|
igor@38
|
246 cat $VOC_FILES
|
igor@38
|
247 fi
|
igor@38
|
248 elif [ "$MERGE_ALL_TAGGED" = "YES" ]
|
igor@38
|
249 then
|
igor@38
|
250 cat ${LANGUAGE}_*.txt
|
igor@38
|
251 elif echo "$1" | grep -q http:
|
igor@38
|
252 then
|
igor@38
|
253 text_from_url "$1"
|
igor@38
|
254 elif [ "$#" != 0 ]
|
igor@38
|
255 then
|
igor@38
|
256 if echo $1 | grep -q ^/
|
igor@38
|
257 then
|
igor@38
|
258 cat "$1"
|
igor@38
|
259 else
|
igor@38
|
260 cat "$oldpwd/$1"
|
igor@38
|
261 fi
|
igor@38
|
262 else
|
igor@38
|
263 cat
|
igor@38
|
264 fi \
|
igor@38
|
265 | part $PART_TO_PROCESS \
|
igor@38
|
266 | tee $ORIGINAL_TEXT \
|
igor@44
|
267 | \
|
igor@44
|
268 get_words_group_words_add_stat \
|
igor@38
|
269 | tee "$TEMP1" > "$TEMP2"
|
igor@38
|
270
|
igor@38
|
271 if [ "$STAT_ONLY" = "YES" ]
|
igor@38
|
272 then
|
igor@38
|
273 cat "$TEMP1"
|
igor@38
|
274 elif [ "$NON_INTERACTIVE_MODE" = "YES" ]
|
igor@38
|
275 then
|
igor@38
|
276 cat "$TEMP1"
|
igor@38
|
277 else
|
igor@38
|
278 if [ `wc -l "$TEMP2" | awk '{print $1}'` != 0 ]
|
igor@38
|
279 then
|
igor@38
|
280 [ "$DONT_ADD_MARKS" = "YES" ] || add_marks "$TEMP2"
|
igor@38
|
281 if [ "$editor" = vim ]
|
igor@38
|
282 then
|
igor@38
|
283 vim -c 'set keywordprg='"$LANGUAGE" -c 'set iskeyword=@,48-57,/,.,-,_,+,,,#,$,%,~,=,48-255' "$TEMP2" < /dev/tty > /dev/tty
|
igor@38
|
284 else
|
igor@38
|
285 $editor "$TEMP2"
|
igor@38
|
286 fi
|
igor@38
|
287 remove_marks "$TEMP2"
|
igor@38
|
288
|
igor@38
|
289 vocabulary="$VOCABULARY"
|
igor@38
|
290 [ -n "$TAG_NAME" ] && vocabulary="`tag_file_name $TAG_NAME`"
|
igor@38
|
291 diff "$TEMP1" "$TEMP2" | awk '{print $3}' | sort -u >> "$vocabulary"
|
igor@38
|
292 fi
|
igor@38
|
293 fi
|
igor@38
|
294
|
igor@38
|
295 rm -f "$TEMP1" "$TEMP2" "${TEMP1}-full" "$ORIGINAL_TEXT"
|