r/programminghelp • • Aug 24 '16

PROLOG CHATBOT

Hello, I'm planning to make a chatbot using prolog. I know a bit about prolog, but I have a hard time coding on a chatter. Do you have any sample? Anything is a big help. Thanks in advance.

4 Upvotes

1 comment sorted by

View all comments

1

u/zmonx Aug 24 '16

I have one tip: You can regard a chatbot as a sequence of state transitions:

State0 -> State1 -> ... -> State

In a case of a chat bot, the state can for example describe which questions and answers have already taken place, i.e., the state is simply the history of the interaction.

So, in this concrete case, you can for example represent a single such state as a list of elements of the form:

  • question(Q)

  • answer(A)

So you have for example:

[question('Hello, what is your name?'), answer('My name is ...'), question('And where are you from?'), answer('...')]

Now the point: You should focus on describing the relation between two successive states of such an interaction. Begin with a Prolog predicate that is called for example:

state0_action_state(State0, Action State) :-

This is a relation between:

  • the current state State0

  • an Actionthat needs to be performed next

  • the resulting new state State.

For example, one of the clauses may be:

state0([], Q, [Q]) :-
        Q = question('Hello, what is your name?').

This describes what the chatbot will do if no history at all is currently present, i.e., the list that represents the history of states is empty.

Next, we make the rules depend on what occurred in the immediately preceding state. For example, if it was a question by the chatbot, then the next action must be to query the user. There are many ways to do this, for example:

state0([question(Q)|States], query_user, [question(Q)|States]).

If the previous state was a user answer, then we need to formulate a new question:

state0([answer(A)|States], question(Q), [question(Q),answer(A)|States]) :-
        here comes the tough part: make the bot formulate a question!

Now the point: Once you have such a declarative description of state transitions, you can define the actual interaction simply as a sequence of such transitions:

interaction(S0) :-
        state0_action_state(S0, Action, S),
        % perform the action Action:
        ... (left as an exercise)
        % and then continue with the new state S:
        interaction(S).

Where you start with ?- interaction([]).. I hope this gets you started. Good luck with your project!