To start: I know this system will have flaws!
NOTE: Im adding a few other languages because I don't find this problem specific to php..A JavaScript or jquery solution would work...I could change the language ...Its the method i am after!
What: I am trying to parse a string to determine what the user desires.
The idea is that the string is generated from voice
Example 1: Turn my kitchen lights on and my bedroom and living room lights off.
Example 2: Turn my kitchen lights on and my bedroom lights on and living room lights off.
Example 3: Turn my kitchen and my bedroom and living room lights off.
This is an overly simplified example but please note that I want to scale beyond these three room as well as just controlling the lights example: outside ceiling fan on...
How: I am currently using a few while loops to iterate through an array and the checking if certain strings are in the array.
More how: My idea was to first split on the string on the "and". I then check each array for a on or off. If it does not have a on or off i join the array with the next.
Help: I would love to clean this concept up as well as see someone else s ideas...I am up for anything ..
Thanks JT
CODE:
$input = 'kitchen lights on and bed and living lights off';
$output = preg_split( "/ (and) /", $input );
$num = (int)count($output);
$i=0;
while($i<$num){
if ((strpos($output[$i],'on') !== false)||(strpos($output[$i],'off') !== false)) {}
elseif(((strpos($output[$i+1],'on') !== false)||(strpos($output[$i+1],'off') !== false))){
$output[$i+1] .= ' + '.$output[$i];
unset($output[$i]);
}
$i++;
}
$output = array_values($output);
$i=0;
$num = (int)count($output);
echo '<br>';
while($i<$num){
if ((strpos($output[$i],'lights') !== false)&&(strpos($output[$i],'on') !== false)&&(strpos($output[$i],'kitchen') !== false)){
echo'kitchen lights on<br>';
}
if ((strpos($output[$i],'lights') !== false)&&(strpos($output[$i],'off') !== false)&&(strpos($output[$i],'kitchen') !== false)){
echo'kitchen lights off<br>';
}
if ((strpos($output[$i],'lights') !== false)&&(strpos($output[$i],'on') !== false)&&(strpos($output[$i],'living') !== false)){
echo'living lights on<br>';
}
if ((strpos($output[$i],'lights') !== false)&&(strpos($output[$i],'off') !== false)&&(strpos($output[$i],'living') !== false)){
echo'living lights off<br>';
}
if ((strpos($output[$i],'lights') !== false)&&(strpos($output[$i],'on') !== false)&&(strpos($output[$i],'bed') !== false)){
echo'bed lights on<br>';
}
if ((strpos($output[$i],'lights') !== false)&&(strpos($output[$i],'off') !== false)&&(strpos($output[$i],'bed') !== false)){
echo'bed lights off<br>';
}
$i++;
}
Code trial 2: Note: This handles all the above examples!
<?php
//works list
$inp[]='turn the lights in the bedroom on';
$inp[]='Turn on the bedroom light';
$inp[]='turn on the lights in the bedroom';
$inp[]='Turn my kitchen and my bedroom and living room lights off.';
$inp[]='Turn the light in the kitchen on and the fan in the bedroom off';
$inp[]='Turn my kitchen lights on and my bedroom and living room lights off';
$inp[]='Turn my kitchen fan and my bedroom lights on and living room lights off.';
$inp[]='Turn my kitchen lights on and my bedroom lights on and living room lights off';
$inp[] = 'kitchen lights on and bath and living lights off';
$inp[] = 'flip on the lights in the living room';
$inp[] = 'turn on all lights';
//does not work list
//$inp[] = 'turn on all lights but living';
foreach ($inp as $input){
$input = trim($input);
$input = rtrim($input, '.');
$input = trim($input);
$input = rtrim($input, '.');
$words = explode(" ", $input);
$state = array('and','but','on','off','all','living','bed','bedroom','bath','kitchen','dining','light','lights','fan','tv');
$result = array_intersect($words, $state);
$result = implode(" ", $result);
$result = trim($result);
//$result = preg_split('/(and|but)/',$input,-1, PREG_SPLIT_DELIM_CAPTURE);
$result = preg_split( "/ (and|but) /", $result );
//$result = explode("and", $result);
$sep=array();
foreach($result as $string){
$word = explode(" ", $string);
$sep[]=$word;
}
$test=array();
$num = (int)count($sep);
$i=0;
while($i<($num)){
$result = (int)count(array_intersect($sep[$i], $state));
$j=$i;
while($result<=3)
{
$imp = implode(" ", $sep[$j]);
if(isset($test[$i])){$test[$i]=$imp.' '.$test[$i];}
else{$test[$i]=$imp;}
if ($result>=3){$j++;break;}
$result = (int)count(array_intersect($sep[++$j], $state));
}
$i=$j;
}
print_r($test);
echo '<br>';
}
?>