"Robo-Human" Intelligent

Asked

Viewed 136 times

3

I’m making a "little human" that you text, and he answers you.

Example: If you say so "Hi" he answers you "Hello, all right?".

I know this is basic, you can use a switch.

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

But I would like to automatically identify what he type.

Example: If I write a sentence "Hello, what’s your name", he would identify "name" and "hello" in the sentence and would answer "Hello, my name is Pleasure John!"

What I must study and learn to make a prototype of this level?

  • How about? https://www.wired.com/2014/04/out-in-the-open-jasper/

  • A simpler solution is to use the method explodes, if I have time I’ll show you a simple example

2 answers

3

You can do it like 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?';
}

Even so I prefer the first one, that you can already define the words/phrases that you want to do match

  • a great response and functional. A question I can put an array instead of hello ? $array = array('hi','ola','hello'); Example. $ola = strips($phrase, $array') !== false

  • I’ll put a reply with array, same final result @Lucasbicalleto

  • Beauty @Miguel.

  • Haha @Miguel really the 1 would be better put this second way that passed I will use in my work for a project I have to do. Thanks for the great reply

  • With stripos could give changed result, why an entry for example as 'I’m in Olária', would enter the case hello.

  • 1

    Yes @Ivics is true, obagdo for having called attention to this

Show 1 more comment

2


See if it suits 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 separate it at each spacing (to make it easier to compare) and at the end go comparing whether the array of words entries have such words, according to which you give the answer, I had thought this thing of array, because when I was doing I saw that the user can type hi or hello or others, so I implemented a function that only gets two arrays, one with the system dictionary and the other with the user input, then it goes through the dictionary until finding a word that contains in the user input.

Example of inputs and outputs:

Entrance: Hello

Output: Hi.


Input: Hello what is your name ?

Exit: Hi, my name and Jose, pleasure.


How old are you ?

Exit: Ops, I was just born, so I’m no year old


Input: Who is your father ?

Exit: My father is Antonio.


Input: Bye Jose

Output: Bye Bye


Any doubt just comment.

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

  • @Ivcs great response was just what I would like and we will not let go of Miguel’s reply that helped me understand also and I will use the two for separate projects thanks staff!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.