Search code examples
c#voice

predicting a incoming sentence in a if statement? c#


my problem is that i am trying to predict a key within a dictionary after this string "what is the weather like in". So you probably know already what this is after that sentence. I am making an weather application with voice recognition. And i need "what is the weather like in" to be static and after that sentence a city comes. For example "what is the weather like in gothenburg". where gothenburg is a recognized city in my dictionary i have.

I tried with

static Dictionary<string, int> cityList = new Dictionary<string, int>();
if (e.PartialResult.ToString() == string.Format("what is the weather like in {0}", cityList.ContainsKey(e.PartialResult.ToString().Trim("what is the weather like in ".ToArray()))))
{
            string city = e.PartialResult.ToString();
            city = city.Trim("what is the weather like in ".ToArray());
            int cityId = cityList[city]; //here is just city the recognized "city" from the voice recognition
}

however that didn't work, if statement is not true when i say for example "what is the weather like in gothenburg". the triming on the end of the if statement is just to get the "said" city standalone. If i wouldnt trim it that would be "what is the weather like in city" and not just "city".

this is the recognized i got when i tried "what is the weather like in south park". "south park" is in my dictionary so i have no clue why the code in the if statement is not being run when this is recognized. seems right to me?

basicly the if statement is suppose to be able to predict multiple city's. "what is the weather like in gothenburg" , "what is the weather like in denmark". You name it, what i do with the city that is recognized is that i trim down that whole string so that the city is just standalone. where i seach my dictionary for an int value by the string. so that i can send out that id to the weather api server and get back a proper response just for that city that was recognized

i am using bing speech api if that makes any difference in solving this issue.


Solution

  • Here's a more readable example of what I was talking about in the comments.

    1. What we can do is first check if the user input .StartsWith the weather query string (in a case-insensitive manner below).
    2. If that's true, then we can get the city portion of the string by calling .Substring() on the user input and passing in the length of our weather query. This will return the rest of the string.
    3. Finally we can pass this to the .TryGetValue method on the cityList dictionary, which will search the keys for that string and, if found, set the out parameter to the cityId value associated with that city in the dictionary.

    Also notice that we should initialize our dictionary with a case-insensitive comparer, just to be sure we don't miss a key because of some capitalization.

    For example:

    static Dictionary<string, int> cityList = 
        new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
    
    static void SomeMethod(SomeType e)
    {
        var weatherQuery = "what is the weather like in";
        int cityId;
    
        string userInput = e.PartialResult.ToString().Trim();
    
        if (userInput.StartsWith(weatherQuery, StringComparison.OrdinalIgnoreCase) &&
            cityList.TryGetValue(userInput.Substring(weatherQuery.Length).Trim(), out cityId))
        {
            // We have a weather query and have located the cityId
            CallSomeWeatherAPI(cityId);
        }
    }