<p>I'm sorry to say that probably there isn't a simple and easy way to go with something like this:</p>

<p>Since you're dealing with a database I will assume you have a wide range of possible questions, and that using a simple synonym table will not do</p>

<h1><a href="https://en.wikipedia.org/wiki/Natural_language_processing" rel="nofollow noreferrer">Natural Language Processing</a> (NLP)</h1>

<p>This is a very active research topic in Machine Learning and, in a nutshell, deals with automatically making sense from text. For your particular scenario. To get some intuition about it, and because it applies perfectly to your question, I would recommend starting with: <a href="https://ieeexplore.ieee.org/abstract/document/4438554/" rel="nofollow noreferrer">Question Similarity Calculation for FAQ Answering</a> by Song et al. (2007)</p>

<p>For a state-of-the-art tool that will help with your application, I suggest <a href="https://arxiv.org/abs/1301.3781" rel="nofollow noreferrer">word2vec</a> (that's the paper, but you might also want to follow a <a href="http://kavita-ganesan.com/gensim-word2vec-tutorial-starter-code/" rel="nofollow noreferrer">tutorial</a>.</p>

<h1>Other options</h1>

<p>If NLP looks more complex than what you're aiming for, I would suggest looking at word similarities, such as: </p>

<ul>
<li><a href="https://en.wikipedia.org/wiki/Levenshtein_distance" rel="nofollow noreferrer">Levenshtein distance</a></li>
<li><a href="https://en.wikipedia.org/wiki/Hamming_distance" rel="nofollow noreferrer">Hamming distance</a></li>
</ul>

<p>These, however, will not perform as well as a well trained NLP system. </p>
Python: given a question, find one that is similar in database
<p>I am writing a Telegram bot that answers to people's questions about a specific city. I wanted to write a piece of code that compares the message with the questions I have in my sqlite database table.</p>

<p>The biggest problem is that I cannot use <code>difflib.get_close_matches</code>, because <strong>the questions are not in English</strong> and at the moment I'm only handling exactly matching strings, for example:</p>

<pre><code>if msg.lower() == "what can you do?":
    send_message("I can answer to any question you have about...", chat_id)
</code></pre>

<p>And that's definitely NOT my aim</p>

<p>So, let's get to the code: I'm using this function to get the last message</p>

<pre><code>URL = "https://api.telegram.org/bot{}/".format(TOKEN)

def get_updates(offset=None):
url = URL + "getUpdates"
if offset:
    url += "?offset={}".format(offset)
js = get_json_from_url(url)
return js
</code></pre>

<p>and assign the returned value to the variable <code>updates</code>, as a result I will have the message text in <code>update["message"]["text"]</code></p>

<p>Now the difficult part, I would have to compare the string to the db records, then, if there is not any similar match, I will have to find synonyms of the words in the message and re-compare them to the records. </p>

<p>BUT this would make the program run awfully slow and I don't really have time nor will to make a list of synonyms for every possible word</p>

<p>Can anybody help me find the way to make a comparison and find a similar string in the db keeping the program as fast as possible?</p>