new-words

annotate new-words-py.sh @ 51:74e05d4436ee

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