Introduction
Many people believe that the programming language prolog is a purely scientific and outdated language for teaching students. The majority at the university “strained” with this language on subjects, at least a little close to the topic of artificial intelligence, this topic did not bypass me.
The standard task I came across turned out to be so boring and common for this language that I was tempted to download it for free without registration, but I decided to take the most out of this course and turn the subject of the prologue to write something practically meaningful and interesting. After reading quite a lot of different documentation, including posts on the prolog in Habré, I came to the conclusion that the prolog is an excellent tool for processing sentences of a formal language. I decided that it would be cool to write a bot that would know how to respond to phrases (maybe even smoothly) on VKontakte.
Implementation
First, I wrote the main functionality of the bot, i.e. an interface that allows me, as an admin, to send commands to execute the bot.
List of current commands:
$commands = array('', '', 'up', 'uptime', '', 'last');
')
Actually, each team speaks for itself. It is necessary to stop on two teams:
- listen to% text% tells the bot to put the song into status, where the performer is the name =% text%. The case does not apply at all, just became curious.
- And last - displays the last message sent to the bot. And the user who sent it.
It looks like this:


All this was done as part of the processing of incoming messages. Further, the processing of messages is not from the admin, i.e. suggestions that require a response.
Here comes the prolog. Or rather, swi-prolog, the implementation of which is available for both windows (it was developed there) and * nix (it works there now). Plus swi-prolog is that it is interpretable. Therefore, you just need to feed the script file to the swi interpreter and catch the result. At the same time, remember the input phrase, and with the resulting phrase - write this bundle to a file. A sheet of masks is best presented by example for the input line:
, ?
the list of masks will look like:
['**','*','*','**','*','*','**','*','*','**','*','*']
In this case, we must remember that this is exactly the question. For this we enclose the list of masks with the predicate request, and save it to a file. Example:
request('?',['**','*','*','**','*','*','**','*','*','**','*','*']).
Next, run the script, the description of the algorithm of which is reduced to several lines:
- We read the predicate from the input file (the same request)
consult('request.txt')
And in order for the text in the file to be perceived as a set of predicates, you need to add a description of dynamic predicates somewhere in the title:
:-dynamic(request/2).
- We read the knowledge base and get a list of questions and answers and a list of unanswered questions.
:-dynamic(question/2). :-dynamic(answer/2). :-dynamic(notanswered/1). :-consult('questionanswerlink.txt'). :-consult('withoutanswer.txt').
- We are looking for a question / answer
goal2 :- toFile, consult('request.txt'), request(Type, KeyWords),% twoFindAllByType(Type, KeyWords, ActualByType), % , ( - , , ) KeyWords ( ). str(ActualByType, ActualPhrasesString),% toFile(ActualPhrasesString),fail.%
- if the answer is not found - take a random question from the list of questions (for which there is no answer)
noAnswer :- toFile, consult('request.txt'), % request(Type, _), % twoAsk(Type, Res),% str(Res, ActualPhrasesString),write(ActualPhrasesString), % toFile(S),fail.
- Write the result to another file (usually, the result is a few phrases)
Additional functions:
List in row
str([], _). % - str([H|[]], S):- % string_concat('', H, S),!. % (H) S ! str([H|_], String) :- % string_concat(H, '', S),% S String = S. % String
Saving to a data file
toFile(Data) :- open('output.txt', append, OS), toFile(OS, Data). toFile:- open('output.txt', write, OS), close(OS). toFile(OS, Data) :- write(OS,Data), nl(OS), false. toFile(OS, _) :- close(OS).
Well, getting unanswered questions:
twoAsk('.', Res):- % notanswered(R),Res = [R|Res]. % notanswered("- ") Res.
Actually, this is all what the prolog script does. The rest of the things are quite simple: we take a random line from the resulting file, and give it to the user, at the same time we write the question-answer to the BR file. Also, if a bot asked a question, and no answer was given - this question is recorded in the database of questions that have not been answered.
Result
This is probably the most expected part of the article. Even considering that this version of the bot was written in 3 days, taking into account the phrase output algorithm, the result may produce phrases depending on the context of the conversation (sometimes). But, especially, he answers, if there are unique \ under-used phrases (it is understandable).
The bot's algorithm is very crooked, especially the part that needs to be “smart.” By the way, in order to train a little bot, I had to go for some inconvenience. For example, he wrote a couple of times on behalf of a bot in groups where girls sell themselves or take pictures and sell photos. This had an effect, and, as was to be expected, distribution by gender: 98% men, 2% the rest. But it gave some negative effect. Bot began to write on behalf of both men and women. This confused users, and as a result, the knowledge base contains quite a lot of questions containing the words “gender”, “your”, “what” and the like. But even taking into account these inconveniences, people continued to communicate with the bot, even without looking at the high response speed (half a second - second, especially for familiar phrases), repetition of messages, replies at random. Some people just need to communicate, even if it is not natural.
And recently we even began to learn:

Sometimes the bot gave out philosophical phrases that you can think about looking at the sky for a very long time:

or not

Sometimes, however, skips absolute nonsense.

But even so, it’s fun to watch:
corrected in time:

the most interesting was:

In addition to the bot account, I created at the same time a public page to which screenshots of the most interesting dialogs were sent, and most of the dialogs can be read by writing “gob baba vk” in Google, strictly +18. There are already about 150 of them. By the way, if this article is read by the prolog guru, maybe they will teach me a couple of lessons, or you can, for example, work together to create a more intelligent work algorithm, of course, if this is interesting to someone.
Unfortunately, the current algorithm does not allow the bot to serve even 10 clients (very difficult), it happens that the processing process is interrupted for a couple of minutes, going through a large knowledge base with a large number of input masks. Therefore, a link to the bot itself can only be given to a couple of people and exclusively in the LS.
Thank you for reading up to this point.