"Robo-Humano" Smart

3

I'm making a "little human" you send message, and he responds.

Example: If you send "Hi" it responds "Hi, how are you?"

I know this is basic, you can use switch .

switch($mensagem){
case "Oi":
    $responder  = "Ola";
    break;
case "xau":
    $responder = "Xau!";
    break;
}

But I'd like to automatically identify what he types.

Example : If I write a phrase

asked by anonymous 13.12.2016 / 17:19

2 answers

3

See if it caters to you:

<form acton="#" method="post">
    <input name="speak" />
    <button>Go!</button>
</form>
<?php

 function clearId($id){
     $special = Array('Á','È','ô','Ç','á','è','Ò','ç','Â','Ë','ò','â','ë','Ø','Ñ','À','Ð','ø','ñ','à','ð','Õ','Å','õ','Ý','å','Í','Ö','ý','Ã','í','ö','ã',
        'Î','Ä','î','Ú','ä','Ì','ú','Æ','ì','Û','æ','Ï','û','ï','Ù','®','É','ù','©','é','Ó','Ü','Þ','Ê','ó','ü','þ','ê','Ô','ß','‘','’','‚','“','”','„');
     $clearspc = Array('a','e','o','c','a','e','o','c','a','e','o','a','e','o','n','a','d','o','n','a','o','o','a','o','y','a','i','o','y','a','i','o','a',
        'i','a','i','u','a','i','u','a','i','u','a','i','u','i','u','','e','u','c','e','o','u','p','e','o','u','b','e','o','b','','','','','','');
     $newId = str_replace($special, $clearspc, $id);

     return strtolower($newId);
}

function comparaComArray($array, $input) {
    foreach($array as $value)
        if(in_array($value, $input))
            return true;
    return false;
}

if(count($_POST) > 0) {
    $words = strtolower(clearId($_POST['speak']));
    $words = explode(' ', $words);

    $saudacoes = array('oi', 'ola', 'ei', 'hello');

    if(comparaComArray($saudacoes, $words) and !in_array('nome', $words))
        echo "Oi. <br>";
    else if(comparaComArray($saudacoes, $words) and in_array('nome', $words))
        echo "Oi, meu nome e jose, prazer. <br>";
    else if(in_array('tchau', $words))
        echo "Bye Bye. <br>";
    else if(in_array('seu', $words) and in_array('pai', $words) and in_array('?', $words))
        echo "Meu pai é o Antonio. <br>";
    else if(in_array('quantos', $words) and in_array('anos', $words) and in_array('voce', $words))
        echo "Ops, acabei de nascer, então tenho nenhum ano de idade<br>";
}

?>
Basically the code flow is to collect a text field from the form, remove the accent from it separating it into each spacing (to make comparisons easier) and in the end go comparing whether the array of words entered have such words , according to this you give the answer, I had thought this array thing, because when I was doing that I saw that the user could type hi or hello or others, so I implemented a function that only receives two arrays , one with the system dictionary and another one with the user input, then it scrolls through the dictionary until it finds a word that contains the user input.

Example of inputs and outputs:

Entry: Hello

Output: Hi.

Entry: Hello, what's your name?

Output: Hi, my name is Jose, pleasure.

How old are you?

Output: Oops, I just got born, so I'm one year old

Entry: Who is your father?

Output: My father is Antonio.

Admission: Bye-bye

Output: Bye Bye

Any questions just comment.

Font of accent strip: How to remove accent in upload with php?

    
13.12.2016 / 18:15
4

You can do this:

$frase = 'Olá, qual seu nome?';

$ola = stripos($frase, 'olá') !== false; // cada uma destas vai ser true ou false
$nome = stripos($frase, 'nome') !== false;
$adeus = stripos($frase, 'adeus') !== false;

switch(true) {
    case($ola && $nome):
        echo 'Olá, meu nome é João prazer!';
        break;
    case($ola):
        echo 'Olá para ti também';
        break;
    case($adeus):
        echo 'Xau';
        break;
    default:
        echo 'Não queres falar comigo?';
}

With array :

$frase = 'Olá qual seu nome';
$palavras = explode(' ', $frase);

switch(true) {
    case(in_array('Olá', $palavras) && in_array('nome', $palavras)):
        echo 'Olá, meu nome é João prazer!';
        break;
    case(in_array('Olá', $palavras)):
        echo 'Olá para ti também';
        break;
    case(in_array('adeus', $palavras)):
        echo 'Xau';
        break;
    default:
        echo 'Não queres falar comigo?';
}

match

    

13.12.2016 / 17:58