【发布时间】:2013-09-12 11:03:51
【问题描述】:
开始:我知道这个系统会有缺陷!
注意:我添加了一些其他语言,因为我没有发现这个问题特定于 php..JavaScript 或 jquery 解决方案可以工作...我可以更改语言...它的我追求的方法!
什么:我正在尝试解析一个字符串以确定用户想要什么。
想法是字符串是从语音生成的
示例 1: 打开厨房的灯,关闭卧室和客厅的灯。
示例 2: 打开厨房灯,打开卧室灯,关闭客厅灯。
示例 3: 关掉我厨房、卧室和客厅的灯。
这是一个过于简化的示例,但请注意,我想在这三个房间之外进行扩展,并且只控制灯光 例如:外部吊扇在...
如何:我目前正在使用一些 while 循环来遍历数组并检查某些字符串是否在数组中。
更多方法:我的想法是首先拆分“and”上的字符串。然后我检查每个阵列的开或关。如果它没有开或关,我将与下一个加入阵列。
帮助:我很想清理这个概念,也想看看其他人的想法...我愿意做任何事情..
谢谢 JT
代码:
$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++;
}
代码试用 2: 注意:这将处理上述所有示例!
<?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>';
}
?>
【问题讨论】:
-
@ILA_JT 我也是...我认为这是一个有趣的练习!
-
我不会查找
and
,而只是查找预定义目标关键字kitchen
、living room
、bedroom
和最近的on
/off
的列表。一个简单的正则表达式和preg_match_all()
将涵盖。 -
@mario 你能定义最近的...字符吗?...我不确定正则表达式如何处理所有三个示例,但我喜欢看一个示例!
-
我会用
preg_split('/(on|off)/', $input, PREG_SPLIT_DELIM_CAPTURE)
进一步简化它。您在 [2*n+1] 中获取状态并使用preg_match_all('/(kitchen|living|bedroom|garage/'
在结果数组中找到 [2*n+0]` 中的房间关键字。 -
@mario 好的,你在 2*n+1 部分失去了我...为什么不把这个作为答案...只是一个想法:P
标签: php javascript jquery regex