Conversion_Kitchen_Code/kitchen_counter/conversion/translation/multiprocessing_funcs2.py

52 lines
1.7 KiB
Python
Executable File

from google.cloud import translate_v2 as Translate
import os
from .script_detector import script_cat
from requests.exceptions import SSLError
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = rf"/home/ubuntu/mnf/MNF/MNF/json_keys/authentication.json"
translate_client = Translate.Client()
# client = translate.TranslationServiceClient()
def script_det(text):
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
no_punct = ""
for char in text:
if char not in punctuations:
no_punct = char
break
script = script_cat(no_punct)[0]
return script
def language_detector(text):
lang_detected = []
print(text,"sentence recieved")
#primary language detector
try:
result = translate_client.detect_language(text)
print("length re:",len(result['language']))
if len(result['language']) > 3:
#print((str(result['language']).split("-"))[0])
return (str(result['language']).split("-"))[0]
#lang_detected.append((str(result['language']).split("-"))[0])
else:
#lang_detected.append(result['language'])
return result['language']
except SSLError:
language_detector(text)
def detecting_languages(line):
non_dial_src_lang = ""
non_dial_src_script = ""
dial_src_lang = ""
dial_src_script = ""
if line[3] == "action":
non_dial_src_lang = language_detector(line[2])
non_dial_src_script = script_det(line[2])
elif line[3] == "dialogue":
dial_src_lang = language_detector(line[2])
dial_src_script = script_det(line[2])
return [non_dial_src_lang, non_dial_src_script, dial_src_lang, dial_src_script]