new-words

view 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 source
1 #!/bin/sh
3 cat <<HELP > /dev/null
5 Поддержка нескольких языков:
7 new-words -l lang URL
9 Например, для немецких текстов:
11 new-words -l de URL
13 Или, предварительно создав соответствующую ссылку:
15 de-words URL
17 HELP
19 WORK_DIR=~/.new-words/
20 LANGUAGE=en
21 VOCABULARY=${LANGUAGE}.txt
22 TEMP1=`mktemp /tmp/news-words-XXXXXXXXXX`
23 TEMP2=`mktemp /tmp/news-words-XXXXXXXXXX`
24 editor=${EDITOR:-vim}
26 my_name="`echo $0 | sed s/-words// | sed s@.*/@@`"
27 [ "${my_name}" = "new" ] || VOCABULARY="$my_name".txt
28 if [ "$1" = "-l" ]
29 then
30 LANGUAGE="$2"
31 VOCABULARY="$LANGUAGE".txt
32 shift 2
33 fi
35 get_words()
36 {
37 tr ' ' '\n' | sed 's/--/ /g' | tr A-Z a-z \
38 | tr -d '*\r,.-:#@()+=—<>$;"?!|·[]^%&'"'" \
39 | tr ' ' '\n' | grep_v_english_perl \
40 | grep -x '[a-zA-Z0-9_-]*' \
41 | sort | uniq -c | awk '{if ($2!="") print;}' | sort -rn
42 }
44 grep_v_english()
45 {
46 [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
47 eval $(cat $VOCABULARY | tr -d "'" | xargs -n10 echo | tr ' ' '|' | sed 's/^/egrep -xv "RRRRRRR|/' | sed 's/$/"/' | tr '\n' '|')cat
48 }
50 grep_v_english_perl()
51 {
52 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
53 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
54 open(VOC, $ENV{VOCABULARY})
55 or die "Can't open VOCABULARY";
56 while (<VOC>){
57 chomp;
58 s/'//g;
59 $voc{$_}="1";
60 }
61 while(<>) {
62 chomp;
63 if (not defined($voc{$_})) { print "$_\n"; }
64 }
65 PERL_SCRIPT
66 [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
67 export VOCABULARY
68 perl $PERL_SCRIPT_TEMP_NAME
69 rm $PERL_SCRIPT_TEMP_NAME
70 }
72 text_from_url()
73 {
74 lynx -dump "$1" | perl -p -e 's@http://[a-zA-Z&_.:/0-9%?=,#+()\[\]~-]*@@'
75 }
77 mkdir -p $WORK_DIR
78 cd $WORK_DIR
79 if echo "$1" | grep -q http:
80 then
81 text_from_url "$1" | get_words | tee "$TEMP1" > "$TEMP2"
82 elif [ "$#" != 0 ]
83 then
84 cat "$1" | get_words | tee "$TEMP1" > "$TEMP2"
85 else
86 get_words | tee "$TEMP1" > "$TEMP2"
87 fi
89 if [ "$editor" = vim ]
90 then
91 vim -c 'set keywordprg='"$LANGUAGE" -c 'set iskeyword=@,48-57,/,.,-,_,+,,,#,$,%,~,=' "$TEMP2" < /dev/tty > /dev/tty
92 else
93 echo 2
94 $editor "$TEMP2"
95 fi
96 diff "$TEMP1" "$TEMP2" | awk '{print $3}' | sort -u >> "$VOCABULARY"
97 rm -f "$TEMP1" "$TEMP2"