Python Virtual Assistant - Efficiency

Asked

Viewed 4,993 times

7

I’m creating a virtual assistant in Python, and I want it to be more "human". I want her to interpret my request, and not just compare what I said to a string. So my question is:

To cover all variations of a request, for example, "It’s gonna rain tomorrow?" and "I’m gonna need an umbrella tomorrow?", is it necessary to have an if condition for each request? For example:

if audio == "Vai chover amanhã?":
    checarClima()...

if audio == "Vou precisar de uma guarda chuva amanhã?":
    checarClima()...

outros if's...

This is the way that great virtual wizards (Siri, Google Now, etc.) are made or there is a technique, a code model that is more efficient and does what you said at the beginning (interpret my request, and not just compare with a string)?

  • Cool project, google uses artificial intelligence and language processing to trace the meaning composed by the words in a sentence, is an arduous path you will need to train a database, remembering that your ifdemonstrated this comparing audio with a rsrs phrase, you know that’s not quite how it works right ? when capturing an audio usually you will have to transcribe it in text before, very complex subject for a mere comment lol

  • Yes, I understand it is not a basic subject. In question I simplified for practical purposes. What I have today, already transforms my audio into text, processes through the if’s, generates a text that is transformed into audio and is played. The core of my doubt was how to remove the plethora of if’s conditions that such a project would have, for a more efficient approach.

3 answers

4

I wasn’t going to answer that question rsrs, but...

Come on you want something nice to play ? or something professional ?

Professional - There is nowhere to run, you will have to work with neural networks, this demands a well-honed Artificial Intelligence, everything is segmented, word for word, each one receives a certain grammatical classification (verb, adjective, nouns, pronouns, etc), this will separate your entire sentence into grammatical categories, later this will be used to compose the context as a whole, it is really something complex...

child’s play - At the beginning of the Internet the existing searchers at the end of the 90’s did not possess IA to interpret a phrase typed in the search, but then how it was done? - Levenshtein is the answer, a widely used algorithm for finding next/similar words, for example if a person typed "abacachi" how would we know he meant "pineapple"? Levenshtein does this, he calculates the minimum distance between two words, imagine in his example:

"Vai chover amanhã?"

"Vou precisar de uma guarda chuva amanhã?"

Imagine that in your system you only have a basis by knowing the word chuva, temperatura and clima that is just three words to define an action for weather forecast, as those three words in your little bank would understand those phrases of yours ?

A: Segment all words and apply Levenshtein’s algorithm to each word, in the first sentence you will get a great word score chover when compared to the word of your bank chuva, That is, the word rain will stand out as the best punctuation in relation to the other words of the phrase, it gives you clues that your phrase may have to do with the mood and you could take an action depending on the scores of the other words of that phrase. The same happens for the next sentence that will have a distance equal to 0 returned by the algorithm of course the two words are equal...

See the expected results for Levenshtein for his first sentence:

chuva => vai = distancia de 4     
chuva => chover = distancia de 3 
chuva => amanhã = distancia de 6

Shortest distance is the word chover is an excellent score for a word with 6 characters, note the word vai the word only has 3 characters but has distance of 4, we can say that it is completely different from the word rain and has to be discarded even right away, gave to get the idea of how everything works/ worked, this algorithm is still very used ...

  • Great answer. I certainly don’t think about doing something professional enough to market, but as efficient as possible to actually be an assistant inside my house, with functions that really save me time like a trip to my computer (such as asking for something from the kitchen, or asking to play a song, etc.). This can be done very basic, with a lot of if’s, but since I’m doing I want to make the most efficient and right that my time allows me.

  • For those who are accompanied and also curious, I answered my own question with a very interesting link, where demonstrates and teaches the process of segregation of the sentence, removal of irrelevant words and classification of words.

  • yes you can use this to gain speed and not submit irrelevant words to get a score by Levenshtein’s algorithm

  • 1

    postgres supports pgsimilarity. It can be a good one to test Levenshtein. I did some tests a few years ago and it was much faster to run straight into the database. http://pgsimilarity.projects.pgfoundry.org/

  • 1

    @loops yes for sure, it would be much faster

3


I received some feedback from other sources:

First take a look at the NTLK and learn about Natural Processing of Language. Learn about tokenization and tagging and I think you’ll get somewhere.

This link contains a bit about NLP (Natural Language Processing): http://xrds.acm.org/blog/2017/01/build-natural-language-processing-based-intelligent-assistant-using-python-easy/

Summary of link text:

Thinking of the project macro, the flow is:

1. The workflow of a request

Voice request > PNL module processes > API

Apis are the modules that will resolve your request (climate API, search API, alarm API, etc.)

2. Tokenization
Function of the NLTK module that will separate each element (word) from the request.

Requisition: "It will rain tomorrow in São Paulo?"
Request after Tokenization: ["It will", "rain", "tomorrow", "in", "São", "Paulo", "?"]

3. Removal of "Stop Words"
Function of the NLTK module to remove words irrelevant to the request, in this case the words "Will" and "in".
Post-removal requisition: ["rain", "tomorrow", "Sao", "Paulo", "?"]

4. Tagging
Tagging is to classify each word as verb, pronoun, adjective, etc. This is important to give meaning to the phrase after tokenization and removal of stop words. NLTK also has a function that does this automatically, but I still haven’t found out if it supports English.

5. Named Entity Recognition (NER))
Also a NLTK function which will go which words are names, places, organizations, etc.

6. Call the Apis
After handling the request, it should be correctly classified so that the correct API is called. In the example, we are asking if it will rain. The word "rain" must be related to the climate API, so this API that will respond to our request.

2

A virtual assistant relies on a number of much more complex technologies such as AI, machine Learning or sentiment analysis.

Siri, for example, was born from a project called CALLUS. What I suggest is to start by using some kind of technology already tested as the Haven on Demand of HP, which even has support for Portuguese, which is rare in this type of projects.

You have a free plan if you want to test, as they even provide Speech Recognition functionality. See more here.

  • Thanks for the answer, loops. Very interesting this Haven on Demand, I will certainly study in depth, but I want to create mine (even though I know it will not reach a level like this), first because I like to create automations and second because then I can say that it was me who did it.

  • @Deniscallau In this case, you can start here. http://www.nltk.org/nltk_data/. textblob is a nice Python tool for working with nltk.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.