Python Forum

Full Version: Translation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I don't understand why I have the result in English. I need to translate it in Russian
from deep_translator import GoogleTranslator
import re
from gensim.models import KeyedVectors
def translation(qwery):
    if bool(re.search(r'[а-яёА-ЯЁ]+', qwery)):
        #print('Отправил вопрос на перевод')
        qwery_en = GoogleTranslator(source='ru', target='en').translate(qwery).replace(' ', '_')
        print(qwery_en)
    else:
        qwery_en = qwery.replace(' ', '_')
    return qwery_en

# Loading Word2Vec model
model = KeyedVectors.load('word2vec-google-news-300.model')

# Loading word list
with open('unique_tags.txt', 'r') as f:
    word_list = f.read().splitlines()

def similar(topic, model, word_list):
    try:
        # Selecting the most similar words from the list based on the topic
        most_similar = []
        for word in word_list:
            if word in model.key_to_index:
                similarity = model.similarity(word, topic)
                most_similar.append((word, similarity))
        most_similar = sorted(most_similar, key=lambda x: x[1], reverse=True)
        # Output with coefficients
        for w in most_similar:
            if w[1] > 0.3:
                print(w[0], w[1])
        # Creating a list of words
        most_similar = [w[0] for w in most_similar]
        print(most_similar)

# Translation into Russian
        from googletrans import Translator
        translator = Translator()
        most_similar_ru = []
        for word in most_similar:
            most_similar_ru.append(translator.translate(word, dest='ru').text)

        print(most_similar_ru)
    except Exception as error:
        print(error)
while True:
    # The topic to which we want to relate the words
    qwery = input("Введите запрос (E as Exit): ")
    topic = translation(qwery)
    similar(topic, model, word_list)
    if qwery == 'E':
        break
Note that the the translator will only be called if your regular expression returns true.
Is this what you expect?

Also, if you find it necessary, you can find the authors contact info here
Are the English words printed once or twice?