'8 Ball' Program - artificial-intelligence

I've tried looking on google but i guess I can just not get the right search phrases to find what I want. If you are familiar with the afterNET IRC server, there is a command '.8' which is an 8 ball. It answers more than just yes/no questions tho. It gives you a variety of answers based on certain words you use in your question, like when, where, color, etc
I'd like to make something like this but have no idea where to start. I've recently studied DFA (Deterministic Finite Automata), is that where I should start? I understand I don't want to be scripting out every possible combination of words people use, but it would be nice to have a system that feels sorta realistic (like the 8ball program on the IRC server), and is expandable for more 'words' whenever I want.
Thanks for any help/links!

You may be giving most 8ball implementations more credit than they deserve. I think the point is that the questions are for yes/no answers, so the provided answers only have to cover a fairly predictable set of possibilities.
Most 8ball scripts that I am aware of (example) will just use an array and a random number to grab an answer.
Magic 8ball bots are very popular on irc as they are very easy to implement - simply respond to text with a given marker (in this case ".8") and respond with a random answer.
I have never heard of a magic 8ball using a deterministic approach, Cleverbot style. Actually, trying that, I'm not even sure how deterministic that is as most of the responses are also totally random and unrelated to what I was saying.
// our answers array
String[] answers = [ "yes", "no", "for sure", "unlikely", "most certainly", "definitely not" ];
public String ask8Ball() {
// rand returns a float between 0>=res>1, the (int) cast rounds down
int index = (int)(java.lang.Math.random() * 7);
return answers[index];
}

Related

How can I return more than one value through function in C?

I have been asked in an interview how one can return more than one value from function. I have answered saying by using pointers we can achieve(call by reference) this in C. Then he told me he is looking for some other way of returning more than one value. I said we can return a struct object but here also he didn't seem to be impressed.
I would like to know others ways to return more than one value from a function.
I have seen this questions being asked here on SO, but could not find anything C specific.
The tricky problem is that the interviewer has some solution they are particularly happy with in mind and they are likely grading you by whether you have the same clever trick as them or not.
You could just name a few ways such as you did, and still not fall upon their secret trick. And if you knew their secret trick, you could well not be impressed with it.
So in these situations, its to turn it from interview into conversation. Once you detect you're not moving towards their ego, you can avoid heading towards the intimidating "I don't know" "I give up" and instead try out the "so do you have any clever solution? Is there an in-house recipe for this at Xyz Inc?" etc.
Any glimpse at their obviously self-impressed solution and you are back on firm ground where you can talk about it and ask them if they have thought about various factors that come to mind and basically interview them.
Everyone loves a good listener, and getting them to talk about their tricks is a good way to get them to leave the interview throughly impressed with you! ;)
There are a few ways:
Return value using the return statement (as you already know)
Return via references.
Return values via the heap.
Return values via global variables.
That depends on what you consider a value. If a value is a piece of information for you, more values could be a struct of values. More values could be also passed via pointers or arrays, even a char* containing a list of (non-zero alphanumerical) values. If you consider a value to be a bit of information a single returned uint32_t may hold 32 values. You could even mess around with signals or sockets or pipes or files.
But for you do not even know the use case and the requirements it imposes on the solution, it's indeed a rather hard task to come up with the right solution (and you actually did come up with some proper solutions ...).
Return a pointer to a structure, or pack several small datatypes into one large datatype, or use global variables.
The first is probably the cleanest way to do it, the other two might have their uses in certain situations.
If we pass the address instead of the true value of the parameters.
Then whenever we refer those parameters we do it with the address.
returning a pointer to structure is the suitable answer.(Obviously, the objective of the program can decide what's the best that can be done). The interviewer might have wanted you to say 'I don't know' which would have shown your lack of confidence in the field. I think you provided good solutions, though not what he had in his mind. You could have asked him about a typical scenario where he wanted multiple values to be returned and then discuss how struct-pointer is a reasonable alternative.

ANTLR and arrays

I have question relating to implementation of arrays with Java+ANTLR combo. (I'm mainly talking about java/c style arrays).
So basically I'm asking how do you implement such feature, if there is such example already available or if someone could point me to anything that may point to solve it.
On other hand, I've searched a bit how would possible solution be. Main problem that I see
is that user may create arrays of various dimensions, even go crazy if he or she wants (like creating 5 dimension arrays or worse).
While grammar for something like this is fairly simple, like
new ID (INT (',' INT)* )
back end really gets involved a bit. As I said, user may input any number of dimensions, so array dimensions should be dynamically created. (at least as I see it, maybe I'm over complicating things?)
After searching I did found something that pretty much solves this problem perfectly, here is link to the question:
Is it possible to dynamically build a multi-dimensional array in Java?
Of course, my question is, is this viable example, it is a bit (to say at least), complicated? Is there more elegant solution to it?
Having that in mind, I was thinking maybe answer might be in the grounds of somehow transforming multidimensions
into more linear structure ? Could something like that be useful ? Simple search on stackoverflow pointed many solutions
to this, like:
Algorithm to convert a multi-dimensional array to a one-dimensional array
Would it be worth to search in that direction ?
Now, at the end, having in mind that arrays are really common feature in many languages, I must find it surprising that after searching ANTLR mailing list there is no similar question, which as I previously said leads me to believe that I'm maybe over complicating things ? (Unless I really suck at search?) I would really appreciate feedback.
Your syntax, if I'm not mistaken, corresponds to something like
new char 4,5,6,7
which is kind of strange. I expect that you really meant
new char[4,5,6,7]
However from a purely syntactic point of view, there's no reason not to just store the indices in an array and let the semantic analysis pass worry about it.

Calling functions from plain text descriptions

I have an app which has common maths functions behind the scenes:
add(x, y)
multiply(x, y)
square(x)
The interface is a simple google- style text field. I want the user to be able to enter a plain text description -
'2*3'
'2 times 3'
'multiply 2 and 3'
'take the product of 2 and 3'
and get a answer mathematical answer
Question is, how should I map the text descriptions to the functions ? I'm guessing I need to
tokenise the text
identify key tokens (function names, arguments)
try and map token combinations to function signatures
However I'm guessing this is already a 'solved problem' in the machine learning space. Should I be using Natural Language Processing ? Plain text search ? Something else ?
All ideas gratefully received, plus implementation suggestions [I'm using Python/AppEngine; I know about NLTK and Whoosh]
[PS I understand Google does this already, at least for the first two queries on the list. I'm guessing they also go it statistically, having a very large amount of search data. I don't have a large amount of data available, so will need an alternative approach].
After you tokenise the text, you need parsing to get a syntax tree of your natural language phrase. Once you have this, you can map the parse tree to a mathematical expression, and then evaluate the expression. I do not think this is a solved problem. I would start with several templates, say the first two, and experiment. The larger the domain of possible descriptions, the harder the task is.
I would recommend some tool for provide grammar/patterns on text like SimpleParse for python http://www.ibm.com/developerworks/linux/library/l-simple.html. As java programmer I would prefer GATE or graph-expression.

FizzBuzz comment that confused me - are hard coded conditions wrong?

I discovered the "FizzBuzz" question today at coding horror. Great article. However, something in one of the user-comments confused me -- here's the quote:
Geez guys - EVERY ONE of you who gave
example code - EVERY ONE - hard coded
the FIZZ and BUZZ conditions...
It sounds to me like this poster is ridiculing people for "hard-coding" conditions, ie :
if(i % 3 == 0)
...
What is point the poster is trying to make? Is there another way to specify conditions in a program?
Thanks for taking the time!
Dan
the FIZZ and BUZZ conditions...
The point of Fizz Buzz is to quickly weed out non-programmers, not find the best programmer. Any reasonable function that meets the specification is acceptable for this test.
If you don't hardcode, great, you extra-pass. But, that doesn't get you out of the hard questions that are following. I usually increase the difficulty with each question, but I don't want to waste time if the candidate totally can't answer simple questions.
There's nothing wrong with hardcoding some conditions.
In the context of an interview, when I know that I'm coding FizzBuzz.java and not Enterprise Fizz Buzz with a database and 1000+ simultaneous users requiring five-nines uptime, it's ideal to hardcode these conditions.
Entry-level programmers, the ones you ask FizzBuzz at least, are to follow specifications and make solutions as simple and elegant as possible. If you're an agile software house, including such features goes against YAGNI and should be discouraged. If the interviewer doesn't ask the ability to use other factors besides three and five, then it wasn't in the spec and therefore isn't needed.
It's meant as a joke.

How do I name my build?

What is the proper way to name my revisions?
v1.0.3.20 Alpha
or
Alpha v1.0.3.20
There isn't really any "proper" way. While most people use the dot pattern "x.x.x", it isn't necessarily the proper way to do it. Likewise with your question, it is up to you.
I would suppose that the former is more natural to say, and I would prefer it, but it doesn't really matter either way.
You have two pieces of information there: version number and release state/quality. The two might be tied together, but don't have to be. For example, you could have multiple "releases" for a single version number: v1.1-alpha, v1.1-beta, v1.1-final, and so forth; or you could break those down into individual numbers: v1.0.1-alpha, v1.0.2-beta, v1.1.0-final; or something entirely different!
This is really a question of branding more than anything; due to the wide interpretation of version info across different products, you already have to know how each different product uses them to make sense of it. Of course, if you're contributing to, working with, or emulating another project: do what they do. It'll be simpler and lead to less confusion.
As used in my examples, I'd prefer "n.n...-quality", but it's only syntax.

Resources