0

I'm trying to create a simple speech based command interface, using pre-defined commands. The specific part that I am having an issue with is when the program is trying to understand parameters passed with the command. I have some working code but it isn't ideal for what I'd like.

For example, I might have a command that checks the weather as follows:

In london united kingdom what is the weather

and the pattern to be recognised would be this:

In _ what is the weather

I'm already getting the commands with some relevance based mysql queries, that's not my problem, my problem is separating out the context of "London united kingdom" from the command.

I have got some working code at the moment but it isn't ideal:

//SPLIT CONTEXT FROM COMMAND
$query = "in london what is the weather";
$trigger = "in _ what is the weather";
$triggers = explode("_", $trigger);
foreach($triggers as $word){
    $query = str_replace($word, "'", $query);
}
$query = explode("'", $query);
array_shift($query);

This will mean that $query['0'] = "london". But it can only support one word at a time, is there a way to support more than one word, by somehow treating it as a variable or something along those lines?

tl;dr

  • Code to separate parameters
  • multiple words
  • Separate it out as a string

Cheers

6
  • You want natural language recognition and parsing with identification of meaning? That's... a bit broad to answer. The biggest technology companies on the planet are investing serious resources into cracking this; don't expect to get a simple solution on Stack Overflow. Commented Apr 11, 2016 at 15:49
  • Nothing natural, pre-defined commands with pre-defined triggers. I just wondered if there was a way of recognising.. something such as: remind me to ______ later and have it recognise which command I'm referring to @deceze Commented Apr 11, 2016 at 15:51
  • 2
    It depends on how far you want to go. You could either have a fixed list of commands and then calculate similarity (for example using similar-text). Or you could create a lexer class that would try to figure out. The latter is yet to be fully achieved by the biggest companies on the speech recognition market, though. To implement the first approach you could employ regular expressions. You commands are always static, so it's the matter of (pseudocode) "What is the weather like in (.*)" Commented Apr 11, 2016 at 15:51
  • The more flexible you want it to be the less we can help you. If you're fine with recognising predefined templates, use just that: templates, like a regex. Anything more complicated than that is a research topic. Commented Apr 11, 2016 at 15:56
  • @Eihwaz I've rewritten the question with a code example, if you have anything to contribute it would be appreciated. Commented Apr 13, 2016 at 20:50

1 Answer 1

1

You can replace the _ in your "pattern" with (.+?)

To capture the end of the string, you can use (.+). Your way works too, but it's less efficient. The + (and *) are greedy by default (? is added to make it non-greedy), so they will go right to the end (and backtrack if needed to make a match, which will never happen since it's the end of the regex).

Your PHP code might look something like this:

$re = "~in (.+?) what is the (.+)~i"; 
$str = "in london united kingdom what is the weather"; 

preg_match($re, $str, $matches);

The i modifier ensures that it's not case sensitive (it's unnecessary if your strings are already all lowercase, as you said). It will match:

IN RUSSIA WHAT IS THE WEATHER

Of course, the matching will be sensitive to misspellings. It will not match:

In london united kingdom what is the whether

While it will be difficult to recognize all misspelled permutations of a phrase, you can account for some common abbreviations like:

in (.+?) what.*?s the (.+)

That will match:

in paris, france whats the weather and in poland what's the weather and also some other variations. It's not terribly sophisticated, and will also match in my backyard what changes the weather.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much @Laurel I'll give it a go, as for capitalisation the command is converted to lower case before it's parsed and I understand misspellings will cause issues :) Thanks
This works perfectly except when the parameter is at the end of the string. I'll try see if I can join the end of the string back on.
$re = "~In (.+?) what is the (.+?\z)~i"; works for the end parameters.. bit dirty but works If you add that to your answer I'll mark it as the answer
@P110 See my updated answer for a cleaner method :)
Thanks Laurel, really helped me out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.