54 lines
1.8 KiB
Python
54 lines
1.8 KiB
Python
|
from google.cloud import translate_v2 as Translate
|
||
|
import os
|
||
|
from .script_detector import script_cat
|
||
|
from requests.exceptions import SSLError
|
||
|
from MNF.settings import BasePath
|
||
|
basePath = BasePath()
|
||
|
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = rf"{basePath}/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]
|