Банк скриптов для голосового движка Festival Wiki
Advertisement

Этот скрипт уже не работает, т.к. Google закрыл API. Новый скрипт здесь http://folklol.blogspot.ru/2012/11/festival.html

Скрипт для получения актуальной погоды

Для использования требуются:

Первая часть: вывод текста на стандартный вывод:

forecast_echo.php:

#!/usr/bin/php
<?php
    // remove + if you don't need it. it slows down rendering.
    // using Google weather API via XML
    // replace 'Санкт-Петербург' with your city.
    $xmlSource          = "http://www.google.ru/ig/api?weather=Санкт-Петербург";
    // strange, but string is coming in cp1251
    $xmlString          = iconv('cp1251','utf8',file_get_contents($xmlSource));
    $xml                = new SimplexmlElement($xmlString,0);
    
    $temperature        = $xml->xpath('/xml_api_reply/weather/current_conditions/temp_c');
    $temperature        = $temperature[0]['data'];
    $humidity           = $xml->xpath('/xml_api_reply/weather/current_conditions/humidity');
    $humidity           = $humidity[0]['data'];
    $condition          = $xml->xpath('/xml_api_reply/weather/current_conditions/condition');
    $condition          = $condition[0]['data'];
    $windCondition      = $xml->xpath('/xml_api_reply/weather/current_conditions/wind_condition');
    $windCondition      = $windCondition[0]['data'];
    $reply = array();
    $cmdReply = array();
    
    // condition
    $reply[] = "Сейч+ас на +улице";
    // Обрабатываем полученную строку чтобы подсказать festival как правильно ставить ударения
    switch ($condition) {
        case "Возможен дождь";
        $condition = "Возможен д+ождь";
        break;
        case "Сильный дождь";
        $condition = "Сильный д+ождь";
        break;
        case "Небольшой дождь";
        $condition = "Небольшой д+ождь";
        break;
        case "Дождь";
        $condition = "Д+ождь";
        break;
        case "Облачно";
        $condition = "+Облачно";
        break;
        case "Мало облачно";
        $condition = "Мало +облачно";
        break;
        case "Преимущественно облачно";
        $condition = "Преимущественно +облачно";
        break;
        case "Облачно с прояснениями";
        $condition = "+Облачно с проясн+ениями";
        break;
    }
    $reply[] = $condition;
    $reply[] = ".";
    // ~condition
    
    // temperature
    $reply[] = "Температ+ура:";
    if(substr($temperature,0,1) === '-')
        $reply[] = "м+инус";
    else    
        $reply[] = "пл+юс";
    exec(dirname(__FILE__)."/number_case.php male " . abs($temperature), $cmdReply);
    $reply[] = trim(join(' ', $cmdReply));
    $cmdReply = array();
    exec(dirname(__FILE__).'/pluralform.php ' . $temperature . ' гр+адус гр+адуса гр+адусов', $cmdReply);
    $reply[] = trim(join(' ', $cmdReply));
    $cmdReply = array();
    $reply[] = ".";
    // ~temperature
    
    // wind condition
    $reply[] = "Ск+орость в+етра:";
    preg_match('/(\d)+/', $windCondition, $match); // simpliest way
    exec(dirname(__FILE__)."/number_case.php " . "male " . $match[0], $cmdReply);
    $reply[] = trim(join(' ', $cmdReply));
    $cmdReply = array();
    exec(dirname(__FILE__).'/pluralform.php ' . $match[0] . ' м+етр м+етра м+етров', $cmdReply);
    $reply[] = trim(join(' ', $cmdReply));
    $cmdReply = array();
    $reply[] = "в сек+унду";
    $reply[] = ".";
    // ~wind conditions
    
    // humidity
    $reply[] = "Вл+ажность:";
    preg_match('/(\d)+/', $humidity, $match);
    exec(dirname(__FILE__)."/number_case.php " . "male " . $match[0], $cmdReply);
    $reply[] = trim(join(' ', $cmdReply));
    $cmdReply = array();
    exec(dirname(__FILE__).'/pluralform.php ' . $match[0] . ' проц+ент проц+ента проц+ентов', $cmdReply);
    $reply[] = trim(join(' ', $cmdReply));
    $cmdReply = array();
    
    // festival is reporting error on " . " strings
    // so lets replace them
    echo trim(str_replace(" .",".",join(' ', $reply)));

?>

Основной скрипт разговора:

say_forecast.sh

#!/bin/bash
# http://ru.festivalspeaker.wikia.com

SCRIPTS=/home/username/scripts/festival/
phrase="$(${SCRIPTS}forecast_echo.php)"
${SCRIPTS}saytext_ru.sh "$phrase"

меняем в обоих скриптах username

chmod u+x say_forecast.sh forecast_echo.php

Готово!

На днях (сегодня 3.09.2012) вместо ответа на запрос о погоде http://www.google.ru/ig/api?weather=Санкт-Петербург мне выдал следующее:

We're sorry... ... but your computer or network may be sending automated queries. To protect our users, we can't process your request right now. See Google Help for more information.

saltty

Advertisement