If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Javascript - No longer working

I've been using the following in a pattern matching chatbot. A person types in an input box and the chatbot responds in an output box.

If someone types "yes" the bot responds with the following except on message 4 where I ask a question. Once that question is asked it will no longer trigger which is what I want. I don't want that question to be asked over and over. This all worked until recently. I can't figure out why it no longer works. Thanks!

What is the purpose of this project? In order to give you the best answer possible, I think it would make sense to figure out your goals, and whether this approach (in general) is the best.

I can imagine a few reasons:
1. You want to learn Javascript. (If that's the case, then we should solve the immediate problem.)
2. You want to create a functional, useful AI chat (which is very hard, and this is probably not the way to do it).
3. You're doing this as some kind of test for another project (if so, what?).
4. This is for some kind of school assignment (if so, what? And keep in mind we don't do homework for you here).

1. You want to learn Javascript. (If that's the case, then we should solve the immediate problem.)

I want to solve this problem

Ok, but do you want to solve this problem to fix the code because you want this particular code to work, or do you want to accomplish some larger goal? Is this educational for you to learn JS, or is this for you to make it work again? (These different goals could have different answers.)

2. You want to create a functional, useful AI chat (which is very hard, and this is probably not the way to do it).

I already have a functioning Chatbot.

Hm. What does it accomplish? Is this the whole code, or does this integrate into something larger?
The three models of chatbot I can think of are the following:
1) Purely rule based-- if X then Y (similar to what you have here). If that's what you're working with, why is this an exception?
And this is really the only kind that is possible to create with a "perfect" result-- if you have certain expected kinds of input, you can make something that works well; further variation beyond this is more challenging.
2) Statistical, using guesses about what appears in the input and what might be a good reply. These can work (an example is "SmarterChild", a bot from AOL(?) on AIM), but they're far from perfect and don't do well with odd input. Your current system doesn't look anything like this, so that's why I asked.
3) A true grammatical model that processes language fully-- at the moment, that's basically just in science fiction. People are working on it, but the best models are statistical and just a means to an end rather than anything meant to really capture linguistic structure or be reliable in all domains.

Generally speaking, it's possible to create a reliable chatbot if you can limit the domain and vocabulary. Beyond that, you'll probably run into trouble. (We've all had the frustrating experience of talking to a robot on the phone and trying to get it to do something it isn't programmed to do, or not in the expected order.)

(For the record, I'm a Linguistics grad student, and I have some (limited) experience with this kind of thing, more observing what's out there than making it myself.)

3. You're doing this as some kind of test for another project (if so, what?).

This is 1 part of the chatbot in question.

It's up to you, but I'm confused why this would be a separate component. Wouldn't this be just one of many similar sections of code?

4. This is for some kind of school assignment (if so, what? And keep in mind we don't do homework for you here).

I'm 52 years old. I graduated a long time ago

I'm not questioning your motives, but we do get "homework questions" here fairly often and I just wanted to explain that.

In short, it would help to know the context.
---

Anyway, focusing on the code itself, it's a little hard to tell what's going on. Is this part of some larger loop? Otherwise I don't see how this repeats at all.

Your current while loop doesn't make very much sense to me. It will only run if you already have num==4, meaning you've already asked that. I'd imagine you want to remove that while loop entirely. Then use an if statement to display the result as long as #4 hasn't already been asked. Something like this--

Finally, you may have some trouble with variable scope here; it looks like you're creating a local variable, which should not be available on the next iteration-- yes_q4_asked will be re-created every time you call this function (I'm assuming this is in a function-- that's why it's hard to help without more code and context).

I've been doing chatbots for 13 years now. This bot is one of the best. It's proven it's worth in numerous contest. Having a good bot doesn't make me a good programmer though. Good bots are more about what they say than the programming behind them. So I'm not looking for a major rewrite or a different approach to building a bot. I just want to get this part working again. As I said it worked fine for many years. The actual bot is huge but here is a small demo that I use for testing. In short, if it works in the demo it will work in the main bot. Maybe that will help people see what the bot is.

In short if you type in "yes" it is going to reply with what you see in the example. However if message 4 is triggered I don't want it to trigger again. Only once...no more. How can I do that. I want the other "yes" replies to continue as is but I obviously don't want to keep asking the same question of "what color are your eyes" over and over.

Note: I didn't mention the main bot or a link for fear people might perceive this as some advertising ploy.

Note: I didn't mention the main bot or a link for fear people might perceive this as some advertising ploy.

We do have pretty strict rules about spam, but you're welcome to post a link if it's related to your question, because that's important in helping us answer it.

I've been doing chatbots for 13 years now. This bot is one of the best. It's proven it's worth in numerous contest. Having a good bot doesn't make me a good programmer though. Good bots are more about what they say than the programming behind them. So I'm not looking for a major rewrite or a different approach to building a bot. I just want to get this part working again. As I said it worked fine for many years. The actual bot is huge but here is a small demo that I use for testing. In short, if it works in the demo it will work in the main bot. Maybe that will help people see what the bot is.

Interesting. So your purpose is more about making this clever and interesting, rather than thinking about linguistic structure or models of language? That's one option (and it sounds like what I said above as option #1 for making a bot), and it does work-- it just requires a lot of definitions in your code to respond to lots of kinds of input. I don't mean to criticize you at all with my comments; I'm just trying to understand what you're after.

There are more efficient ways to write this, probably. But if you're happy with it, that's fine.

As for the specific issue, did you try to use the code I showed you above?

The trouble, I think, is going to be variable scope. When you define a variable within a function, it only exists within that function (and only in that instance of the function). To get around this, you'll need a global variable so that you can track it from iteration to iteration. You can do that by moving this line outside of the function:var yes_q4_asked="false";
Does that make sense?

Unfortunately I'm much better with PHP than Javascript, so I don't know if I can give you the specific answer to debug this code, but I wanted to reply because I'm interested in the subject matter.

Have you considered using a database? That might make things simpler. But that would be for a new project, rather than changing this one if it's mostly working.

Yes, I'm a part of the Chatterbox Challenge which is an annual chatbot contest. We started it in 2001 so this will be the 13 year now. It's not really a scientific contest as we all know the limits of AI. It's more of a place where people can show off their work. Virtually every bot of note including Smarter Child has entered it at some point.

Yes, as you can imagine the bot has hundreds of variables. They are all located outside the function. I still don't get why it stopped working.

Unfortunately I'm much better with PHP than Javascript, so I don't know

Okay, thanks for the help. I'll try to run across some javascript expert and see if I can get to the bottom of things.

So let me make sure I know what you're asking:
1. You want the bot to continuously respond to "yes" with one of those options, never stopping.
2. You want #4 to show up randomly, but only show up once.
3. After that, it should keep replying, with the other options, but just not #4 again.

Is that right? My other code was a misinterpretation, I think-- it was designed to have the bot stop replying entirely after #4 was asked once. But you just want to stop #4.

In that case, I think there's a simple typo, now that I see how all of this works. You have a == instead of =:yes_q4_asked=="true"; (line 50)
Change that to:yes_q4_asked="true";

Sometimes it's the little things. If that works, great! And sorry for apparently turning a simple answer into a complicated one, but it was hard to figure out without context.