def arch_doc(lengua, ruta, session=None, user=None, proc=False): """Crear un documento del contenido de un archivo, solo para análisis.""" l = cargar(lengua) session = session or iwqet.start(l, None, user) arch = open(ruta, encoding='utf8') texto = arch.read() d = iwqet.Document(l, None, texto, proc=proc, session=session) return d
def init_session(): global SESSION global AMH global ENG if not ENG: load_languages() # Load users and create session if there's a user if USER and not SESSION: SESSION = start(ENG, AMH, USER)
def ሰነድ(ቋንቋ, ዱካ, session=None, ተጠቃሚ=None, proc=False): """ Create a document from the content of a file, only for analysis. """ l = load1(ቋንቋ) session = session or iwqet.start(l, None, ተጠቃሚ) arch = open(ዱካ, encoding='utf8') ጽሁፍ = arch.read() d = iwqet.Document(l, None, ጽሁፍ, proc=proc, session=session) return d
def sentence(sentence, ambig=False, solve=True, user=None, segment=True, max_sols=1, verbosity=0): a, g = load() gui = iwqet.gui.GUI() gui.source = a gui.target = g iwqet.start(gui, user) d = iwqet.Document(a, g, sentence, True) s = d[0] s.initialize(ambig=ambig, verbosity=verbosity) if solve or segment: s.solve(all_sols=ambig or max_sols > 1, max_sols=max_sols) if s.solutions and segment: solution = s.solutions[0] solution.get_segs() output_sols(s) return s
def sentence(sentence, ambig=False, solve=True, user=None, segment=True, max_sols=1, verbosity=0): e, a = load() session = iwqet.start(e, a, user) d = iwqet.Document(e, a, sentence, True, session=session) s = d[0] s.initialize(ambig=ambig, verbosity=verbosity) if solve or segment: s.solve(all_sols=ambig or max_sols > 1, max_sols=max_sols) if s.solutions and segment: solution = s.solutions[0] solution.get_segs() output_sols(s) return s
def tra(): global GUI if not GUI: create_gui() form = request.form # print("Form {}".format(form)) if not GUI.source: # print("Ninguna lengua fuente") start(gui=GUI, use_anon=False, create_memory=False) # Translating document? isdoc = form.get('isdoc') == "true" or form.get('modo') == 'doc' DB = form.get('docsrc') == 'DB' if isdoc: print("TRANSLATING DOCUMENT") if DB: print(" Selecting from DB") else: print("TRANSLATING SENTENCE") # Initialize various window parameters GUI.set_props(form, ['hide', 'nocorr'], ['tfuente']) GUI.props['isdoc'] = isdoc # We may need to clear the doc if we've changed mode if isdoc and 'documento' in form and not form['documento']: GUI.doc = None # Pick translation without offering options username = GUI.user.username if GUI.user else '' tradtodo = form.get('tradtodo') == 'true' abandonar_doc = form.get('abandonardoc') == 'true' if abandonar_doc: # print("Abandonando actual documento...") GUI.clear(isdoc=True, abandonar=True) if 'ayuda' in form and form['ayuda'] == 'true': # Opened help window. Keep everything else as is. return render_template('tra.html', doc=isdoc, documento=GUI.doc_html, props=GUI.props, user=username, choose=False, tradtodo=tradtodo) # No document loaded from file or from DB no_doc = isdoc and not GUI.doc and not GUI.doc_html # No sentence entered in sentence UI no_sent = not isdoc and not 'ofuente' in form # print("nodoc {}, nosent {}".format(no_doc, no_sent)) if no_doc or no_sent and 'modo' in form and form['modo']: # print("modo in form: {}".format(form.get('modo'))) # Mode (sentence vs. document) has changed # isdoc = form.get('modo') == 'doc' if 'documento' in form and form['documento']: # A document has been loaded, make it into a Document object make_document(GUI, form['documento'], html=True) # print("PROCESANDO TEXTO EN ARCHIVO {}".format(GUI.doc)) # Re-render, using HTML for Document return render_template('tra.html', documento=GUI.doc.html, doc=True, props=GUI.props, user=username, text_html=GUI.text_select_html, choose=False, tradtodo=tradtodo) elif isdoc: # and form.get('docsrc') == 'almacén': # A Text object is to be loaded; create the text list and HTML if DB: print("Selecting from DB") # Re-render, displaying domain/text dropdowns print("TEXT HTML {}".format(GUI.text_select_html)) return render_template('tra.html', doc=True, props=GUI.props, text_html=GUI.text_select_html, DB=True, user=username, choose=False, tradtodo=tradtodo) textid = form.get('textid', '') if textid: # print("Making text doc from text {}".format(textid)) make_text(GUI, int(textid)) return render_template('tra.html', documento=GUI.doc_html, doc=True, props=GUI.props, user=username, text_html=GUI.text_select_html, choose=False, tradtodo=tradtodo) print("SOMETHING WRONG WITH DOC IF") # else: # # Re-render, displaying domain/text dropdowns ## print("DISPLAY DOMAINS") # return render_template('tra.html', doc=True, props=GUI.props, # text_html=GUI.text_select_html, # user=username, choose=False, # tradtodo=tradtodo) else: # Clear the GUI and re-render, keeping the mode setting GUI.clear(isdoc=isdoc) return render_template('tra.html', doc=isdoc, props=GUI.props, user=username, choose=False, tradtodo=tradtodo, text_html=GUI.text_select_html) if form.get('erase') == 'true': # print("Clearing text, isdoc? {}, tradtodo {}".format(isdoc, tradtodo)) record = form.get('registrar') == 'true' if record: trans = form.get('ometa') # print("Recording translation: {}".format(trans)) GUI.clear(record=True, translation=trans, isdoc=True) else: GUI.clear(record=False, isdoc=isdoc, tradtodo=tradtodo) #, form.get('ometa')) # Start over with no current sentence or translation (translating sentence) GUI.props['tfuente'] = "115%" return render_template('tra.html', oracion=None, tra_seg_html=None, user=username, props=GUI.props, documento=None, text_html=GUI.text_select_html, doc=isdoc, choose=False, tradtodo=tradtodo) GUI.props['isdoc'] = isdoc if not 'ofuente' in form: # No sentence entered or selected # print("NO SENTENCE ENTERED") return render_template('tra.html', user=username, props=GUI.props, doc=isdoc, choose=False, tradtodo=tradtodo) if not GUI.doc and not GUI.has_text: # Create a new document make_document(GUI, form['ofuente'], html=False) if len(GUI.doc) == 0: # print(" pero documento está vacío.") return render_template('tra.html', error=False, user=username, doc=isdoc, props=GUI.props, text_html=GUI.text_select_html, choose=False, tradtodo=tradtodo) oindex = int(form.get('oindex', 0)) # print("oindex {}".format(oindex)) docscrolltop = form.get('docscrolltop', 0) if 'tacept' in form and form['tacept']: # A new translation to be added to the accepted sentence translations. # print("ACCEPTING NEW TRANSLATION {}".format(form['tacept'])) GUI.accept_sent(oindex, form['tacept']) aceptado = GUI.doc_tra_acep_str return render_template('tra.html', oracion='', doc=True, tra_seg_html='', tra='', oindex=-1, documento=GUI.doc_html, aceptado=aceptado, user=username, props=GUI.props, choose=False, tradtodo=tradtodo) if GUI.doc_tra_acep and GUI.doc_tra_acep[oindex]: error = "¡Ya aceptaste una traducción para esta oración; por favor seleccioná otra oración para traducir!" return render_template('tra.html', oracion='', doc=True, error=error, tra_seg_html='', tra='', aceptado=GUI.doc_tra_acep_str, docscrolltop=docscrolltop, documento=GUI.doc_html, user=username, props=GUI.props, choose=False, tradtodo=tradtodo) if GUI.doc_tra_html and GUI.doc_tra_html[oindex]: # Find the previously generated translation tra_seg_html = GUI.doc_tra_html[oindex] tra = GUI.doc_tra[oindex] # print("Looking for previous translation... (oindex={}, isdoc={}, tra {})".format(oindex, isdoc, tra)) # Highlight selected source sentence with segments GUI.update_doc(oindex, repeat=True) # print("SENTENCE at {} ALREADY TRANSLATED".format(oindex)) return render_template('tra.html', oracion=GUI.fue_seg_html, tra_seg_html=tra_seg_html, tra=tra, documento=GUI.doc_html, doc=True, oindex=oindex, docscrolltop=docscrolltop, aceptado=GUI.doc_tra_acep_str, user=username, props=GUI.props, choose=False, tradtodo=tradtodo) # Here's where a sentence or a whole document gets translated if tradtodo: print("TRANSLATING THE WHOLE DOCUMENT: {}".format(GUI.doc)) # sentences = doc_sentences(doc=GUI.doc, textid=GUI.textid, gui=GUI) all_trans = trad_doc() doctrans = '\n'.join(all_trans) print("Translations: {}".format(doctrans[:100])) GUI.props['tfuente'] = '100%' # translations = '' # for sentence in sentences: # print("Translating sentence {}".format(sentence)) # translation = gui_trans(GUI, choose=False, return_string=True, sentence=sentence) # translations += translations + "\n" + translation return render_template('tra.html', documento=GUI.doc_html, doc=True, aceptado=doctrans, docscrolltop=docscrolltop, choose=False, user=username, props=GUI.props, tradtodo=True) else: # Get the sentence, the only one in GUI.doc if isdoc is False. if GUI.has_text and GUI.textid >= 0: # Make the sentence from the TextSeg object GUI.sentence = sentence_from_textseg(source=GUI.source, target=GUI.target, textid=GUI.textid, oindex=oindex) else: GUI.sentence = GUI.doc[oindex] print("CURRENT SENTENCE {}".format(GUI.sentence)) # Translate and segment the sentence, assigning GUI.segs source = form.get('ofuente', '') solve(isdoc=isdoc, index=oindex, choose=False, source=source) # print(GUI.tra_seg_html) oracion = GUI.fue_seg_html return render_template('tra.html', oracion=oracion, tra_seg_html=GUI.tra_seg_html, tra=GUI.tra, documento=GUI.doc_html, doc=isdoc, oindex=oindex, aceptado=GUI.doc_tra_acep_str, docscrolltop=docscrolltop, choose=False, user=username, props=GUI.props, tradtodo=False)