Spaces:
Running
Running
File size: 4,564 Bytes
e5fcfe9 |
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
import json, random, os
from flask import Flask, jsonify, request, send_from_directory
app = Flask(__name__)
DATA_FILE = "sum.json" # 全フレーズの JSON ファイル
WEAK_FILE = "weak_phrases.json" # 苦手フレーズ用の JSON ファイル
def load_json(file_path):
if os.path.exists(file_path):
with open(file_path, "r", encoding="utf-8") as f:
return json.load(f)
else:
return {"phrases": []}
def save_json(file_path, data):
with open(file_path, "w", encoding="utf-8") as f:
json.dump(data, f, indent=4, ensure_ascii=False)
def get_all_phrases():
data = load_json(DATA_FILE)
phrases = []
for category in data["phrases"]:
for phrase in category["phrases"]:
phrases.append({
"category": category.get("category_english", ""),
"category_japanese": category.get("category_japanese", ""),
"phrase": phrase.get("phrase", ""),
"description": phrase.get("description", ""),
"example": phrase.get("example", "")
})
return phrases
def get_phrases_by_category(category_name):
data = load_json(DATA_FILE)
for category in data["phrases"]:
if category.get("category_english", "").lower() == category_name.lower() or category.get("category_japanese", "") == category_name:
return category["phrases"]
return []
def get_thanks_phrases():
data = load_json(DATA_FILE)
phrases = []
for category in data["phrases"]:
# この組み合わせのみ対象
if category.get("category_english") == "Thanks for..." and category.get("category_japanese") == "〜に感謝する":
for phrase in category["phrases"]:
phrases.append({
"category": category.get("category_english", ""),
"category_japanese": category.get("category_japanese", ""),
"phrase": phrase.get("phrase", ""),
"description": phrase.get("description", ""),
"example": phrase.get("example", "")
})
return phrases
def get_random_phrase(phrases):
if not phrases:
return None
return random.choice(phrases)
@app.route("/phrases", methods=["GET"])
def phrases():
mode = request.args.get("mode", "random")
if mode == "weak":
weak_data = load_json(WEAK_FILE)
phrase_list = weak_data.get("phrases", [])
elif mode == "category":
category = request.args.get("category", "")
phrase_list = get_phrases_by_category(category)
elif mode == "thanks":
phrase_list = get_thanks_phrases()
else:
phrase_list = get_all_phrases()
return jsonify(phrase_list)
@app.route("/add_weak", methods=["POST"])
def add_weak():
data = request.get_json()
phrase_text = data.get("phrase")
if not phrase_text:
return jsonify({"error": "フレーズが入力されていません"}), 400
all_phrases = get_all_phrases()
phrase_obj = next((p for p in all_phrases if p["phrase"] == phrase_text), None)
if not phrase_obj:
return jsonify({"error": "指定されたフレーズが見つかりません"}), 404
weak_data = load_json(WEAK_FILE)
if "phrases" not in weak_data:
weak_data["phrases"] = []
if phrase_obj not in weak_data["phrases"]:
weak_data["phrases"].append(phrase_obj)
save_json(WEAK_FILE, weak_data)
return jsonify({"message": "苦手フレーズとして追加しました", "phrase": phrase_obj})
else:
return jsonify({"message": "既に苦手フレーズとして登録済みです", "phrase": phrase_obj})
@app.route("/categories", methods=["GET"])
def categories():
data = load_json(DATA_FILE)
cats = []
for category in data["phrases"]:
cats.append({
"english": category.get("category_english", ""),
"japanese": category.get("category_japanese", "")
})
return jsonify(cats)
@app.route("/")
def index():
return send_from_directory(".", "index.html")
@app.route("/<path:filename>")
def static_files(filename):
return send_from_directory(".", filename)
if __name__ == "__main__":
# Hugging Face Spaces では PORT 環境変数が指定されるので、それを利用
port = int(os.environ.get("PORT", 7860))
app.run(debug=True, host="0.0.0.0", port=port)
|