File size: 3,027 Bytes
6a1a740
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61a4cfd
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
from flask import Flask, request, jsonify
from flask_cors import CORS
import process_sanskrit as ps
import re
import json

app = Flask(__name__)
CORS(app)

def clean_definitions(content):
    """
    Recursively flattens the dictionary data.
    Normalizes whitespace but PRESERVES HTML tags.
    """
    results = []
    if isinstance(content, str):
        # Normalize whitespace but do NOT strip <tags>
        clean = re.sub(r'\s+', ' ', content).strip()
        if clean:
            results.append(clean)
    elif isinstance(content, list):
        for item in content:
            results.extend(clean_definitions(item))
    elif isinstance(content, dict):
        for value in content.values():
            results.extend(clean_definitions(value))
    return results

@app.route('/split')
def split_word():
    word = request.args.get('word')
    if not word: return jsonify({"error": "No word"}), 400
    try:
        split_result = ps.split(word)
        if split_result and isinstance(split_result[0], list):
            components = split_result[0]
        else:
            components = split_result if split_result else [word]
        components = [c for c in components if c]
        is_compound = len(components) > 1
        return jsonify({"is_compound": is_compound, "components": components})
    except Exception as e:
        return jsonify({"is_compound": False, "components": [word]})

@app.route('/meaning')
def get_meaning():
    word = request.args.get('word')
    try:
        raw_results = ps.process(word, 'mw', 'ap90', 'cae', 'bhs')
        if not raw_results:
            return jsonify([])

        grouped_results = {}

        for entry in raw_results:
            stem = entry[0]
            word_type = entry[1] if entry[1] else "morphology"
            grammar = entry[2] if entry[2] else [["form recognized"]]
            dict_data = entry[6]
            
            formatted_defs = {}
            for source, content in dict_data.items():
                cleaned = clean_definitions(content)
                if cleaned:
                    formatted_defs[source] = cleaned

            def_key = json.dumps(formatted_defs, sort_keys=True)

            if def_key in grouped_results:
                existing = grouped_results[def_key]
                for tag_set in grammar:
                    if tag_set not in existing["detected_tags"]:
                        existing["detected_tags"].append(tag_set)
                if word_type not in existing["type"]:
                    existing["type"] += f" / {word_type}"
            else:
                grouped_results[def_key] = {
                    "stem": stem,
                    "type": word_type,
                    "detected_tags": grammar,
                    "definitions": formatted_defs
                }

        return jsonify(list(grouped_results.values()))
    except Exception as e:
        print(f"Meaning error: {e}")
        return jsonify({"error": str(e)}), 500

if __name__ == '__main__':
    app.run(debug=True)