Add multiple values to a dictionary - python

I am doing a riddle guessing game. This relies on the user getting the question correct (obviously) to progress, so the user's answer must match the question answer stored in a dictionary. This will make the quiz incredibly difficult if they don't get the exact wording the same, so is it possible to add multiple versions of a correct answer to cover this? I am using a json file, with simple "question" and "answer" keys. I have this so far, (which throws up all kinds of errors!) but hopefull yyou will get the gist of what I'm after:
[
{
"question": "I'm so fast you can't see me, though everyone else can see straight through me. I don't stop until the day you die. What am I?",
"answer": (("The blink of an eye"),("Blink of an eye"),("Blink of eye"),("Eye blink"))
}
]
Thanks!

Why don't you store the answers in a list?
{
"question": "I'm so fast you can't see me, though everyone else can see straight through me. I don't stop until the day you die. What am I?",
"answer": ["The blink of an eye","Blink of an eye","Blink of eye","Eye blink"]
}

Try something like this:
The use of array in this situation is good option. In your question the json format is wrong. Therefore you are facing the issue.
[{
"question": "I'm so fast you can't see me, though everyone else can see straight through me. I don't stop until the day you die. What am I?",
"answer": ["The blink of an eye", "Blink of an eye", "Eye blink"]
}]

If you're open to other choices instead of storing all the possible answers, try just store a single answer and compare the string similarity between user's response to the correct answer.
Some common similarity measures such as Leveinstain distance should work pretty well, also there's a nice library called fuzzywuzzy which deals with cases such as 'blink of eye' vs 'eye blink' (the ordering)

Related

Amazon Alexa hierarchical dialog

I'm playing with the Amazon Alexa SDK and I'm programming my first skill.
My goal is to programm a dialog like this:
Where do you live?
How long have you been living there?
The problem is that I can answer the question number one with keywords from the second question and then Alexa assumes that I answered to the second question and will provide me the answer for question number two.
Is there a way to prevent this?
I know I could use session attributes but this looks rather hacky to me...
Thanks for your help =)
PS: I'm using flask-ask for my Alexa Skill.
There are a few ways you could structure this dialog. The first way, similar to what you're doing currently, is to use a series of single turn dialogs. My guess is that you're not using the right slot types in your existing interaction model, which is why the wrong intents are matched.
If I were using a single turn approach, I'd have two intents.
The first intent would match responses to the question "Where do you live?"
Intent: GetAddressCity
Utterance 1: {AMAZON.US_CITY}
Utterance 2: I live in {AMAZON.US_CITY}
The second would match responses to the question "How long have you been living there?"
Intent: GetAddressDuration
Utterance 1: {AMAZON.NUMBER}
Utterance 2: {AMAZON.NUMBER} years
Utterance 3: For {AMAZON.NUMBER} years
A different approach altogether (and one that I'd likely use in this situation) would be to structure the conversation using a Multi-turn Dialog. This approach would define a single intent, with multiple required slot values.
Intent: GetLivingInfo
Utterance 1: I live in {AMAZON.US_CITY}
Utterance 2: I've lived in {AMAZON.US_CITY} for {AMAZON.NUMBER} years
One intent, with two required slots. If the user says I live in Boston, Utterance 1 will match, and Alexa will respond with a prompt asking for the number of years they've lived there. Look into Multi-turn Dialogs for more details.
I think Bill's answer is the best solution. Yes, you can use multiple questions, but the 2nd question needs to have the response information passed to it by the first response. You can do this using a DB such as DynamoDB keyed to the user's ID, but I think it is much easier to pass it in the session.
Bill is 100 percent correct fix your slot values as he recommended and the flow should work correctly . I did somewhat the same in skill MedTime if you wish to give it a try and provide me some feedback. I will be glad to work with you on building skills for Alexa.

Siri-like app: calculating similarities between a query and a predefined set of control phrases

I am trying to make a Apple Siri-like application in python in which you give it vocal commands or questions through a microphone, it determines the text version of the inputted audio, and then determines the appropriate action to take based on the meaning of the command/question. I am going to be using the Speech Recognition library to accept microphone input and convert from speech to text (via the IBM Watson Speech to Text API).
The main problem I have with it right now is that when I define an action for the app to execute when the appropriate command is given/question is asked, I don't know how to determine if the said command/question is denoting that action. Let me clarify what I mean by that with an example:
Say we have a action called hello. There are multiple ways for somebody to say "hello" to another person (or in this case, my application), such as:
"Hello"
"Hi"
"Howdy"
...Etcetera...
Of course, I want all of these ways of saying "hello" to be classified under the action of hello. That is, when someone says "hello", "hi", or "howdy", the response for the action hello should be executed (most likely just the app saying "hello" back in this case).
My first thought on how to solve this was to supply the app with all of or the most common ways to say a certain command/question. So, if I follow the previous example, I would tell the computer that "hello", "hi", and "howdy" all meant the same thing: the hello action. However, this method has a couple flaws. First off, it simply wouldn't understand ways of saying "hello" that weren't hardcoded in, such as "hey". Second off, once the responses for new commands/questions start getting coded in, it would become very tedious entering all the ways to say a certain phrase.
So then, because of the aforementioned problems, I started looking into ways to calculate the similarities between a group of sentences, and a single query. I eventually came across the Gensim library for python. I looked into it and found some very promising information on complex processes such as latent semantic indexing/analysis (LSI/LSA) and Tf-idf. However, it seemed to me like these things were mainly for comparing documents with large word counts as they rely on the frequency of certain terms. Assuming this is true, these processes wouldn't really provide me with accurate results as the commands/questions given to my app will probably be about eight words on average. I could be completely wrong, after all I know very little about these processes.
I also discovered WordNet, and how to work with it in python using the Natural Language Toolkit (NLTK). It looks like it could be useful, but I'm not sure how.
So, finally, I guess my real question here is what would be the best solution to the problem I've mentioned? Should I use one of the methods I've mentioned? Or is there a better way to do what I want that I don't know about?
Any help at all would be greatly appreciated. Thanks in advance.
P.S. Sorry for the wordy explanation; I wanted to be sure I was clear :P
This is a hard problem. It is also the subject of Task 11 of this year's set of Semantic evaluation challenges (Semeval 2017). So take a look at the task description, which will give you a road map for how this problem can be solved. The task also comes with a suite of training data, which is essential for approaching a problem like this. The challenge is still ongoing, but eventually you'll be able to learn from the solutions as well.
So the short answer to "how do I determine if some command/question is denoting a certain action" is: Use the training data from Semeval2017 (or your own of course), and write a classifier. The nltk book can help you get up to speed with writing classifiers.

What is deckware?

I stumbled upon that word in this blog post.
There is similar question in hacker news, but without a definitive answer.
Google doesn't seem to be much help either.
Deckware generally refers to "software" that's actually just a deck of slides showing how something is supposed/planned/intended to work. In other words, like the "no startup pitches" bit, it's saying "we don't want to see what you plan to build or are starting building, we want to see what you have built, and is now working."
It refers to a presentation, that is, a deck of slides. The term is derisive, implying that there is no software at all, merely a presentation about software. Compare to "vaporware".

How to manage many variables

I am making a program that asks a user a lot of questions, and I have each question defined at the top of my file. Unfortunately because of the ridiculous number of questions I need to have, the file has become extremely packed and difficult to navigate. The questions are organized by different sections, so I thought it would be great if I could fold all of the variables by section and label them with a comment.
I am using Pydev for Eclipse. I have done some searching but haven't found anything promising. Any suggestions for how to do this or how to better organize my variables?
"Data driven programming": store your questions in a data file, and your program just needs the required logic to load and present them.
The short answer is that you shouldn't have a lot of variables to manage, rather they should be organized in some way, using a list or a dictionary or some other technique.
The answer really depends on the nature of your questions and answers, but, for example, if I wanted to sum the answers together or plot the answers on a graph, it might make sense to have a list of questions and a corresponding list of answers:
qlist = [ "What's your chem grade", "What's your Math grade", . . . ]
alist = []
for q in qlist:
a = <answer>
alist.append(a)
Of course, if your questions and answers are not amenable to that approach, you could organize them some other way, for example, using a dictionary. One simple approach would be to use the question as the key, though that could get cumbersome.

Fetching google's answers, and displaying them?

I'm starting a personal assistant project. I've noticed that if you type something like "How old is Obama" on Google, the first hit is a little thing that says "51 years (August 4, 1961)". This works for a lot of things, like, if you type "Who is Romney's wife" it returns "Ann Romney (m. 1969)". This is incredibly useful. How can I fetch this data and retrieve it?
Also, if nothing pops up, like saying "How much money is google worth", then scan each of the hits one by one and determines it. (I can do the determination part, I just need to know the scanning).
Can this be done using urllib2?
Have you considered wolframalpha ?
Its more helpful in doing dynamic computations based on a vast collection of built-in data, algorithms, and methods.
Also, Here is an example:
How old is Obama

Resources