Skip to content
Snippets Groups Projects
Commit db01aeb4 authored by Piero Toffanin's avatar Piero Toffanin
Browse files

Only choose from languages available in argos

parent 41e381ae
No related branches found
No related tags found
No related merge requests found
......@@ -22,8 +22,7 @@ const res = await fetch("https://libretranslate.com/translate", {
source: "en",
target: "es"
}),
headers: {
"Content-Type": "application/json"}
headers: { "Content-Type": "application/json" }
});
console.log(await res.json());
......
......@@ -2,6 +2,8 @@ from flask import Flask, render_template, jsonify, request, abort, send_from_dir
from flask_swagger import swagger
from flask_swagger_ui import get_swaggerui_blueprint
from langdetect import detect_langs
from langdetect import DetectorFactory
DetectorFactory.seed = 0 # deterministic
def get_remote_address():
if request.headers.getlist("X-Forwarded-For"):
......@@ -18,6 +20,11 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_la
from app.language import languages
app = Flask(__name__)
# For faster access
language_map = {}
for l in languages:
language_map[l.code] = l.name
if debug:
app.config['TEMPLATES_AUTO_RELOAD'] = True
......@@ -197,11 +204,14 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_la
q = q[:char_limit]
if source_lang == 'auto':
candidate_langs = detect_langs(q)
candidate_langs = list(filter(lambda l: l.lang in language_map, detect_langs(q)))
if len(candidate_langs) > 0:
candidate_langs.sort(key=lambda l: l.prob, reverse=True)
if debug:
print(candidate_langs)
source_lang = next(iter([l.code for l in languages if l.code == candidate_langs[0].lang]), None)
if not source_lang:
source_lang = 'en'
......
......@@ -345,8 +345,7 @@ document.addEventListener('DOMContentLoaded', function(){
' source: "' + this.$options.filters.escape(this.sourceLang) + '",',
' target: "' + this.$options.filters.escape(this.targetLang) + '"',
' }),',
' headers: {',
' "Content-Type": "application/json"}',
' headers: { "Content-Type": "application/json" }',
' });',
'',
'console.log(await res.json());'].join("\n");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment