fastionary/fastionary/app.py
2026-01-15 00:17:39 +01:00

50 lines
1.5 KiB
Python

from flask import Flask
from flask import request
from flask import render_template
import json
#import os
app = Flask(__name__)
#def get_dictionary(filename='fastionary/enfr.json'):
# print (filename)
# if os.path.exists(filename):
# with open (filename, 'r') as json_file:
# dictionary = json.load(json_file)
# else:
# dictionary = {}
# # what happens if dictionary not found?
# # return dummy dictionary?
# # display that no dictionary file was found
# # where should this be handled
# return dictionary
#
#def get_translations(word):
# dictionary=get_dictionary()
# translation = None
# if word != None or word != '':
# translation = dictionary.get(word)
# return translation
def get_translations(word, file_path='fastionary/data/detw.jsonl'):
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
entry = json.loads(line)
if entry.get('word') == word:
if 'translations' in entry and entry['translations']:
for sense in entry['translations']:
if sense['lang_code'] == 'tw':
return sense['word']
# return entry['translations'][0]['word']
return None
@app.route('/')
def trans(q=None):
query = request.args.get('q', None)
translation = get_translations(query)
return render_template('trans.html', q=query, t=translation)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)