Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Translation
#1
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
Reply
#2
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
Reply
#3
Are the English words printed once or twice?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Translation API snippyro 0 1,260 Nov-02-2021, 01:09 AM
Last Post: snippyro
  [GoogleTrans] How can i print my translation word ?... JamieVanCadsand 7 11,748 Aug-29-2021, 12:01 PM
Last Post: Melcu54
  Translation of R Code to Python for Statistical Learning Course SterlingAesir 2 2,142 Aug-27-2020, 08:46 AM
Last Post: ndc85430
  recording a translation table into a file arbiel 0 1,457 Mar-31-2020, 02:33 PM
Last Post: arbiel
  Manipulation a file for translation apsyrtos 1 2,039 Sep-10-2019, 02:56 AM
Last Post: luoheng
  Scilab -> Python translation silverfish 0 5,092 Mar-23-2018, 07:35 AM
Last Post: silverfish
  Python2 to Python3 translation - .keys() 20salmon 1 3,533 Nov-03-2017, 10:41 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020