new-words

changeset 2:68722cd6faff

Добавление пометок к словам.

Справа от слова можно поставить пометку,
которая попадает в файл ~/.new-words/notes-LANG.txt.
Потом, при появлении этого слова,
она вспоминается и выводится вместе с ним.
author igor@book.xt.vpn
date Fri Feb 26 21:41:27 2010 +0200 (2010-02-26)
parents 4c9076f87241
children c703b8898696
files README new-words.sh
line diff
     1.1 --- a/README	Mon Feb 22 08:34:11 2010 +0200
     1.2 +++ b/README	Fri Feb 26 21:41:27 2010 +0200
     1.3 @@ -14,11 +14,8 @@
     1.4  == Что сделать ==
     1.5  
     1.6  * Переписать на Perl (пока на Perl)
     1.7 -* Пометки к словам
     1.8  * Статистика по текущему тексту; уровни покрытия словами текста
     1.9  * Время добавления слова и прогресс добавления
    1.10  * Источник слова
    1.11  * Поиск предложений, содержащих слова, в тексте
    1.12  * Автоматическое определение языка по тексту
    1.13 -
    1.14 -
     2.1 --- a/new-words.sh	Mon Feb 22 08:34:11 2010 +0200
     2.2 +++ b/new-words.sh	Fri Feb 26 21:41:27 2010 +0200
     2.3 @@ -17,20 +17,21 @@
     2.4  HELP
     2.5  
     2.6  WORK_DIR=~/.new-words/
     2.7 -LANGUAGE=en
     2.8 -VOCABULARY=${LANGUAGE}.txt
     2.9  TEMP1=`mktemp /tmp/news-words-XXXXXXXXXX`
    2.10  TEMP2=`mktemp /tmp/news-words-XXXXXXXXXX`
    2.11  editor=${EDITOR:-vim}
    2.12  
    2.13 -my_name="`echo $0 | sed s/-words// | sed s@.*/@@`"
    2.14 -[ "${my_name}" = "new" ] || VOCABULARY="$my_name".txt
    2.15 +LANGUAGE=en
    2.16 +my_name="`echo $0 | sed s@.*/@@ | sed s/-.*// `"
    2.17 +[ "${my_name}" = "new" ] || LANGUAGE="$my_name"
    2.18  if [ "$1" = "-l" ]
    2.19  then
    2.20      LANGUAGE="$2"
    2.21      VOCABULARY="$LANGUAGE".txt
    2.22      shift 2
    2.23  fi
    2.24 +VOCABULARY=${LANGUAGE}.txt
    2.25 +NOTES_FILE=notes-${LANGUAGE}.txt
    2.26  
    2.27  get_words()
    2.28  {
    2.29 @@ -74,6 +75,101 @@
    2.30  lynx -dump "$1" | perl -p -e 's@http://[a-zA-Z&_.:/0-9%?=,#+()\[\]~-]*@@'
    2.31  }
    2.32  
    2.33 +add_marks()
    2.34 +{
    2.35 +    PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
    2.36 +    cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
    2.37 +$file = $ARGV[0];
    2.38 +our $dict;
    2.39 +if (open(NOTES, $ENV{NOTES_FILE})) {
    2.40 +    while(<NOTES>) {
    2.41 +        chomp;
    2.42 +        s/^\s+//;
    2.43 +        my ($a,$b)=split /\s+/,$_,2;
    2.44 +        $dict{$a}=$b;
    2.45 +    }
    2.46 +}
    2.47 +if (open(F, $file)) {
    2.48 +    @lines=<F>;
    2.49 +    close(F);
    2.50 +
    2.51 +    if (open(F, ">$file")) {
    2.52 +        for (@lines) {
    2.53 +            m/\s+\S+\s+(\S+)/;
    2.54 +            $name=$1;
    2.55 +            if (defined($dict{$name})) {
    2.56 +                chomp;
    2.57 +                $mark=$dict{$name};
    2.58 +                $space=" "x(30-length($_));
    2.59 +                print F "$_$space$mark\n";
    2.60 +            }
    2.61 +            else {
    2.62 +                print F "$_";
    2.63 +            }
    2.64 +        }
    2.65 +        close(F);
    2.66 +    }
    2.67 +}
    2.68 +PERL_SCRIPT
    2.69 +    [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
    2.70 +    export NOTES_FILE
    2.71 +    perl $PERL_SCRIPT_TEMP_NAME "$1"
    2.72 +    rm $PERL_SCRIPT_TEMP_NAME
    2.73 +}
    2.74 +
    2.75 +remove_marks()
    2.76 +{
    2.77 +    PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
    2.78 +    cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
    2.79 +$file = $ARGV[0];
    2.80 +our %dict;
    2.81 +if (open(F, $file)) {
    2.82 +    @lines=<F>;
    2.83 +    close(F);
    2.84 +
    2.85 +    if (open(F, ">$file")) {
    2.86 +        for (@lines) {
    2.87 +            chomp;
    2.88 +            if (m/(\s+)(\S+)(\s+)(\S+)(\s+)(.*)/) {
    2.89 +                my $name=$4;
    2.90 +                my $comment=$6;
    2.91 +                $dict{$name}=$comment;
    2.92 +                print F "$1$2$3$4\n";
    2.93 +            }
    2.94 +            else {
    2.95 +                print F "$_\n";
    2.96 +            }
    2.97 +        }
    2.98 +    }
    2.99 +}
   2.100 +if (open(NOTES, $ENV{NOTES_FILE})) {
   2.101 +    @lines=<NOTES>;
   2.102 +    close(NOTES);
   2.103 +
   2.104 +    if (open(NOTES, ">".$ENV{NOTES_FILE})) {
   2.105 +        for (@lines) {
   2.106 +            chomp;
   2.107 +            s/^\s+//;
   2.108 +            my ($a,$b)=split /\s+/,$_,2;
   2.109 +            if (not defined($dict{$a}) || ($dict{$a} eq $b)) {
   2.110 +                print NOTES "$_\n";
   2.111 +                if (defined($dict{$a})) { unset($dict{$a}); }
   2.112 +            }
   2.113 +        }
   2.114 +        for (keys %dict) {
   2.115 +            $mark=$dict{$_};
   2.116 +            $space=" "x(30-length($_));
   2.117 +            print NOTES "$_$space$mark\n";
   2.118 +        }
   2.119 +    }
   2.120 +}
   2.121 +PERL_SCRIPT
   2.122 +    [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
   2.123 +    export NOTES_FILE
   2.124 +    perl $PERL_SCRIPT_TEMP_NAME "$1"
   2.125 +    rm $PERL_SCRIPT_TEMP_NAME
   2.126 +}
   2.127 +
   2.128  mkdir -p $WORK_DIR
   2.129  cd $WORK_DIR
   2.130  if echo "$1" | grep -q http: 
   2.131 @@ -86,6 +182,7 @@
   2.132      get_words | tee "$TEMP1" > "$TEMP2"
   2.133  fi
   2.134  
   2.135 +add_marks "$TEMP2"
   2.136  if [ "$editor" = vim ]
   2.137  then
   2.138      vim -c 'set keywordprg='"$LANGUAGE" -c 'set iskeyword=@,48-57,/,.,-,_,+,,,#,$,%,~,=' "$TEMP2" < /dev/tty > /dev/tty
   2.139 @@ -93,5 +190,7 @@
   2.140      echo 2
   2.141      $editor "$TEMP2"
   2.142  fi
   2.143 +remove_marks "$TEMP2"
   2.144 +
   2.145  diff "$TEMP1" "$TEMP2" | awk '{print $3}' | sort -u >> "$VOCABULARY"
   2.146  rm -f "$TEMP1" "$TEMP2"