new-words

diff new-words.sh @ 0:b21fb3f1fcb7

new-words.sh добавлен в репозиторий

Пока здесь есть только маленький README-файл
и скрипт new-words.sh, который делает минимум работы.
Подробности: http://xgu.ru/wiki/new-words
author igor@book.xt.vpn
date Sun Feb 21 19:34:18 2010 +0200 (2010-02-21)
parents
children 4c9076f87241
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/new-words.sh	Sun Feb 21 19:34:18 2010 +0200
     1.3 @@ -0,0 +1,97 @@
     1.4 +#!/bin/sh
     1.5 +
     1.6 +cat <<HELP > /dev/null
     1.7 +
     1.8 +Поддержка нескольких языков:
     1.9 +
    1.10 +    new-words -l lang URL
    1.11 +
    1.12 +Например, для немецких текстов:
    1.13 +
    1.14 +    new-words -l de URL
    1.15 +
    1.16 +Или, предварительно создав соответствующую ссылку:
    1.17 +
    1.18 +    de-words URL
    1.19 +
    1.20 +HELP
    1.21 +
    1.22 +WORK_DIR=~/.new-words/
    1.23 +LANGUAGE=en
    1.24 +VOCABULARY=${LANGUAGE}.txt
    1.25 +TEMP1=`mktemp /tmp/news-words-XXXXXXXXXX`
    1.26 +TEMP2=`mktemp /tmp/news-words-XXXXXXXXXX`
    1.27 +editor=${EDITOR:-vim}
    1.28 +
    1.29 +my_name="`echo $0 | sed s/-words// | sed s@.*/@@`"
    1.30 +[ "${my_name}" = "new" ] || VOCABULARY="$my_name".txt
    1.31 +if [ "$1" = "-l" ]
    1.32 +then
    1.33 +    LANGUAGE="$2"
    1.34 +    VOCABULARY="$LANGUAGE".txt
    1.35 +    shift 2
    1.36 +fi
    1.37 +
    1.38 +get_words()
    1.39 +{
    1.40 +tr ' ' '\n' | sed 's/--/ /g' | tr A-Z a-z \
    1.41 +| tr -d '*\r,.-:#@()+=—<>$;"?!|·[]^%&'"'" \
    1.42 +| tr ' ' '\n' | grep_v_english_perl \
    1.43 +| grep -x '[a-zA-Z0-9_-]*' \
    1.44 +| sort | uniq -c | awk '{if ($2!="") print;}' | sort -rn
    1.45 +}
    1.46 +
    1.47 +grep_v_english()
    1.48 +{
    1.49 +[ -e "$VOCABULARY" ] || touch "$VOCABULARY"
    1.50 +eval $(cat $VOCABULARY | tr -d "'" | xargs -n10 echo | tr ' ' '|' | sed 's/^/egrep -xv "RRRRRRR|/' | sed 's/$/"/' | tr '\n' '|')cat
    1.51 +}
    1.52 +
    1.53 +grep_v_english_perl()
    1.54 +{
    1.55 +    PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
    1.56 +    cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
    1.57 +open(VOC, $ENV{VOCABULARY})
    1.58 + or die "Can't open VOCABULARY";
    1.59 +while (<VOC>){
    1.60 +    chomp;
    1.61 +    s/'//g;
    1.62 +    $voc{$_}="1";
    1.63 +}
    1.64 +while(<>) {
    1.65 +    chomp;
    1.66 +    if (not defined($voc{$_})) { print "$_\n"; }
    1.67 +}
    1.68 +PERL_SCRIPT
    1.69 +    [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
    1.70 +    export VOCABULARY 
    1.71 +    perl $PERL_SCRIPT_TEMP_NAME
    1.72 +    rm $PERL_SCRIPT_TEMP_NAME
    1.73 +}
    1.74 +
    1.75 +text_from_url()
    1.76 +{
    1.77 +lynx -dump "$1" | perl -p -e 's@http://[a-zA-Z&_.:/0-9%?=,#+()\[\]~-]*@@'
    1.78 +}
    1.79 +
    1.80 +mkdir -p $WORK_DIR
    1.81 +cd $WORK_DIR
    1.82 +if echo "$1" | grep -q http: 
    1.83 +then 
    1.84 +    text_from_url "$1" | get_words | tee "$TEMP1" > "$TEMP2"
    1.85 +elif [ "$#" != 0 ]
    1.86 +then
    1.87 +    cat "$1" | get_words | tee "$TEMP1" > "$TEMP2"
    1.88 +else 
    1.89 +    get_words | tee "$TEMP1" > "$TEMP2"
    1.90 +fi
    1.91 +
    1.92 +if [ "$editor" = vim ]
    1.93 +then
    1.94 +    vim -c 'set keywordprg='"$LANGUAGE" -c 'set iskeyword=@,48-57,/,.,-,_,+,,,#,$,%,~,=' "$TEMP2" < /dev/tty > /dev/tty
    1.95 +else
    1.96 +    echo 2
    1.97 +    $editor "$TEMP2"
    1.98 +fi
    1.99 +diff "$TEMP1" "$TEMP2" | awk '{print $3}' | sort -u >> "$VOCABULARY"
   1.100 +rm -f "$TEMP1" "$TEMP2"