Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,74 +1,90 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify
|
| 2 |
-
from flask_cors import CORS
|
| 3 |
-
import process_sanskrit as ps
|
| 4 |
-
import re
|
| 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 |
-
if not raw_results:
|
| 51 |
-
return jsonify([])
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
stem = entry[0]
|
| 57 |
-
word_type = entry[1]
|
| 58 |
-
grammar = entry[2]
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
app.run(debug=True)
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
import process_sanskrit as ps
|
| 4 |
+
import re
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
CORS(app)
|
| 9 |
+
|
| 10 |
+
def clean_definitions(content):
|
| 11 |
+
"""
|
| 12 |
+
Recursively flattens the dictionary data.
|
| 13 |
+
Normalizes whitespace but PRESERVES HTML tags.
|
| 14 |
+
"""
|
| 15 |
+
results = []
|
| 16 |
+
if isinstance(content, str):
|
| 17 |
+
# Normalize whitespace but do NOT strip <tags>
|
| 18 |
+
clean = re.sub(r'\s+', ' ', content).strip()
|
| 19 |
+
if clean:
|
| 20 |
+
results.append(clean)
|
| 21 |
+
elif isinstance(content, list):
|
| 22 |
+
for item in content:
|
| 23 |
+
results.extend(clean_definitions(item))
|
| 24 |
+
elif isinstance(content, dict):
|
| 25 |
+
for value in content.values():
|
| 26 |
+
results.extend(clean_definitions(value))
|
| 27 |
+
return results
|
| 28 |
+
|
| 29 |
+
@app.route('/split')
|
| 30 |
+
def split_word():
|
| 31 |
+
word = request.args.get('word')
|
| 32 |
+
if not word: return jsonify({"error": "No word"}), 400
|
| 33 |
+
try:
|
| 34 |
+
split_result = ps.split(word)
|
| 35 |
+
if split_result and isinstance(split_result[0], list):
|
| 36 |
+
components = split_result[0]
|
| 37 |
+
else:
|
| 38 |
+
components = split_result if split_result else [word]
|
| 39 |
+
components = [c for c in components if c]
|
| 40 |
+
is_compound = len(components) > 1
|
| 41 |
+
return jsonify({"is_compound": is_compound, "components": components})
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return jsonify({"is_compound": False, "components": [word]})
|
| 44 |
+
|
| 45 |
+
@app.route('/meaning')
|
| 46 |
+
def get_meaning():
|
| 47 |
+
word = request.args.get('word')
|
| 48 |
+
try:
|
| 49 |
+
raw_results = ps.process(word, 'mw', 'ap90', 'cae', 'bhs')
|
| 50 |
+
if not raw_results:
|
| 51 |
+
return jsonify([])
|
| 52 |
+
|
| 53 |
+
grouped_results = {}
|
| 54 |
+
|
| 55 |
+
for entry in raw_results:
|
| 56 |
+
stem = entry[0]
|
| 57 |
+
word_type = entry[1] if entry[1] else "morphology"
|
| 58 |
+
grammar = entry[2] if entry[2] else [["form recognized"]]
|
| 59 |
+
dict_data = entry[6]
|
| 60 |
+
|
| 61 |
+
formatted_defs = {}
|
| 62 |
+
for source, content in dict_data.items():
|
| 63 |
+
cleaned = clean_definitions(content)
|
| 64 |
+
if cleaned:
|
| 65 |
+
formatted_defs[source] = cleaned
|
| 66 |
+
|
| 67 |
+
def_key = json.dumps(formatted_defs, sort_keys=True)
|
| 68 |
+
|
| 69 |
+
if def_key in grouped_results:
|
| 70 |
+
existing = grouped_results[def_key]
|
| 71 |
+
for tag_set in grammar:
|
| 72 |
+
if tag_set not in existing["detected_tags"]:
|
| 73 |
+
existing["detected_tags"].append(tag_set)
|
| 74 |
+
if word_type not in existing["type"]:
|
| 75 |
+
existing["type"] += f" / {word_type}"
|
| 76 |
+
else:
|
| 77 |
+
grouped_results[def_key] = {
|
| 78 |
+
"stem": stem,
|
| 79 |
+
"type": word_type,
|
| 80 |
+
"detected_tags": grammar,
|
| 81 |
+
"definitions": formatted_defs
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
return jsonify(list(grouped_results.values()))
|
| 85 |
+
except Exception as e:
|
| 86 |
+
print(f"Meaning error: {e}")
|
| 87 |
+
return jsonify({"error": str(e)}), 500
|
| 88 |
+
|
| 89 |
+
if __name__ == '__main__':
|
| 90 |
app.run(debug=True)
|