new-words
annotate new-words-py.sh @ 38:adbc809d3924
Transition to Python started
new-words-py.sh is a wrapper around
new-words.py version which is not finished yet.
new-words-py.sh is a wrapper around
new-words.py version which is not finished yet.
author | Igor Chubin <igor@chub.in> |
---|---|
date | Sat Jan 22 23:42:31 2011 +0100 (2011-01-22) |
parents | |
children | a598e0d25784 |
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@38 | 15 -k put higher words that are similar to the known words (only for English) |
igor@38 | 16 -l lang override language settings |
igor@38 | 17 -n non-interactive mode (don't run vi) |
igor@38 | 18 -N turn off known words filtering |
igor@38 | 19 -a don't add marks (and don't save marks added by user) |
igor@38 | 20 -p pages work with specified pages only (pages = start-stop/total ) |
igor@38 | 21 -s show the text statistics (percentage of known words and so on) and exit |
igor@38 | 22 -S show your vocabulary statistics (number of words and word groups) |
igor@38 | 23 -t tag tag known words with tag |
igor@38 | 24 -T show list of active tags |
igor@38 | 25 -m tag merge the words tagged with "tag" into the main vocabulary |
igor@38 | 26 -M merge the words tagged with any tag into the main vocabulary |
igor@38 | 27 -r tag remove subvocabulary for the "tag" |
igor@38 | 28 -2 -3 find 2 and 3 words' sequences |
igor@38 | 29 |
igor@38 | 30 The language of the text can be specified also |
igor@38 | 31 by name of the program new-words (correspondent link must be created before). |
igor@38 | 32 For example, these calls are equivalent: |
igor@38 | 33 |
igor@38 | 34 de-words URL |
igor@38 | 35 new-words -l de URL |
igor@38 | 36 |
igor@38 | 37 HELP |
igor@38 | 38 } |
igor@38 | 39 |
igor@38 | 40 if [ "$1" = "-h" ] |
igor@38 | 41 then |
igor@38 | 42 show_usage |
igor@38 | 43 exit 0 |
igor@38 | 44 fi |
igor@38 | 45 |
igor@38 | 46 NEW_WORDS_PY=/home/igor/hg/new-words/new-words.py |
igor@38 | 47 WORK_DIR=~/.new-words/ |
igor@38 | 48 TEMP1=`mktemp /tmp/new-words-temp1.XXXXXXXXXX` |
igor@38 | 49 TEMP2=`mktemp /tmp/new-words-temp2.XXXXXXXXXX` |
igor@38 | 50 export ORIGINAL_TEXT=`mktemp /tmp/new-words-orig.XXXXXXXXXX` |
igor@38 | 51 editor=${EDITOR:-vim} |
igor@38 | 52 |
igor@38 | 53 # language detection |
igor@38 | 54 |
igor@38 | 55 LANGUAGE=en |
igor@38 | 56 my_name="`echo $0 | sed s@.*/@@ | sed s/-.*// `" |
igor@38 | 57 for arg |
igor@38 | 58 do |
igor@38 | 59 if echo "$arg" | grep -q http://...wikipedia.org/wiki/ |
igor@38 | 60 then |
igor@38 | 61 LANGUAGE="`echo $arg | sed s@http://@@ | sed s@.wikipedia.*@@`" |
igor@38 | 62 fi |
igor@38 | 63 done |
igor@38 | 64 [ "${my_name}" = "new" ] || LANGUAGE="$my_name" |
igor@38 | 65 |
igor@38 | 66 #---------------------------------------------------- |
igor@38 | 67 # command line options processing |
igor@38 | 68 |
igor@38 | 69 STAT_ONLY=NO |
igor@38 | 70 NEED_TO_USE_VOCABULARY_WHEN_SORT=NO |
igor@38 | 71 DONT_ADD_MARKS=NO |
igor@38 | 72 NON_INTERACTIVE_MODE=NO |
igor@38 | 73 PART_TO_PROCESS='' |
igor@38 | 74 GROUP_WORDS_BY_THREE=NO |
igor@38 | 75 GROUP_WORDS_BY_TWO=NO |
igor@38 | 76 TAG_NAME='' |
igor@38 | 77 MERGE_THIS_TAGS='' |
igor@38 | 78 TAGS_LIST_ONLY=NO |
igor@38 | 79 MERGE_TAGGED_WORDS=NO |
igor@38 | 80 MERGE_ALL_TAGGED=NO |
igor@38 | 81 DONT_ADD_MARKLINES=NO |
igor@38 | 82 FILTER_WORDS=YES |
igor@38 | 83 SHOW_VOC_STAT=NO |
igor@38 | 84 COMPRESSED_WORDLIST=NO |
igor@38 | 85 OLD_STYLE="NO" |
igor@38 | 86 while getopts Ocl:sSkanNp:t:Tm:Mr:23 opt |
igor@38 | 87 do |
igor@38 | 88 case "$opt" in |
igor@38 | 89 O) OLD_STYLE=YES;; |
igor@38 | 90 c) COMPRESSED_WORDLIST=YES;; |
igor@38 | 91 s) STAT_ONLY=YES;; |
igor@38 | 92 S) SHOW_VOC_STAT=YES;; |
igor@38 | 93 k) NEED_TO_USE_VOCABULARY_WHEN_SORT=YES;; |
igor@38 | 94 l) LANGUAGE="$OPTARG";; |
igor@38 | 95 a) DONT_ADD_MARKS=YES;; |
igor@38 | 96 n) NON_INTERACTIVE_MODE=YES;; |
igor@38 | 97 N) FILTER_WORDS=NO;; |
igor@38 | 98 p) PART_TO_PROCESS="$OPTARG";; |
igor@38 | 99 t) TAG_NAME="$OPTARG";; |
igor@38 | 100 T) TAGS_LIST_ONLY="YES";; |
igor@38 | 101 m) DONT_ADD_MARKLINES="YES"; MERGE_TAGGED_WORDS="YES"; MERGE_THIS_TAGS="$TAG_NAME $OPTARG";; |
igor@38 | 102 M) DONT_ADD_MARKLINES="YES"; MERGE_ALL_TAGGED="YES";; |
igor@38 | 103 r) REMOVE_TAG="YES"; TAG_NAME="$TAG_NAME $OPTARG";; |
igor@38 | 104 2) GROUP_WORDS_BY_TWO=YES;; |
igor@38 | 105 3) GROUP_WORDS_BY_THREE=YES;; |
igor@38 | 106 \?) # unknown flag |
igor@38 | 107 show_usage |
igor@38 | 108 exit 1;; |
igor@38 | 109 esac |
igor@38 | 110 done |
igor@38 | 111 shift `expr $OPTIND - 1` |
igor@38 | 112 |
igor@38 | 113 if [ "$1" = "-l" ] |
igor@38 | 114 then |
igor@38 | 115 LANGUAGE="$2" |
igor@38 | 116 shift 2 |
igor@38 | 117 fi |
igor@38 | 118 |
igor@38 | 119 VOCABULARY=${LANGUAGE}.txt |
igor@38 | 120 NOTES_FILE=notes-${LANGUAGE}.txt |
igor@38 | 121 |
igor@38 | 122 if [ "${SHOW_VOC_STAT}" = "YES" ] |
igor@38 | 123 then |
igor@38 | 124 $0 -l "${LANGUAGE}" -N -n ${WORK_DIR}/${VOCABULARY} | head -1 | awk '{print $5}' | tr -d "<>" |
igor@38 | 125 exit 0 |
igor@38 | 126 fi |
igor@38 | 127 |
igor@38 | 128 #---------------------------------------------------- |
igor@38 | 129 |
igor@38 | 130 get_words() |
igor@38 | 131 { |
igor@38 | 132 if [ "$OLD_STYLE" = NO ] |
igor@38 | 133 then |
igor@38 | 134 $NEW_WORDS_PY -l "$LANGUAGE" -f get_words "$1" |
igor@38 | 135 else |
igor@38 | 136 get_words_OLD "$@" |
igor@38 | 137 fi |
igor@38 | 138 } |
igor@38 | 139 |
igor@38 | 140 get_words_OLD() |
igor@38 | 141 { |
igor@38 | 142 export FILTER_WORDS |
igor@38 | 143 tr ' ' '\n' | sed 's/--/ /g' \ |
igor@38 | 144 | sed "s/'/__APOSTROPHE__/g" \ |
igor@38 | 145 | perl -MEncode -Mutf8 -n -e '$_ = decode( "utf8", $_);y/*\r,.:#@()+=—<>$;"?!|·[]^%&/ /; binmode STDOUT, ":utf8"; print if /^[[:alpha:] '"'"'_-]*$/'\ |
igor@38 | 146 | sed "s/__APOSTROPHE__/'/g" \ |
igor@38 | 147 | tr ' ' '\n' \ |
igor@38 | 148 | tee "$1" \ |
igor@38 | 149 | grep_v_english_perl \ |
igor@38 | 150 | sort | uniq -c | awk '{if ($2!="") print;}' | sort -rn |
igor@38 | 151 } |
igor@38 | 152 |
igor@38 | 153 add_stat() |
igor@38 | 154 { |
igor@38 | 155 if [ "$DONT_ADD_MARKLINES" = "YES" ] |
igor@38 | 156 then |
igor@38 | 157 cat |
igor@38 | 158 return |
igor@38 | 159 fi |
igor@38 | 160 before="$1" |
igor@38 | 161 after=${before}2 |
igor@38 | 162 cat > "$after" |
igor@38 | 163 total="`wc -w $1 | awk '{print $1}'`" |
igor@38 | 164 total_unknown="`cat $after|awk '{s=s+$1}END{print s}'`" |
igor@38 | 165 total_known="`echo $total-$total_unknown|bc`" |
igor@38 | 166 percentage="`echo '100*('$total-$total_unknown')'/$total | bc -l | sed 's/\\.\(.\).*/.\1/'`" |
igor@38 | 167 #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@38 | 168 sentences="`cat $ORIGINAL_TEXT | perl -e 'local $/; $_=<>; s/[^.]//msg; print length($_);'`" |
igor@38 | 169 |
igor@38 | 170 |
igor@38 | 171 if [ "$STAT_ONLY" = "YES" ] |
igor@38 | 172 then |
igor@38 | 173 echo "LANG KNOWN% UNKNOWN% KNOWN TOTAL WPS UWPS*10" |
igor@38 | 174 echo "$LANGUAGE $percentage `echo \(100-$percentage\) | bc -l` $total_known $total `echo $total/$sentences|bc` `echo 10*$total_unknown/$sentences|bc` " |
igor@38 | 175 rm $after |
igor@38 | 176 return 0 |
igor@38 | 177 else |
igor@38 | 178 groups="`echo $(grep '# groups' $after | awk '{print $3}')`" |
igor@38 | 179 words="`echo $(grep -v '^#' $after | wc -l)`" |
igor@38 | 180 echo "# $LANGUAGE, $percentage, <$total_known/$total>, <$groups/$words>" |
igor@38 | 181 fi |
igor@38 | 182 |
igor@38 | 183 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX` |
igor@38 | 184 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME |
igor@38 | 185 my $total=shift(@ARGV); |
igor@38 | 186 my $total_known=shift(@ARGV); |
igor@38 | 187 my $s=0; |
igor@38 | 188 my $mark_line=int($total_known*100/$total/5)*5; |
igor@38 | 189 if ($mark_line>=90) { |
igor@38 | 190 $mark_line=int($total_known*100/$total)+1; |
igor@38 | 191 } else { $mark_line +=5; }; |
igor@38 | 192 while(<>) |
igor@38 | 193 { |
igor@38 | 194 next if /^#\s*groups\s*/; |
igor@38 | 195 print; |
igor@38 | 196 /^\s*([0-9]*)\s*/; |
igor@38 | 197 $s+=$1; |
igor@38 | 198 if (($total_known+$s)*100/$total>=$mark_line) { |
igor@38 | 199 print "# $mark_line\n"; |
igor@38 | 200 if ($mark_line>=90) { $mark_line+=1; } else { $mark_line +=5; }; |
igor@38 | 201 } |
igor@38 | 202 } |
igor@38 | 203 PERL_SCRIPT |
igor@38 | 204 perl $PERL_SCRIPT_TEMP_NAME "$total" "$total_known" "$after" |
igor@38 | 205 rm $PERL_SCRIPT_TEMP_NAME |
igor@38 | 206 rm $after |
igor@38 | 207 } |
igor@38 | 208 |
igor@38 | 209 two_and_three_words() |
igor@38 | 210 { |
igor@38 | 211 if [ $GROUP_WORDS_BY_THREE = NO -a $GROUP_WORDS_BY_TWO = NO ] |
igor@38 | 212 then |
igor@38 | 213 cat |
igor@38 | 214 else |
igor@38 | 215 cat |
igor@38 | 216 |
igor@38 | 217 export GROUP_WORDS_BY_THREE |
igor@38 | 218 export GROUP_WORDS_BY_TWO |
igor@38 | 219 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-two-and-three-XXXXXXXX` |
igor@38 | 220 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME |
igor@38 | 221 #!/usr/bin/perl |
igor@38 | 222 local $/; |
igor@38 | 223 $words=<>; |
igor@38 | 224 $words=~ s@[!?;,:#1-9".]@ @g; |
igor@38 | 225 $words =~ s@\s+@ @g; |
igor@38 | 226 @words = split /\s+/, $words; |
igor@38 | 227 for ($i=0; $i<$#words-3;$i++) { |
igor@38 | 228 my ($a,$b,$c)= ($words[$i],$words[$i+1],$words[$i+2]); |
igor@38 | 229 if ($ENV{GROUP_WORDS_BY_THREE} eq "YES" and ($a && $b && $c)) { |
igor@38 | 230 print "${a}_${b}_${c}\n"; |
igor@38 | 231 }; |
igor@38 | 232 if ($ENV{GROUP_WORDS_BY_TWO} eq "YES" and ($a && $b)) { |
igor@38 | 233 print "${a}_${b}\n"; |
igor@38 | 234 }; |
igor@38 | 235 } |
igor@38 | 236 PERL_SCRIPT |
igor@38 | 237 perl $PERL_SCRIPT_TEMP_NAME "$ORIGINAL_TEXT" |
igor@38 | 238 rm $PERL_SCRIPT_TEMP_NAME |
igor@38 | 239 fi |
igor@38 | 240 } |
igor@38 | 241 |
igor@38 | 242 grep_v_english() |
igor@38 | 243 { |
igor@38 | 244 [ -e "$VOCABULARY" ] || touch "$VOCABULARY" |
igor@38 | 245 eval $(cat $VOCABULARY | tr -d "'" | xargs -n10 echo | tr ' ' '|' | sed 's/^/egrep -xv "RRRRRRR|/' | sed 's/$/"/' | tr '\n' '|')cat |
igor@38 | 246 } |
igor@38 | 247 |
igor@38 | 248 grep_v_english_perl() |
igor@38 | 249 { |
igor@38 | 250 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX` |
igor@38 | 251 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME |
igor@38 | 252 if ($ENV{FILTER_WORDS} eq "NO") { |
igor@38 | 253 while(<>) { print; } |
igor@38 | 254 exit(0); |
igor@38 | 255 } |
igor@38 | 256 $voc_files=$ENV{VOC_FILES}; |
igor@38 | 257 $voc_files=~s@^ @@; |
igor@38 | 258 for $voc_file (split /\s+/,$voc_files) { |
igor@38 | 259 if (open(VOC, $voc_file)) { |
igor@38 | 260 while (<VOC>){ |
igor@38 | 261 chomp; |
igor@38 | 262 #s/'//g; |
igor@38 | 263 $voc{$_}="1"; |
igor@38 | 264 } |
igor@38 | 265 } |
igor@38 | 266 } |
igor@38 | 267 while(<>) { |
igor@38 | 268 chomp; |
igor@38 | 269 if (not defined($voc{$_})) { print "$_\n"; } |
igor@38 | 270 } |
igor@38 | 271 PERL_SCRIPT |
igor@38 | 272 [ -e "$VOCABULARY" ] || touch "$VOCABULARY" |
igor@38 | 273 export VOCABULARY VOC_FILES |
igor@38 | 274 VOC_FILES=$VOCABULARY |
igor@38 | 275 for i in $TAG_NAME |
igor@38 | 276 do |
igor@38 | 277 VOC_FILES="${VOC_FILES} `tag_file_name $i`" |
igor@38 | 278 done |
igor@38 | 279 perl $PERL_SCRIPT_TEMP_NAME |
igor@38 | 280 rm $PERL_SCRIPT_TEMP_NAME |
igor@38 | 281 } |
igor@38 | 282 |
igor@38 | 283 group_words() |
igor@38 | 284 { |
igor@38 | 285 if [ "$OLD_STYLE" = NO ] |
igor@38 | 286 then |
igor@38 | 287 $NEW_WORDS_PY -l "$LANGUAGE" -f group_words "$1" |
igor@38 | 288 else |
igor@38 | 289 group_words_OLD "$@" |
igor@38 | 290 fi |
igor@38 | 291 } |
igor@38 | 292 |
igor@38 | 293 group_words_OLD() |
igor@38 | 294 { |
igor@38 | 295 #if [ "$LANGUAGE" != "en" ] |
igor@38 | 296 #then |
igor@38 | 297 # cat |
igor@38 | 298 # return |
igor@38 | 299 #fi |
igor@38 | 300 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-group-words-XXXXXXXX` |
igor@38 | 301 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME |
igor@38 | 302 #!/usr/bin/perl |
igor@38 | 303 |
igor@38 | 304 use Encode; |
igor@38 | 305 use utf8; |
igor@38 | 306 use Lingua::Stem::Snowball qw(stem); |
igor@38 | 307 |
igor@38 | 308 eval { |
igor@38 | 309 # http://stackoverflow.com/questions/251694/how-can-i-check-if-i-have-a-perl-module-before-using-it |
igor@38 | 310 require String::Similarity; |
igor@38 | 311 String::Similarity->import(); |
igor@38 | 312 }; |
igor@38 | 313 unless($@) |
igor@38 | 314 { |
igor@38 | 315 our $HAVE_String_Similarity=1; |
igor@38 | 316 } |
igor@38 | 317 |
igor@38 | 318 |
igor@38 | 319 sub load_notes_dict() |
igor@38 | 320 { |
igor@38 | 321 my %dict; |
igor@38 | 322 if (open(NOTES, $ENV{NOTES_FILE})) { |
igor@38 | 323 while(<NOTES>) { |
igor@38 | 324 $_ = decode( "utf8", $_); |
igor@38 | 325 chomp; |
igor@38 | 326 s/^\s+//; |
igor@38 | 327 my ($a,$b)=split /\s+/,$_,2; |
igor@38 | 328 $dict{$a}=$b; |
igor@38 | 329 } |
igor@38 | 330 } |
igor@38 | 331 return %dict; |
igor@38 | 332 } |
igor@38 | 333 |
igor@38 | 334 sub similar($$){ |
igor@38 | 335 my $a=shift; |
igor@38 | 336 my $b=shift; |
igor@38 | 337 if ($HAVE_String_Similarity) { |
igor@38 | 338 return $Similarity{"$a $b"}; |
igor@38 | 339 } |
igor@38 | 340 else { |
igor@38 | 341 return 0; |
igor@38 | 342 } |
igor@38 | 343 } |
igor@38 | 344 |
igor@38 | 345 |
igor@38 | 346 sub normalize_without_linked($) |
igor@38 | 347 { |
igor@38 | 348 if ( $ENV{LANGUAGE} eq "en" ) { return normalize_english(shift); } |
igor@38 | 349 elsif ( $ENV{LANGUAGE} eq "de" ) { return normalize_german(shift); } |
igor@38 | 350 elsif ( $ENV{LANGUAGE} eq "uk" ) { return normalize_ukrainian(shift); } |
igor@38 | 351 elsif ( $ENV{LANGUAGE} eq "io" ) { return normalize_esperanto(shift); } |
igor@38 | 352 else { return shift ; } |
igor@38 | 353 } |
igor@38 | 354 |
igor@38 | 355 sub normalize_with_linked($) |
igor@38 | 356 { |
igor@38 | 357 my $word = normalize_without_linked(shift); |
igor@38 | 358 #return $word; |
igor@38 | 359 if ($linked_words{$word}) { |
igor@38 | 360 return $linked_words{$word}; |
igor@38 | 361 } |
igor@38 | 362 else { |
igor@38 | 363 return $word; |
igor@38 | 364 } |
igor@38 | 365 } |
igor@38 | 366 |
igor@38 | 367 sub normalize($) |
igor@38 | 368 { |
igor@38 | 369 return normalize_with_linked(shift); |
igor@38 | 370 } |
igor@38 | 371 |
igor@38 | 372 sub normalize_ukrainian($) |
igor@38 | 373 { |
igor@38 | 374 $_=lc(shift); |
igor@38 | 375 s/[юіоеуаи]$//g; |
igor@38 | 376 return $_; |
igor@38 | 377 } |
igor@38 | 378 |
igor@38 | 379 sub normalize_esperanto($) |
igor@38 | 380 { |
igor@38 | 381 $_=lc(shift); |
igor@38 | 382 # verbs |
igor@38 | 383 s/i$//; s/is$//; s/os$//; s/as$//; s/us$//; |
igor@38 | 384 |
igor@38 | 385 # nouns |
igor@38 | 386 s/j?n?$//; |
igor@38 | 387 |
igor@38 | 388 return $_; |
igor@38 | 389 } |
igor@38 | 390 |
igor@38 | 391 sub normalize_german($) |
igor@38 | 392 { |
igor@38 | 393 @stems = stem('de', \@_); |
igor@38 | 394 return $stems[0]; |
igor@38 | 395 } |
igor@38 | 396 |
igor@38 | 397 sub normalize_german_($) |
igor@38 | 398 { |
igor@38 | 399 $_=lc(shift); |
igor@38 | 400 |
igor@38 | 401 s/heit$//; s/keit$//; s/tum$//; s/ung$//; s/nis$//;s/schaft$//; s/ist$//; |
igor@38 | 402 s/en$//; s/er$//; |
igor@38 | 403 |
igor@38 | 404 s/lich$//; s/ig$//; |
igor@38 | 405 s/al$//; s/isch$//; |
igor@38 | 406 s/ell$//; s/haft$//; |
igor@38 | 407 |
igor@38 | 408 s/bar$//; s/sam$//; s/lich$//; |
igor@38 | 409 |
igor@38 | 410 @prefixes=qw( |
igor@38 | 411 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@38 | 412 be emp ent er ge miss ver zer durch über um unter wieder); |
igor@38 | 413 @prefixes=(); |
igor@38 | 414 for $pref (@prefixes) { |
igor@38 | 415 s/^$pref//; |
igor@38 | 416 } |
igor@38 | 417 |
igor@38 | 418 |
igor@38 | 419 return $_; |
igor@38 | 420 } |
igor@38 | 421 |
igor@38 | 422 sub normalize_english($) |
igor@38 | 423 { |
igor@38 | 424 $_=lc(shift); |
igor@38 | 425 |
igor@38 | 426 s/s$//; |
igor@38 | 427 |
igor@38 | 428 s/ation$//; s/ness$//; s/ship$//; s/ally$//; s/ance$//;s/ity$//; s/ment$//; |
igor@38 | 429 |
igor@38 | 430 s/ed$//; |
igor@38 | 431 s/en$//; |
igor@38 | 432 s/er$//; |
igor@38 | 433 s/est$//; |
igor@38 | 434 s/ing$//; |
igor@38 | 435 |
igor@38 | 436 s/ism$//; s/ist$//; s/ful$//; s/able$//; s/ably$//; |
igor@38 | 437 s/ify$//; s/fy$//; s/ly$//; |
igor@38 | 438 s/ise$//; s/ize$//; |
igor@38 | 439 |
igor@38 | 440 s/e$//; |
igor@38 | 441 return $_; |
igor@38 | 442 } |
igor@38 | 443 |
igor@38 | 444 |
igor@38 | 445 sub compare($$) |
igor@38 | 446 { |
igor@38 | 447 my $a=shift; |
igor@38 | 448 my $b=shift; |
igor@38 | 449 $a =~ s/^\s*//; |
igor@38 | 450 $b =~ s/^\s*//; |
igor@38 | 451 my ($a1, $a2)= split /\s+/,$a,2; |
igor@38 | 452 my ($b1, $b2)= split /\s+/,$b,2; |
igor@38 | 453 |
igor@38 | 454 my $cmp = $group_weight{normalize($a2)} <=> $group_weight{normalize($b2)}; |
igor@38 | 455 |
igor@38 | 456 if ($cmp) { |
igor@38 | 457 return $cmp; |
igor@38 | 458 } |
igor@38 | 459 else { |
igor@38 | 460 if (normalize($a2) ne normalize($b2)) { |
igor@38 | 461 return normalize($a2) cmp normalize($b2); |
igor@38 | 462 } |
igor@38 | 463 else { |
igor@38 | 464 return $a1 <=> $b1; |
igor@38 | 465 } |
igor@38 | 466 } |
igor@38 | 467 } |
igor@38 | 468 |
igor@38 | 469 sub log_($) |
igor@38 | 470 { |
igor@38 | 471 return; |
igor@38 | 472 open(LOG, ">>", "/tmp/log1"); |
igor@38 | 473 print LOG $_[0]; |
igor@38 | 474 close(LOG); |
igor@38 | 475 } |
igor@38 | 476 |
igor@38 | 477 sub find_linked_words($) |
igor@38 | 478 { |
igor@38 | 479 my %linked_words; |
igor@38 | 480 my $dict = shift; |
igor@38 | 481 log_("1"); |
igor@38 | 482 log_(join(" ", keys(%$dict))); |
igor@38 | 483 |
igor@38 | 484 for $key (keys(%$dict)) { |
igor@38 | 485 $val = $dict->{$key}; |
igor@38 | 486 log_($key."\n"); |
igor@38 | 487 if ($val =~ /\@([a-z]*)/) { |
igor@38 | 488 $linked_words{normalize($key)} = normalize($1); |
igor@38 | 489 log_(normalize($key)." = ".normalize($1)."\n"); |
igor@38 | 490 } |
igor@38 | 491 } |
igor@38 | 492 return %linked_words; |
igor@38 | 493 } |
igor@38 | 494 |
igor@38 | 495 sub lc_length($) |
igor@38 | 496 { |
igor@38 | 497 my $a= shift; |
igor@38 | 498 $a =~ s/[a-z]//g; |
igor@38 | 499 return length($a); |
igor@38 | 500 } |
igor@38 | 501 |
igor@38 | 502 our %dict = load_notes_dict(); |
igor@38 | 503 our %linked_words = find_linked_words(\%dict); |
igor@38 | 504 |
igor@38 | 505 our %Vocabulary; |
igor@38 | 506 open(VOC, $ENV{VOCABULARY}) |
igor@38 | 507 or die "Can't open VOCABULARY"; |
igor@38 | 508 while (<VOC>){ |
igor@38 | 509 chomp; |
igor@38 | 510 #s/'//g; |
igor@38 | 511 $Vocabulary{normalize($_)}="1"; |
igor@38 | 512 } |
igor@38 | 513 close(VOC); |
igor@38 | 514 |
igor@38 | 515 binmode STDIN,":utf8"; |
igor@38 | 516 @lines=<STDIN>; |
igor@38 | 517 for $L (@lines) { |
igor@38 | 518 chomp($L); |
igor@38 | 519 #$L = decode( "utf8", $L); |
igor@38 | 520 $l=$L; |
igor@38 | 521 $l =~ s/^\s*//; |
igor@38 | 522 my ($a, $b)=split(/\s+/,$l,2); |
igor@38 | 523 $group_weight{normalize($b)}+=$a; |
igor@38 | 524 } |
igor@38 | 525 if ($ENV{NEED_TO_USE_VOCABULARY_WHEN_SORT} eq "YES") { |
igor@38 | 526 for $k (keys %group_weight) { |
igor@38 | 527 if (defined($Vocabulary{$k})) { |
igor@38 | 528 $group_weight{$k} *= 2; |
igor@38 | 529 } |
igor@38 | 530 } |
igor@38 | 531 } |
igor@38 | 532 @lines2 = sort { compare($b,$a) } @lines; |
igor@38 | 533 binmode STDOUT, ":utf8"; |
igor@38 | 534 print "# groups ".scalar(keys(%group_weight))."\n"; |
igor@38 | 535 if ($ENV{COMPRESSED_WORDLIST} eq "YES") { |
igor@38 | 536 my $sum = 0; |
igor@38 | 537 my $min = 9999; |
igor@38 | 538 for $L (@lines2) { |
igor@38 | 539 chomp($L); $l=$L; $l =~ s/^(\s*)//; my $spaces = $1; my ($a, $b)=split(/\s+/,$l,2); |
igor@38 | 540 $group_name = normalize($b); |
igor@38 | 541 if ($group_name ne $prev_group_name and $prev_group_name ne '' ) { |
igor@38 | 542 #print (" "x(7-length($sum))),"$sum $main_word\n"; |
igor@38 | 543 print +(" "x(7-length($sum))),"$sum $main_word\n"; |
igor@38 | 544 $sum = $a; |
igor@38 | 545 $min = length($b) + 2*lc_length($b); |
igor@38 | 546 $main_word = $b; |
igor@38 | 547 } |
igor@38 | 548 else { |
igor@38 | 549 $sum += $a; |
igor@38 | 550 if ($min > length($b) + 2*lc_length($b)) { |
igor@38 | 551 $min = length($b) + 2*lc_length($b); |
igor@38 | 552 $main_word = $b; |
igor@38 | 553 } |
igor@38 | 554 } |
igor@38 | 555 $prev_group_name = $group_name; |
igor@38 | 556 } |
igor@38 | 557 } |
igor@38 | 558 else { |
igor@38 | 559 for $l (@lines2) { |
igor@38 | 560 print "$l\n"; |
igor@38 | 561 } |
igor@38 | 562 } |
igor@38 | 563 PERL_SCRIPT |
igor@38 | 564 export VOCABULARY |
igor@38 | 565 export NEED_TO_USE_VOCABULARY_WHEN_SORT |
igor@38 | 566 export LANGUAGE |
igor@38 | 567 export COMPRESSED_WORDLIST |
igor@38 | 568 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE" |
igor@38 | 569 export NOTES_FILE |
igor@38 | 570 perl $PERL_SCRIPT_TEMP_NAME |
igor@38 | 571 rm $PERL_SCRIPT_TEMP_NAME |
igor@38 | 572 } |
igor@38 | 573 |
igor@38 | 574 text_from_url() |
igor@38 | 575 { |
igor@38 | 576 lynx -dump "$1" | perl -p -e 's@http://[a-zA-Z&_.:/0-9%?=,#+()\[\]~-]*@@' |
igor@38 | 577 } |
igor@38 | 578 |
igor@38 | 579 add_marks() |
igor@38 | 580 { |
igor@38 | 581 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX` |
igor@38 | 582 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME |
igor@38 | 583 use Encode; |
igor@38 | 584 |
igor@38 | 585 sub load_notes_dict() |
igor@38 | 586 { |
igor@38 | 587 my %dict; |
igor@38 | 588 if (open(NOTES, $ENV{NOTES_FILE})) { |
igor@38 | 589 while(<NOTES>) { |
igor@38 | 590 $_ = decode( "utf8", $_); |
igor@38 | 591 chomp; |
igor@38 | 592 s/^\s+//; |
igor@38 | 593 my ($a,$b)=split /\s+/,$_,2; |
igor@38 | 594 $dict{$a}=$b; |
igor@38 | 595 } |
igor@38 | 596 } |
igor@38 | 597 return %dict; |
igor@38 | 598 } |
igor@38 | 599 |
igor@38 | 600 %dict = load_notes_dict(); |
igor@38 | 601 |
igor@38 | 602 $file = $ARGV[0]; |
igor@38 | 603 if (open(F, $file)) { |
igor@38 | 604 @lines=<F>; |
igor@38 | 605 close(F); |
igor@38 | 606 for (@lines) {$_ = decode( "utf8", $_);}; |
igor@38 | 607 |
igor@38 | 608 if (open(F, ">$file")) { |
igor@38 | 609 binmode F, ":utf8"; |
igor@38 | 610 for (@lines) { |
igor@38 | 611 m/\s+\S+\s+(\S+)/; |
igor@38 | 612 $name=$1; |
igor@38 | 613 if (not /^#/ and defined($dict{$name})) { |
igor@38 | 614 chomp; |
igor@38 | 615 $mark=$dict{$name}; |
igor@38 | 616 $space=" "x(30-length($_)); |
igor@38 | 617 print F "$_$space$mark\n"; |
igor@38 | 618 } |
igor@38 | 619 else { |
igor@38 | 620 print F "$_"; |
igor@38 | 621 } |
igor@38 | 622 } |
igor@38 | 623 close(F); |
igor@38 | 624 } |
igor@38 | 625 } |
igor@38 | 626 PERL_SCRIPT |
igor@38 | 627 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE" |
igor@38 | 628 export NOTES_FILE |
igor@38 | 629 perl $PERL_SCRIPT_TEMP_NAME "$1" |
igor@38 | 630 rm $PERL_SCRIPT_TEMP_NAME |
igor@38 | 631 } |
igor@38 | 632 |
igor@38 | 633 remove_marks() |
igor@38 | 634 { |
igor@38 | 635 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX` |
igor@38 | 636 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME |
igor@38 | 637 $file = $ARGV[0]; |
igor@38 | 638 our %dict; |
igor@38 | 639 if (open(F, $file)) { |
igor@38 | 640 @lines=<F>; |
igor@38 | 641 close(F); |
igor@38 | 642 |
igor@38 | 643 if (open(F, ">$file")) { |
igor@38 | 644 for (@lines) { |
igor@38 | 645 chomp; |
igor@38 | 646 if (not /^#/ and m/(\s+)(\S+)(\s+)(\S+)(\s+)(.*)/) { |
igor@38 | 647 my $name=$4; |
igor@38 | 648 my $comment=$6; |
igor@38 | 649 $dict{$name}=$comment; |
igor@38 | 650 print F "$1$2$3$4\n"; |
igor@38 | 651 } |
igor@38 | 652 else { |
igor@38 | 653 print F "$_\n"; |
igor@38 | 654 } |
igor@38 | 655 } |
igor@38 | 656 } |
igor@38 | 657 } |
igor@38 | 658 if (($ENV{DONT_ADD_MARKS} ne "YES") and open(NOTES, $ENV{NOTES_FILE})) { |
igor@38 | 659 @lines=<NOTES>; |
igor@38 | 660 close(NOTES); |
igor@38 | 661 |
igor@38 | 662 if (open(NOTES, ">".$ENV{NOTES_FILE})) { |
igor@38 | 663 for (@lines) { |
igor@38 | 664 chomp; |
igor@38 | 665 s/^\s+//; |
igor@38 | 666 my ($a,$b)=split /\s+/,$_,2; |
igor@38 | 667 if (not defined($dict{$a}) || ($dict{$a} eq $b)) { |
igor@38 | 668 print NOTES "$_\n"; |
igor@38 | 669 if (defined($dict{$a})) { unset($dict{$a}); } |
igor@38 | 670 } |
igor@38 | 671 } |
igor@38 | 672 for (keys %dict) { |
igor@38 | 673 $mark=$dict{$_}; |
igor@38 | 674 $space=" "x(30-length($_)); |
igor@38 | 675 print NOTES "$_$space$mark\n"; |
igor@38 | 676 } |
igor@38 | 677 } |
igor@38 | 678 } |
igor@38 | 679 PERL_SCRIPT |
igor@38 | 680 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE" |
igor@38 | 681 export NOTES_FILE |
igor@38 | 682 export DONT_ADD_MARKS |
igor@38 | 683 perl $PERL_SCRIPT_TEMP_NAME "$1" |
igor@38 | 684 rm $PERL_SCRIPT_TEMP_NAME |
igor@38 | 685 } |
igor@38 | 686 |
igor@38 | 687 part() |
igor@38 | 688 { |
igor@38 | 689 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-part-XXXXXXXX` |
igor@38 | 690 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME |
igor@38 | 691 #!/usr/bin/perl |
igor@38 | 692 |
igor@38 | 693 my @lines=<STDIN>; |
igor@38 | 694 my $lines=$#lines; |
igor@38 | 695 my $interval=$ARGV[0]; |
igor@38 | 696 if (not $interval) { |
igor@38 | 697 print @lines; |
igor@38 | 698 } |
igor@38 | 699 else { |
igor@38 | 700 my ($start,$stop,$total); |
igor@38 | 701 if ($interval =~ m@(.*)/(.*)@) { |
igor@38 | 702 $start = $1; |
igor@38 | 703 $total = $2; |
igor@38 | 704 } |
igor@38 | 705 else { |
igor@38 | 706 $start=$interval; |
igor@38 | 707 $total=0; |
igor@38 | 708 } |
igor@38 | 709 if ($start =~ m@(.*)-(.*)@) { |
igor@38 | 710 $start = $1; |
igor@38 | 711 $stop = $2; |
igor@38 | 712 } |
igor@38 | 713 if ($start =~ m@(.*)\+(.*)@) { |
igor@38 | 714 $start = $1; |
igor@38 | 715 $stop = $start+$2; |
igor@38 | 716 } |
igor@38 | 717 |
igor@38 | 718 $start=int($lines/$total*$start); |
igor@38 | 719 $stop=int($lines/$total*$stop); |
igor@38 | 720 |
igor@38 | 721 for($i=$start;$i<$stop;$i++){ |
igor@38 | 722 print $lines[$i]; |
igor@38 | 723 } |
igor@38 | 724 } |
igor@38 | 725 PERL_SCRIPT |
igor@38 | 726 perl $PERL_SCRIPT_TEMP_NAME "$1" |
igor@38 | 727 rm $PERL_SCRIPT_TEMP_NAME |
igor@38 | 728 } |
igor@38 | 729 |
igor@38 | 730 if [ "$TAGS_LIST_ONLY" = "YES" ] |
igor@38 | 731 then |
igor@38 | 732 cd "${WORK_DIR}" |
igor@38 | 733 echo ${LANGUAGE}_*.txt | tr ' ' '\n' | grep -v '*' | sed 's/[^_]*_//;s/.txt$//' |
igor@38 | 734 exit 0 |
igor@38 | 735 fi |
igor@38 | 736 |
igor@38 | 737 tag_file_name() |
igor@38 | 738 { |
igor@38 | 739 echo "${LANGUAGE}_${1}.txt" |
igor@38 | 740 } |
igor@38 | 741 |
igor@38 | 742 if [ "$REMOVE_TAG" = "YES" ] |
igor@38 | 743 then |
igor@38 | 744 cd "${WORK_DIR}" |
igor@38 | 745 for i in $TAG_NAME |
igor@38 | 746 do |
igor@38 | 747 echo "$TAGNAME" | grep -q '[/*?]' && continue |
igor@38 | 748 f="`tag_file_name $i`" |
igor@38 | 749 if [ -e "$f" ] |
igor@38 | 750 then |
igor@38 | 751 rm -f "$f" && echo Tag "'$i'" removed |
igor@38 | 752 else |
igor@38 | 753 echo Unknown tag "'$i'" |
igor@38 | 754 fi |
igor@38 | 755 done |
igor@38 | 756 exit 0 |
igor@38 | 757 fi |
igor@38 | 758 |
igor@38 | 759 mkdir -p $WORK_DIR |
igor@38 | 760 oldpwd="$PWD" |
igor@38 | 761 cd $WORK_DIR |
igor@38 | 762 if [ "$MERGE_TAGGED_WORDS" = "YES" ] |
igor@38 | 763 then |
igor@38 | 764 VOC_FILES='' |
igor@38 | 765 for i in $MERGE_THIS_TAGS |
igor@38 | 766 do |
igor@38 | 767 f=`tag_file_name $i` |
igor@38 | 768 [ -e "$f" ] && VOC_FILES="${VOC_FILES} $f" |
igor@38 | 769 done |
igor@38 | 770 if [ -z "$VOC_FILES" ] |
igor@38 | 771 then |
igor@38 | 772 echo Unknown tags: $MERGE_THIS_TAGS > /dev/stderr |
igor@38 | 773 else |
igor@38 | 774 cat $VOC_FILES |
igor@38 | 775 fi |
igor@38 | 776 elif [ "$MERGE_ALL_TAGGED" = "YES" ] |
igor@38 | 777 then |
igor@38 | 778 cat ${LANGUAGE}_*.txt |
igor@38 | 779 elif echo "$1" | grep -q http: |
igor@38 | 780 then |
igor@38 | 781 text_from_url "$1" |
igor@38 | 782 elif [ "$#" != 0 ] |
igor@38 | 783 then |
igor@38 | 784 if echo $1 | grep -q ^/ |
igor@38 | 785 then |
igor@38 | 786 cat "$1" |
igor@38 | 787 else |
igor@38 | 788 cat "$oldpwd/$1" |
igor@38 | 789 fi |
igor@38 | 790 else |
igor@38 | 791 cat |
igor@38 | 792 fi \ |
igor@38 | 793 | part $PART_TO_PROCESS \ |
igor@38 | 794 | tee $ORIGINAL_TEXT \ |
igor@38 | 795 | two_and_three_words \ |
igor@38 | 796 | get_words ${TEMP1}-full \ |
igor@38 | 797 | group_words \ |
igor@38 | 798 | add_stat ${TEMP1}-full \ |
igor@38 | 799 | tee "$TEMP1" > "$TEMP2" |
igor@38 | 800 |
igor@38 | 801 if [ "$STAT_ONLY" = "YES" ] |
igor@38 | 802 then |
igor@38 | 803 cat "$TEMP1" |
igor@38 | 804 elif [ "$NON_INTERACTIVE_MODE" = "YES" ] |
igor@38 | 805 then |
igor@38 | 806 cat "$TEMP1" |
igor@38 | 807 else |
igor@38 | 808 if [ `wc -l "$TEMP2" | awk '{print $1}'` != 0 ] |
igor@38 | 809 then |
igor@38 | 810 [ "$DONT_ADD_MARKS" = "YES" ] || add_marks "$TEMP2" |
igor@38 | 811 if [ "$editor" = vim ] |
igor@38 | 812 then |
igor@38 | 813 vim -c 'set keywordprg='"$LANGUAGE" -c 'set iskeyword=@,48-57,/,.,-,_,+,,,#,$,%,~,=,48-255' "$TEMP2" < /dev/tty > /dev/tty |
igor@38 | 814 else |
igor@38 | 815 $editor "$TEMP2" |
igor@38 | 816 fi |
igor@38 | 817 remove_marks "$TEMP2" |
igor@38 | 818 |
igor@38 | 819 vocabulary="$VOCABULARY" |
igor@38 | 820 [ -n "$TAG_NAME" ] && vocabulary="`tag_file_name $TAG_NAME`" |
igor@38 | 821 diff "$TEMP1" "$TEMP2" | awk '{print $3}' | sort -u >> "$vocabulary" |
igor@38 | 822 fi |
igor@38 | 823 fi |
igor@38 | 824 |
igor@38 | 825 rm -f "$TEMP1" "$TEMP2" "${TEMP1}-full" "$ORIGINAL_TEXT" |