Bot philosopher for vk.com

Based on many articles that were devoted to the botovodam ...



Recently I developed “turnkey” one interesting project dedicated to vk.com social network. The task is simple - to create a bot that can be added to the conversation and which will give random quotes. But it seemed too simple, so I had to revive the great philosopher Friedrich Nietzsche, who would make a bot out of a bot - super bot ...



image



I did not see anything complicated in this, so a script of this kind was quickly written:

Authorization -> reading messages -> analysis and response formation -> response

All this was done using the Callback API VK v.5.80 ( nothing new )

The most difficult point was “analysis and response formation”. Indeed, this is the most important thing.

But the fact is that I wanted to create a “less intelligent bot” than that which would simply issue a random phrase from the dictionary.



“How did it work out for me and did it work out?” - please read ...



So, let's begin!



In principle, the bot has a fairly simple functionality, but it is simple only superficially. If you go deeper, then everything is much more complicated, because we will write in PHP ( )



I repeat that I don’t just need a quote generator, so I decided to ask the bot some logic. However, I also don’t need a neural network, because learning is a daunting task.



I solved the problem with creativity when I tried to revive the great philosopher of the XIX century - Friedrich Nietzsche.



Yes, it is Nietzsche who will be in the role of the bot, and therefore even random quotes will seem smarter.

But I wanted to somehow unite the interlocutor and the bot, to create a connection between them ...



So, the interlocutor’s phrase was taken as a basis, which was transformed by filters into keywords, according to which from a previously prepared base there were sentences.



Thus, as if the answer to the question of the questioner was formed.



I received a new message from the user like this:



//  case 'message_new': //... id   $user_id = $data->object->peer_id; //   $body = $data->object->text;
      
      





And then he brought the message in a suitable form to highlight the keywords and feed them into a database search.



Firstly, I completely got rid of the endings " a, and, s, am, yami, ah, oh ... " and so on ... I removed punctuation marks and other symbols.



Secondly, I did not take into account prepositions. First, I limited the words to their length - up to 3 letters. But this was wrong, because Nietzsche’s philosophies were characterized by such words as: “ god, world, rock, century, etc. ”. In addition, after filtering words of four letters, there were words of three letters, because the endings were discarded. Then I just limited my search to such words as: “ over, what, about, how, where, etc. ”.



Thirdly, Nietzsche wrote more in his books on his own, so he replaced incoming words like " yours, yours, you, love, you can" with "mine, mine, I love, I can, " respectively.



Thus, a kind of dialogue is obtained.



Next - it was necessary to make a base, a dictionary of phrases / quotes / statements from fragments of Nietzsche’s books.



I downloaded Nietzsche’s books in txt format from liters. Gathered them together, and then filtered:



1 quote = 1 sentence ( used the delimiter as ".", "! ',"? " )



the base turned out to be something like this:



- , , -

, , , ,

, , : ,

,








Well, there is a database and a filtered message from the user.



: ", ?"

: " "








It remains to fasten the search system. It was difficult with this, because different settings gave different results. And if one code gave the correct answer, then the other request did not give what was needed.



It was decided not to use regular expressions, but rather to use them together with built-in functions like:



substr

stripos

substr_count









and other text features ...



Using long comparison conditions and infinitely long cycles, I achieved approximately the desired result.



The search logic is something like this: go through each sentence and find the words from the incoming message that are present in this sentence.



If the word is found, then +1 is added as the "weight of the variable."



Therefore, the more words are found in the sentence, the greater is the “weight of the variable”.



This suggests that if the words "morality and life" were found in some sentence from the database, then this is a 100% answer that will be issued.



But if the words were on the topic and produced a lot of 100% of the results, then the random () method was issued by the rand () method.



In addition, these were not always small sentences, so I had to trim the answer, roughly by such regulars:



  preg_match('/(?:^|\.\s+)([^\.]*?'.$wordpattern.'[^\.]*?\.+)\s+/i', $text , $matches);
      
      





Then everything is automatic:

sending message



 $request_params = array( 'message' => $matches[1], #'message' => $find[0], 'peer_id' => $user_id, 'access_token' => $token, 'v' => '5.80' ); $get_params = http_build_query($request_params); file_get_contents('https://api.vk.com/method/messages.send?'. $get_params); header("HTTP/1.1 200 OK"); echo('ok');
      
      





I specifically did not upload all the code, but it is relatively small. The goal was to create a response engine on demand, rather than artificially trained intelligence.



You can test the bot at this link . Just write him a message. It is important to know that at the beginning of the sentence you need to put “Nietzsche”, as if referring to it, only then will the bot respond.



This is a test version, and therefore incomplete, therefore, mats, youth slang, mistakes in words, etc. - the bot does not take into account and the base is only 10,000 phrases.



As examples, I can give the dialogs:



image



image



image



image



image



image



Conclusion: Yes, the bot does not have two-way communication with the user - it is not artificial intelligence. But in a conversation from a large number of participants, the bot will become a good "interlocutor."



All Articles