Spaces:
Sleeping
Sleeping
| 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) | |
| 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) | |
| 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}) | |
| 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) | |
| def index(): | |
| return send_from_directory(".", "index.html") | |
| 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) | |