How to effectively utilize nltk in a voice assistant?

Viewed 898

first question ever here so bear with me if my etiquette is poor. I'm currently working on a project where the goal is to implement a voice assistant using python. We were recommended to use natural language processing to help the assistant parse problems more effectively and I've successfully installed nltk for this. I'm totally new to natural language processing so I've run into some confusion.

Right now my code will take a verbal input to the mic such as:

"what is the weather in Chicago?"

and sucessfully tokenize it, remove the stopwords and tag it as follows:

    import nltk # importing the natural language toolkit
    from nltk import word_tokenize # this allows us to tokenize a sentence
    from nltk.corpus import stopwords # this allows us to filter out stopwords
    # Tokenizes the sentence
    tokens = word_tokenize(text)
    print(tokens)

    # Removes stopwords from the sentence
    sWords = set(stopwords.words('english'))
    cleanTokens = [w for w in tokens if not w in sWords]
    print(cleanTokens)

    # Tags the sentence
    tagged = nltk.pos_tag(cleanTokens)
    print(tagged)

    # Prints fully processed sentence with tags attatched
    print(nltk.ne_chunk(tagged))

Output:

['what', 'is', 'the', 'weather', 'in', 'Chicago']
['weather', 'Chicago']
[('weather', 'NN'), ('Chicago', 'NNP')]
(S weather/NN (GPE Chicago/NNP))

Essentially my problem is that I'm not sure where to go from here. I haven't really found any good examples of how text like this should be used with API's to actually return the weather in Chicago.

Would I be right to simply use if/else statements like in this pseudocode?:

if tagged.contains("weather")
   city = searchForCities(tagged)
   return city.weatherReport
elif tagged.contains("time) ect...

To summarize, when you have tokenized/tagged nltk text, what's the best way for your code to determine what to do next so that the relevant information is used by the correct library?

3 Answers

Ok so you want to parse intent in an STT (Speech-To-Text') interface, preferably open-source. nltk doesn't really do intent or Speech-To-Text. "when you have tokenized/tagged nltk text, what's the best way for your code to determine what to do next so that the relevant information is used by the correct library?" is called determining the intent of the user query.

1) Here's a list of stacks being used by 10+ Top Open source Voice Assistants Projects for developers (Linux, Raspberry Pi, Windows & Mac OS X) - 12/2018

2) Mycroft AI is the leading open-source voice-assistant I'm aware of, the project is years behind and even they threw out Mozilla DeepSpeech in favor of Google's STT. Mycroft AI lists the following STT options:

  • (Default Engine:) Google's STT engine (not open-source)
  • Mozilla DeepSpeech
  • Kaldi ("a toolkit for speech recognition written in C++")

Welcome to Stackoverflow. There is no simple answer to this, which might get clear the more you read about chat bots and voice assistants. That is why this question is so good actually. When you get decently acquainted with the most prominent speech assisstant systems, e.g. am***n al**a, you will notice that internally, the speech recognition output is mapped onto "intents", e.g. "playMusicIntent", which is parametrized with e.g. the song title and the interpreter. Mapping important words (which have semantics, not function words) onto different intents is a good starting point, but also means "hardcoding" rules of the if/else scheme. Most chat bots are taylored to specific domains, e.g. a product catalogue. A good start is to learn collocations of words, e.g. "wheather" and "in", by which you can register the intent. This can be done using NLTK or Spacy. A lot must be realized using classifiers which rely on corpus data, e.g. speech act type recognition (https://www.nltk.org/book/ch06.html). NLU (natural language understanding) is most important here, given that you have a good SR engine. This might enquire a large amount of training data for intention recognition. If you give information on your domain, we (guys on SO) may help more.

I think you almost did everything. You just need an API to get data. openweathermap is a service that provides weather data, including current weather data, forecasts, and historical data to the developers of web services and mobile applications. Create an account there and get your API credential. Follow this tutorial for the rest of the thing

Related