File size: 2,404 Bytes
9fa4f9e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# MTUOC_Marian
# Copyright (C) 2023 Antoni Oliver
# v. 07/06/2023
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import websocket
import socket
import time
import sys
import config
from MTUOC_misc import printLOG
def connect_to_Marian():
print("CONNECT TO MARIAN.")
from websocket import create_connection
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
conn=s.connect_ex((config.MTEngineIP, config.MTEnginePort))
if conn == 0:marianstarted=True
service="ws://"+config.MTEngineIP+":"+str(config.MTEnginePort)+"/translate"
error=True
while error:
try:
config.ws = create_connection(service)
printLOG(1,"Connection with Marian Server created","")
error=False
except:
printLOG(1,"Error: waiting for Marian server to start. Retrying in 2 seconds.",sys.exc_info())
time.sleep(2)
error=True
def translate_segment_Marian(segmentPre):
translation_candidates={}
translation_candidates["segmentNOTAGSPre"]=segmentPre
lseg=len(segmentPre)
try:
config.ws.send(segmentPre)
except:
printLOG(1,"Error sending segment to Marian.",sys.exc_info())
translations = config.ws.recv()
tc_aux=translations.split("\n")
translation_candidates["translationNOTAGSPre"]=[]
translation_candidates["alignments"]=[]
for tca in tc_aux:
printLOG(3,"TCA:",tca)
try:
segmentaux=tca.split(" ||| ")[1].strip()
alignmentaux=tca.split(" ||| ")[2].strip()
translation_candidates["translationNOTAGSPre"].append(segmentaux)
translation_candidates["alignments"].append(alignmentaux)
except:
pass
return(translation_candidates) |