parse natural language

**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 '
'; while($i<$num){ if ((strpos($output[$i],'lights') !== false)&&(strpos($output[$i],'on') !== false)&&(strpos($output[$i],'kitchen') !== false)){ echo'kitchen lights on
'; } if ((strpos($output[$i],'lights') !== false)&&(strpos($output[$i],'off') !== false)&&(strpos($output[$i],'kitchen') !== false)){ echo'kitchen lights off
'; } if ((strpos($output[$i],'lights') !== false)&&(strpos($output[$i],'on') !== false)&&(strpos($output[$i],'living') !== false)){ echo'living lights on
'; } if ((strpos($output[$i],'lights') !== false)&&(strpos($output[$i],'off') !== false)&&(strpos($output[$i],'living') !== false)){ echo'living lights off
'; } if ((strpos($output[$i],'lights') !== false)&&(strpos($output[$i],'on') !== false)&&(strpos($output[$i],'bed') !== false)){ echo'bed lights on
'; } if ((strpos($output[$i],'lights') !== false)&&(strpos($output[$i],'off') !== false)&&(strpos($output[$i],'bed') !== false)){ echo'bed lights off
'; } $i++; } **Code trial 2:** Note: This handles all the above examples! =3){$j++;break;} $result = (int)count(array_intersect($sep[++$j], $state)); } $i=$j; } print_r($test); echo '
'; } ?>
@ILA_JT me too... It's a fun exercise I think!

以上就是parse natural language的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏