Skip to content

Short Python script that initializes a REPL for translating from English to Russian and vice versa.

Notifications You must be signed in to change notification settings

Avtomatovich/Perevod

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

Perevod

Perevod is a short Python script (one of my first) which offers a simple REPL for translating text from English to Russian and vice versa. I wrote this when I was first teaching myself Python. The script can detect whether you are writing in Russian by detecting the use of Cyrillic in the input. It checks if the input is English by checking for the absence of Cyrillic letters. This means that it cannot tell between other languages that use the standard Latin alphabet. It then sends the text to a server which returns the translated text. Here is the code:

import sys
from translate import Translator

class Perevod:
        
    def _is_cyrillic(self, text):
        cyrillic_unicode = range(0x0400, 0x04FF)
        
        for char in text:
            if ord(char) in cyrillic_unicode:
                return True
            
    def convert(self, text):
        if self._is_cyrillic(text):
            translator = Translator(from_lang="ru", to_lang="en")
        else:
            translator = Translator(from_lang="en", to_lang="ru")

        return translator.translate(text)
        
def main():
    if len(sys.argv) != 2:
        print("Usage: python perevod.py <text>")
        return
    
    perevod = Perevod()
    text = sys.argv[1]
    response = perevod.convert(text)
    print(text + ":\n\t->" + response)
        
if __name__ == "__main__":
    perevod = Perevod()
    while True:
        text = input('>> ')
        if text != 'exit':
            print(perevod.convert(text))
        else:
            break

About

Short Python script that initializes a REPL for translating from English to Russian and vice versa.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages