new-words

changeset 66:53ba2847501f

added misc scripts
author Igor Chubin <igor@chub.in>
date Tue Mar 27 14:16:46 2012 +0200 (2012-03-27)
parents 5a003076eb11
children 87bb1c5e6616
files misc/de-shell misc/memo-word-changer misc/memo.js misc/one-word-voc misc/word-changer.pl
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/misc/de-shell	Tue Mar 27 14:16:46 2012 +0200
     1.3 @@ -0,0 +1,42 @@
     1.4 +
     1.5 +make_wordlist()
     1.6 +{
     1.7 +    cp -R stardict-GR-LingvoUniversal-2.4.2 /tmp/
     1.8 +    cd /tmp/stardict-GR-LingvoUniversal-2.4.2/
     1.9 +    mv GR-Universal.dict.dz GR-Universal.dict.gz
    1.10 +    gunzip GR-Universal.dict.gz
    1.11 +    /usr/lib/stardict-tools/stardict2txt GR-Universal.ifo ~/Langs/Deutsch/GR-Universal.txt
    1.12 +    cat ~/Langs/Deutsch/GR-Universal.txt | expand| sed 's/ *<.*//; s/ *$//' > ~/Langs/Deutsch/word-list.txt
    1.13 +}
    1.14 +
    1.15 +if [ "$1" = init ]
    1.16 +then
    1.17 +    cd /usr/share/stardict/dic/
    1.18 +    make_wordlist
    1.19 +    exit 0
    1.20 +fi
    1.21 +
    1.22 +TEMP=`mktemp /tmp/nw-shell-XXXXXX`
    1.23 +WORD_LIST=~/Langs/Deutsch/word-list.txt
    1.24 +rlwrap -f ${WORD_LIST} sh -c '
    1.25 +    while true; 
    1.26 +    do 
    1.27 +        printf "\033[01;33m > " ;
    1.28 +        read word options || { exit; printf "\033[00m" ; } ;
    1.29 +        if [ "$word" = grep ]
    1.30 +        then
    1.31 +            grep "$options" "'"${WORD_LIST}"'"
    1.32 +        elif [ "$word" = vf ]
    1.33 +        then
    1.34 +            verbformen "$options"
    1.35 +        elif [ "$word" = "#" ]
    1.36 +        then
    1.37 +            printf "\033[00mWelcome to session >>"$options"<<\n"  ;
    1.38 +            echo $word $options >> ~/Langs/Deutsch/de-shell.log
    1.39 +        else
    1.40 +            printf "\033[00m" ;
    1.41 +            echo $word >> ~/Langs/Deutsch/de-shell.log
    1.42 +            de $word; 
    1.43 +        fi
    1.44 +    done'
    1.45 +rm $TEMP
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/misc/memo-word-changer	Tue Mar 27 14:16:46 2012 +0200
     2.3 @@ -0,0 +1,150 @@
     2.4 +#!/usr/bin/perl
     2.5 +
     2.6 +our $change_matix = '';
     2.7 +
     2.8 +our $change_matrix = '
     2.9 +um über mit von vom nach zum zur in an auf
    2.10 +der das des dem den ein einer einem einen eines
    2.11 +';
    2.12 +
    2.13 +our $change_matrix = '
    2.14 +router network ip route
    2.15 +';
    2.16 +
    2.17 +if (open(FILE, $ARGV[0])) {
    2.18 +    local $/;
    2.19 +    $change_matrix = <FILE>;
    2.20 +}
    2.21 +shift @ARGV;
    2.22 +
    2.23 +my $BaseURL = $ARGV[0];
    2.24 +shift;
    2.25 +
    2.26 +my $Subdir = $ARGV[0];
    2.27 +shift;
    2.28 +
    2.29 +my $StateFile = $ARGV[0];
    2.30 +shift;
    2.31 +
    2.32 +sub load_state()
    2.33 +{
    2.34 +    my %state;
    2.35 +    my $entries = 0;
    2.36 +    if (open(FILE, "$StateFile")) {
    2.37 +        while(<FILE>) {
    2.38 +            chomp;
    2.39 +            my ($k,$v) = split /\s+/, $_, 2;
    2.40 +            $state{$k} = $v;
    2.41 +            $entries += 1;
    2.42 +        }
    2.43 +    }
    2.44 +    $state{"ENTRIES"} = $entries;
    2.45 +    return \%state;
    2.46 +}
    2.47 +
    2.48 +@colors = qw(ccccff 7aedaa ddeecc ffccee);
    2.49 +
    2.50 +our %change_matrix;
    2.51 +our %color;
    2.52 +our $dropdown_number = 0;
    2.53 +
    2.54 +$i = 0;
    2.55 +for (split /\n/, $change_matrix){
    2.56 +    next if /^\s*$/;
    2.57 +    my @words = sort(split /\s+/);
    2.58 +    for my $kw (@words) {
    2.59 +        $change_matrix{$kw} = [ "???", @words ];
    2.60 +        $color{$kw} = $colors[$i%@colors];
    2.61 +    }
    2.62 +    # uppercase
    2.63 +    for (@words) {
    2.64 +        s/(.)(.*)/\U$1\E$2/;
    2.65 +    }
    2.66 +    for my $kw (@words) {
    2.67 +        $change_matrix{$kw} = [ "???", @words ];
    2.68 +        $color{$kw} = $colors[$i%@colors];
    2.69 +    }
    2.70 +    $i += 1;
    2.71 +};
    2.72 +
    2.73 +#for (keys(%change_matrix)) {
    2.74 +#    print "$_ => ".join(", ", @{$change_matrix{$_}})."\n";
    2.75 +#    print "$_ => ".$color{$_}."\n";
    2.76 +#}
    2.77 +our $CorrectAnswers = 0;
    2.78 +
    2.79 +sub generate_drop_down($$)
    2.80 +{
    2.81 +    my $word = $_[0];
    2.82 +    my %state = %{$_[1]};
    2.83 +
    2.84 +    my $name = "dropdown$dropdown_number";
    2.85 +    my $value = $state{$name};
    2.86 +    $dropdown_number += 1;
    2.87 +    my @variants = @{$change_matrix{$word}};
    2.88 +    $options = join("", map {my $selected = ""; $selected='selected="selected"' if $_ eq $value; "<option value=$_ $selected>$_</option>"} @variants);
    2.89 +    my $color = $color{$word};
    2.90 +    if ($word eq $value) {
    2.91 +        $color = "ffffff";
    2.92 +        $CorrectAnswers += 1;
    2.93 +    }
    2.94 +    if ($color) {
    2.95 +        $color = "background:#$color";
    2.96 +    }
    2.97 +    my $disabled = "";
    2.98 +    if ($value eq $word) {
    2.99 +        $disabled = "disabled";
   2.100 +    }
   2.101 +    my $onchange = "onchange='javascript:myAlert(\"$name\", this.form.$name, \"$word\")'";
   2.102 +    return "<form style='display:inline!important;'><select name='$name' $value $disabled $onchange style='font-size:80%; $color'>".$options."</select></form>"; 
   2.103 +}
   2.104 +sub highlight_word($$$)
   2.105 +{
   2.106 +    my $tag = $_[0];
   2.107 +    my $text = $_[1];
   2.108 +    my $state = $_[2];
   2.109 +
   2.110 +    if ($tag=~/^<[aA]/) {
   2.111 +        return $tag.$text;
   2.112 +    }
   2.113 +    my $re = join("|", keys(%change_matrix));
   2.114 +    $text =~ s@(^|[;> ])($re)(?=([ <]|$))@"$1".generate_drop_down($2, $state)@egms;
   2.115 +    return $tag.$text;
   2.116 +}
   2.117 +
   2.118 +my $state = load_state();
   2.119 +#my %a = %{$state};
   2.120 +#print join(", ", keys(%a)),"\n";
   2.121 +
   2.122 +$jquery = "<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js'></script>";
   2.123 +local $/;
   2.124 +$text = <>;
   2.125 +$text =~ s@(<a.*?>)(.*?)(</a>)@$2@g;
   2.126 +$text =~ s@(.*)(<body[^>]*>)@$2@ims;
   2.127 +my $before_body = $1;
   2.128 +$text =~ s@(</body[^>]*>)@<script type='text/javascript'>memo_init();</script>$1@ims;
   2.129 +$text =~ s/(<.*?>)([^<]*)(?=<)/highlight_word($1, $2, $state)/egms;
   2.130 +$text = $before_body.$text;
   2.131 +
   2.132 +my $wrong_answers = $$state{"ENTRIES"} - $CorrectAnswers;
   2.133 +$info_div = "
   2.134 +    <div style='
   2.135 +        position:fixed;
   2.136 +        bottom:0;
   2.137 +        right:0;
   2.138 +        color:white;
   2.139 +        background-color:green;
   2.140 +        font-size:70%;
   2.141 +        padding:5pt;
   2.142 +        z-index:1000;
   2.143 +        border-top-left-radius:
   2.144 +        5pt;-moz-border-radius-topleft:5pt;
   2.145 +        '>
   2.146 +            <span id='wrong_answers' style='color:#ff2222;'>$wrong_answers</span>&nbsp;&nbsp;
   2.147 +            <span id='correct_answers'>$CorrectAnswers</span>&nbsp;&nbsp;
   2.148 +            <span id='yet_to_answer'>$dropdown_number</span>
   2.149 +    </div>";
   2.150 +
   2.151 +$text =~ s@(<body[^>]*>)@<script type="text/javascript">var memo_subdir="$Subdir";</script><script type="text/javascript" src="http://xgu.ru/memo_files/memo.js"></script>$jquery$1$info_div@i;
   2.152 +$text =~ s@(<head[^>]*>)@$1<base href="$BaseURL">@i;
   2.153 +print $text;
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/misc/memo.js	Tue Mar 27 14:16:46 2012 +0200
     3.3 @@ -0,0 +1,42 @@
     3.4 +var memo_state = {
     3.5 +        'initialized' : 0,
     3.6 +        'answers' : {},
     3.7 +    };
     3.8 +
     3.9 +function memo_init() {
    3.10 +    memo_state.initialized = 1;
    3.11 +}
    3.12 +function memo_saveState() {
    3.13 +    if (memo_state.initialized == 1) {
    3.14 +        $.post("http://xgu.ru/memo/post/state/"+memo_subdir, memo_state.answers);
    3.15 +    }
    3.16 +}
    3.17 +function memo_saveAnswer(key, value) {
    3.18 +    if (memo_state.initialized == 1) {
    3.19 +        var entry = {};
    3.20 +        entry[key] = value;
    3.21 +        $.post("http://xgu.ru/memo/post/state/"+memo_subdir, entry);
    3.22 +    }
    3.23 +}
    3.24 +function myAlert(name, dropdown, correct_answer) {
    3.25 +    var myindex  = dropdown.selectedIndex;
    3.26 +    var SelValue = dropdown.options[myindex].value;
    3.27 +    memo_state.answers[name] = SelValue;
    3.28 +    if (SelValue == correct_answer) {
    3.29 +        dropdown.style.backgroundColor = '#ffffff';
    3.30 +        dropdown.disabled = true;
    3.31 +
    3.32 +        var x = $("#correct_answers").html();
    3.33 +        $("#correct_answers").html(parseInt(x) + 1);
    3.34 +
    3.35 +        x = $("#yet_to_answer").html();
    3.36 +        $("#yet_to_answer").html(parseInt(x) - 1);
    3.37 +    }
    3.38 +    else {
    3.39 +        var x = $("#wrong_answers").html();
    3.40 +        $("#wrong_answers").html(parseInt(x) + 1);
    3.41 +    }
    3.42 +    //memo_saveState();
    3.43 +    memo_saveAnswer(name, SelValue);
    3.44 +    return true;
    3.45 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/misc/one-word-voc	Tue Mar 27 14:16:46 2012 +0200
     4.3 @@ -0,0 +1,35 @@
     4.4 +
     4.5 +cat <<EOF > /dev/null
     4.6 +Как работает?
     4.7 +----------------
     4.8 +
     4.9 +Слова, которые мы должны обрабатывать поступают на стандартный поток ввода.
    4.10 +* Удаляем из них комментарии.
    4.11 +* Размножаем слова, которые, возможно, находятся не в нормальной форме.
    4.12 +  Например, слова, заканчивающиеся на te.
    4.13 +* Прогоняем через прогу, которая разложит их по категориям.
    4.14 +* Разложенные по категориям слова прогоняем ещё раз.
    4.15 +EOF
    4.16 +
    4.17 +word_multiplier()
    4.18 +{
    4.19 +    cat
    4.20 +}
    4.21 +
    4.22 +new-words -l de -R 95 -n -c -a "$@" \
    4.23 +| grep -v ^# | awk '{print $2}' | word_multiplier \
    4.24 +| while read word
    4.25 +do
    4.26 +    de-short "$word"
    4.27 +done \
    4.28 +| perl ~/hg/new-words/misc/categorized.pl \
    4.29 +| while read word 
    4.30 +do 
    4.31 +    if echo $word | fgrep -q "|"
    4.32 +    then
    4.33 +    w="$(echo $word | awk '{print $1}')"
    4.34 +    a="`de-super-short $w`"; [ -n "$a" ] && echo $w " | " $a 
    4.35 +    else
    4.36 +    echo "$word"
    4.37 +    fi
    4.38 +done
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/misc/word-changer.pl	Tue Mar 27 14:16:46 2012 +0200
     5.3 @@ -0,0 +1,87 @@
     5.4 +our $change_matrix = '
     5.5 +um über mit von vom nach zum zur in an auf
     5.6 +der das des dem den ein einer einem einen eines
     5.7 +';
     5.8 +
     5.9 +@colors = qw(ccccff 7aedaa ddeecc ffccee);
    5.10 +
    5.11 +our %change_matrix;
    5.12 +our %color;
    5.13 +our $dropdown_number = 0;
    5.14 +
    5.15 +$i = 0;
    5.16 +for (split /\n/, $change_matrix){
    5.17 +    next if /^\s*$/;
    5.18 +    my @words = sort(split /\s+/);
    5.19 +    for my $kw (@words) {
    5.20 +        $change_matrix{$kw} = [ "???", @words ];
    5.21 +        $color{$kw} = $colors[$i%@colors];
    5.22 +    }
    5.23 +    # uppercase
    5.24 +    for (@words) {
    5.25 +        s/(.)(.*)/\U$1\E$2/;
    5.26 +    }
    5.27 +    for my $kw (@words) {
    5.28 +        $change_matrix{$kw} = [ "???", @words ];
    5.29 +        $color{$kw} = $colors[$i%@colors];
    5.30 +    }
    5.31 +    $i += 1;
    5.32 +};
    5.33 +
    5.34 +for (keys(%change_matrix)) {
    5.35 +    print "$_ => ".join(", ", @{$change_matrix{$_}})."\n";
    5.36 +    print "$_ => ".$color{$_}."\n";
    5.37 +}
    5.38 +
    5.39 +sub generate_drop_down($)
    5.40 +{
    5.41 +    my $name = "dropdown$dropdown_number";
    5.42 +    $dropdown_number += 1;
    5.43 +    my $word = $_[0];
    5.44 +    my @variants = @{$change_matrix{$word}};
    5.45 +    $options = join("", map {"<option value=$_>$_</option>"} @variants);
    5.46 +    my $color = $color{$word};
    5.47 +    if ($color) {
    5.48 +        $color = "background:#$color";
    5.49 +    }
    5.50 +    my $onchange = "onchange='javascript:myAlert(this.form.$name, \"$word\")'";
    5.51 +    return "<form style='display:inline!important;'><select name='$name' $onchange style='font-size:80%; $color'>".$options."</select></form>"; 
    5.52 +}
    5.53 +sub highlight_word($$)
    5.54 +{
    5.55 +    my $tag = $_[0];
    5.56 +    my $text = $_[1];
    5.57 +    if ($tag=~/^<[aA]/) {
    5.58 +        return $tag.$text;
    5.59 +    }
    5.60 +    my $re = join("|", keys(%change_matrix));
    5.61 +    #$text =~ s@ ([Aa]uf) @<input contenteditable=True style='text-color:#eecccc' />@g;
    5.62 +    $text =~ s@(^|[;> ])($re)(?=([ <]|$))@"$1".generate_drop_down($2)@eg;
    5.63 +    return $tag.$text;
    5.64 +}
    5.65 +
    5.66 +$jquery = "<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js'></script>";
    5.67 +local $/;
    5.68 +$text = <>;
    5.69 +$text =~ s@(<a.*?>)(.*?)(</a>)@$2@g;
    5.70 +$text =~ s/(<.*?>)([^<]*)(?=<)/highlight_word($1, $2)/eg;
    5.71 +$info_div = "
    5.72 +    <div style='
    5.73 +        position:fixed;
    5.74 +        bottom:0;
    5.75 +        right:0;
    5.76 +        color:white;
    5.77 +        background-color:green;
    5.78 +        font-size:70%;
    5.79 +        padding:5pt;
    5.80 +        z-index:1000;
    5.81 +        border-top-left-radius:
    5.82 +        5pt;-moz-border-radius-topleft:5pt;
    5.83 +        '>
    5.84 +            <span id='wrong_answers' style='color:#ff2222;'>0</span>&nbsp;&nbsp;
    5.85 +            <span id='correct_answers'>0</span>&nbsp;&nbsp;
    5.86 +            <span id='yet_to_answer'>$dropdown_number</span>
    5.87 +    </div>";
    5.88 +
    5.89 +$text =~ s@(<body[^>]*>)@<script type="text/javascript" src="Kiew.js"></script>$jquery$1$info_div@i;
    5.90 +print $text;