Activators (triggers) in PHP

Asked

Viewed 96 times

1

I would like to know how to create an activator - or Trigger - in PHP but I don’t know where to start.

I wanted to make the user type, for example, "nato stack". PHP would recognize the first term "NATO" and play as a result "Sierra-Tango-Alpha-Charlie-Kilo".

I did a search and this is called Trigger, or Activator.

My model for creation is this: https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Phonetic.pm

Issue 1:
I got it from here with PHP:

<?php
$busca = strtolower('nato StACKoverFLoW');
$nato = str_replace("nato", "", $busca);
$trans = array("a" => "Alfa ", "b" => "Bravo ", "c" => "Charlie ", "d" => "Delta ", "e" => "Echo ", "f" => "Foxtrot ", "g" => "Golf ", "h"  => "Hotel ", "i" => "India ", "j" => "Juliet ", "k" => "Kilo ", "l" => "Lima ", "m" => "Mike ", "n" => "November ", "o" => "Oscar ", "p" => "Papa ", "q" => "Quebec ", "r" => "Romeo ", "s" => "Sierra ", "t" => "Tango ", "u" => "Uniform ", "v" => "Victor ", "w" => "Whiskey ", "x" => "Xray ", "y" => "Yankee ", "z" => "Zulu ", "1" => "One ", "2" => "Two ", "3" => "Three ", "4" => "Four ", "5" => "Five ", "6" => "Six ", "7" => "Seven ", "8" => "Eight ", "9" => "Nine ", "0" => "Zero");
echo strtr($nato, $trans);
?>
  • This repository you passed calls this "Instant Answers"

  • @That’s right! It’s called Goodie Instant Answers.

  • a not very robust solution is to test several regular expressions, one for each command, and stop when any "marry".

1 answer

1


I believe you can implement this with regular expression, follow an example:

<?php
$regex = '/nato (?<nato>[a-z0-9 ]+)/';

$vocabulario = array('a' => "Alfa", 'b' => "Bravo", 'c' => "Charlie", 'd' => "Delta", 'e' => "Echo", 'f' => "Foxtrot", 'g' => "Golf", 'h' => "Hotel", 'i' => "India", 'j' => "Juliet", 'k' => "Kilo", 'l' => "Lima", 'm' => "Mike", 'n' => "November", 'o' => "Oscar", 'p' => "Papa", 'q' => "Quebec", 'r' => "Romeo", 's' => "Sierra", 't' => "Tango", 'u' => "Uniform", 'v' => "Victor", 'w' => "Whiskey", 'x' => "Xray", 'y' => "Yankee", 'z' => "Zulu", '1' => "One", '2' => "Two", '3' => "Three", '4' => "Four", '5' => "Five", '6' => "Six", '7' => "Seven", '8' => "Eight", '9' => "Nine", '0' => "Zero");

$entrada = 'minha frase com nato stack overflow';

$temComandoNato = preg_match($regex, strtolower($entrada), $encontrados);

if($temComandoNato) {
    $letras = str_split($encontrados['nato']);

    $natos = array_map(function($letra) use($vocabulario){
        return  @$vocabulario[$letra];
    }, $letras);

    echo implode('-', $natos);
}

Parse simplified with string handling functions, the difference in output is that it does not Dash Dash "-" when it finds space.

if($temComandoNato) {
    $text = $encontrados['nato'];
    $text = wordwrap($text, 1, '-', true);
    $text = strtr($text, $vocabulario);
    echo $text;
}
  • Congratulations on the code, except for one detail, by adding two words to the sentence, PHP only transforms the first. For example "stack overflow" is only read "stack".

  • 1

    I changed it to be perfect.

  • Now yes! Just one question: regular expression represents a significant change in execution or only in code semantics?

  • from your code to mine is a significant change as I have solved the question of how you identify that the string has the NATO command, outside that the implementation I did considers the NATO anywhere in the sentence.

  • perhaps a simpler solution using your code is to check if nato is at the beginning of "input" with strpos($input, 'nato') === 0

  • I like how you used strtr, I’ll see if I can adapt my solution to use it.

  • Can PHP only accept Regex at the beginning of the sentence? In the Duckduckgo repository, the script only accepts the word - Trigger - at the beginning.

Show 3 more comments

Browser other questions tagged

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