• Авторизация


#!/usr/bin/perl -w -Ilib use strict; # 13-11-2004 11:52 к комментариям - к полной версии - понравилось!



#!/usr/bin/perl -w -Ilib

use strict;

# модули для получения информации из XML и доступа к
# веб-ресурсам, соответственно
use XML::XPath;
use LWP::UserAgent;

# верхняя и нижняя части страницы
require "./page-template.pl";

# библиотека функций. например для обработки полей присланной
# формы
require "./xml-lib.pl";

print "Content-Type: text/html;charset=utf-8\n\n";

print &header;


# обработка полей формы
my %params = &parse_form;

# получение списка слов
my @words = (
'погаснет', 'потухнет',
'собака', 'кошка',
'рыбы', 'птицы',
'капитализм', 'социализм',
'отец', 'папа',
'энциклопедический', 'толковый',
'темный', 'светлый',
'выходить', 'входить',
'фрукты', 'овощи',
'мать', 'мама',
'преподаватель', 'учитель');

my $pair = int (rand (scalar (@words) / 2));

my $first_word = $params {'first'} || $words[$pair * 2];
my $second_word = $params {'second'} || $words[$pair * 2 + 1];
my $user_word = $params {'word'};

my $first_esc = xmlescape ($first_word);
my $second_esc = xmlescape ($second_word);
my $user_esc = xmlescape ($user_word);

my $ua = LWP::UserAgent->new;
$ua -> agent("Bond, James Bond/0.07");

my $exit = 1;

$exit = 0 if $ENV{'REQUEST_METHOD'} eq 'POST';

my $reqid = undef;
my $page = 0;
my $found = 0;
my $pages = 50;

my $mode = 'by server'; # or use by page

my $first_xml;
my $second_xml;

my $first_word_matches;
my $second_word_matches;

print "
";
unless ($exit)
{

my $first_query = <

$first_esc /1 !$user_esc
0




DOC

my $second_query = <

$second_esc /1 !$user_esc
0
0




DOC

my $req = HTTP::Request -> new
( POST => 'http://xmlsearch.yandex.ru/xmlsearch');

$req -> content_type ('application/x-www-form-urlencoded');
$req -> content ($first_query);

my $response = $ua -> request ($req);
$first_xml = $response -> content;

my $first_query_success = $response -> is_success;



if ($first_query_success)
{
$first_xml = XML::XPath -> new ( xml => $first_xml );

my $error_code = $first_xml -> findvalue ('/yandexsearch/response/error/@code');
my $error = $first_xml -> findvalue ('/yandexsearch/response/error');
if ("$error_code" eq '0')
{
$first_word_matches = "$error_code";
}

elsif ("$error")
{
print "$error: $first_word и $user_word
\n";
#$exit = 1;
}
}
else
{
warn "response from server not succeed\ndetails: " .
$response -> {'code'} . " " . $response -> {'message'};
}

$req -> content ($second_query);
$response = $ua -> request ($req);

$second_xml = $response -> content;

my $second_query_success = $response -> is_success;

if ($second_query_success)
{
$second_xml = XML::XPath -> new ( xml => $second_xml );

my $error_code = $second_xml -> findvalue ('/yandexsearch/response/error/@code');
my $error = $second_xml -> findvalue ('/yandexsearch/response/error');
if ("$error_code" eq '0')
{
$second_word_matches = "$error_code";
}

elsif ("$error")
{
print "$error: $second_word и $user_word
\n";
#$exit = 1;
}
}
else
{
warn "response from server not succeed\ndetails: " .
$response -> {'code'} . " " . $response -> {'message'};

print "Внутренняя ошибка сервера\n";
$exit = 1;
}
}

if ($exit)
{
print <Перед вами пара слов. Цель игры -
подобрать третье слово, которое с одним
из слов данной пары встречается намного
чаще, чем с другим, т.е. наиболее
«контрастное».

При подсчете очков учитываются только
случаи, когда ваше слово и слово
из пары стоят подряд (образуют
словосочетание). Разные формы введенного
слова считаются разными словами.


INTRO


}
else
{

if ($mode eq 'by server')
{
$first_word_matches = $first_xml -> findvalue ('/yandexsearch/response/results/grouping/found[@priority = "strict"]/text()')
unless defined $first_word_matches;
$second_word_matches = $second_xml -> findvalue ('/yandexsearch/response/results/grouping/found[@priority = "strict"]/text()')
unless defined $second_word_matches;
}

elsif ($mode eq 'by page')
{
$first_word_matches = $first_xml -> findvalue ('/yandexsearch/response/found[@priority = "strict"]/text()')
unless defined $first_word_matches;
$second_word_matches = $second_xml -> findvalue ('/yandexsearch/response/found[@priority = "strict"]/text()')
unless defined $second_word_matches;
}

if (defined $first_word_matches and int $first_word_matches > 0)
{ print "$first_word и $user_word: найдено $first_word_matches словосочетаний
\n" };
if (defined $second_word_matches and int $second_word_matches > 0)
{ print "$second_word и $user_word: найдено $second_word_matches словосочетаний
\n" };

print <






NEW_PHRASE
}


print "";

print <
















Задание: $first_word $second_word



Пример: для слов «луна»
и «планета» достаточно
контрастным будет слово «полная»,
так как «полная
луна» намного чаще встречается,
чем «полная планета».







FORM



print &footer;

вверх^ к полной версии понравилось! в evernote


Вы сейчас не можете прокомментировать это сообщение.

Дневник #!/usr/bin/perl -w -Ilib use strict; # | morskaya_svinka - МОРСКАЯ СВИНКА сошла с ума! | Лента друзей morskaya_svinka / Полная версия Добавить в друзья Страницы: раньше»