#!/usr/bin/perl use CGI qw(param); $filename ="karyaka-data"; ######################################## open(KARYAKA, "< $filename") || die ("couldn't open $filename: $!\n"); @filecontents =; close(KARYAKA) || die ("couldn't close $filename: $!\n"); # format Karyaki: # vopros (nachinaetsya na "1") # otvet (nachinaetsya na "2") # stroka, sostoyaschaya iz simvola "3" # [optional] esche otvet etc. # 3 # vopros (nachinaetsya na "1") # (etc.) # v konce pustaya stroka srand; @answers=(); $answer=""; $question="default"; @numbers = (); # parsing the answers list while (@filecontents>1) { $mightbe_quest = ($filecontents[0]); $test = substr ($mightbe_quest, 0, 1); if ("$test" eq "1") { @answers = (); $question = $mightbe_quest; $question =~ s/1\s//; chomp($question); shift(@filecontents); push(@questions, $question); } $mightbe_begin = ($filecontents[0]); $test = substr ($mightbe_begin, 0, 1); if ("$test" eq "2") { $begin_answ = $mightbe_begin; $begin_answ =~ s/2//; $answer = $begin_answ; shift(@filecontents); $mightbe_end = ($filecontents[0]); until ("$mightbe_end" eq "3\n") { $answer = $answer . $mightbe_end; shift(@filecontents); $mightbe_end = $filecontents[0]; } push(@answers, $answer); shift(@filecontents); } $big_answer = join("::", @answers); # creating questions list # print "Now the question is $question\n"; #creating all-the-variety-of answers list # print "And the corresponding answer is $big_answer\n\n"; push(@big_answers, $big_answer); $choose_from{$question} = $big_answer; } # we constructed two lists of karyaka contents # and a question-answer hash %choose_from open (LOG, ">>karyaka.log"); $vopros = param("comments"); # print "\nЗдравствуйте, меня зовут Каряка.\n # Я вся мохнатая. Кусаюсь. Денег нет.\n # Вот мои родные: Бука, Бяка.\n # Профессия: симулирую искусственный интеллект.\n\n # Задавайте мне вопросы, я стану на них отвечать.\n # А так пока что я не знаю, с чего начать.\n\n"; # $vopros = ; chomp($vopros); print LOG "$vopros\n"; $vopros =~ tr/Ю-Ъ/ю-ъ/; $word_before = "думаю"; # Let us construct a list of "priority" keys @priors = ("что","ли"); $key_question = "default"; $num_of_suspects = 0; foreach $question (@questions) { if ($vopros =~ /$question[\s,.!:?;]/) { $num_of_suspects = $num_of_suspects + 1; push(@mightbe_keys, $question); $vopros =~ s/$question//; } if ($vopros =~ /([ю-ъ\-]+) ли[\s,.!:?;]/) { $word_before = $1; } } if (($num_of_suspects < 4) && ($num_of_suspects > 0)) { foreach $mightbe_key(@mightbe_keys) { foreach $prior(@priors) { if ($mightbe_key eq $prior) { $key_question = $mightbe_key; } } } if ($key_question eq "default") { $n = @mightbe_keys - 1; $key_question = $mightbe_keys[$n]; } } elsif ($num_of_suspects == 0) { $key_question = "default"; } else { $key_question = "too_many"; } # Now we have the key question to find the (hopefully) corresponding answer $the_big_answer = $choose_from{$key_question}; @the_answers = split(/::/, $the_big_answer); $number = @the_answers; $number_random = rand($number); if (!defined($the_answers[$number_random])) {$number_random = @the_answers - 1;} $the_answer = $the_answers[$number_random]; # $the_answer =~ s#\#\n#g; $the_answer =~ s/word_before/$word_before/g; print "Content-type:text/html; charset=koi8-r\n\n"; print "$the_answer"; print "\\Спросить еще?\"; close (LOG) || die ("cannot close the karyaka.log file: $!");