-
Notifications
You must be signed in to change notification settings - Fork 4
/
prakriya_restplus.py
199 lines (150 loc) · 12 KB
/
prakriya_restplus.py
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Create a web service for verb form derivation.
See api.sanskritworld.in for documentation.
Author - Dr. Dhaval Patel
Date - 04 January 2023
email - [email protected]
"""
from flask import Flask, jsonify
#from flask_restplus import Api, Resource, reqparse
from flask_restx import Api, Resource, reqparse
from flask_cors import CORS
import html
from prakriya import Prakriya
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
CORS(app)
apiversion = 'v0.0.2'
api = Api(app, version=apiversion, title=u'Prakriyāpradarśinī API', description='Describes step by step verb form generation according to Paninian grammar. For more user friendly experience, please visit <a href="https://avinashvarna.github.io/prakriya/" target="_blank">this page</a> by Avinash Varna. Please report any issues <a href="https://github.com/drdhaval2785/prakriya/issues">here</a>. For more details, please see the github projects for <a href="https://github.com/drdhaval2785/SanskritVerb">SanskritVerb</a> and <a href="https://github.com/drdhaval2785/prakriya">prakriya</a>.')
storagedirectory = '/var/www/html/sanskritworldflask/'
def wholedata(verbform, inTran='slp1', outTran='slp1', argument=''):
"""Fetch the whole data from prakriya package."""
p = Prakriya()
p.input_translit(inTran)
p.output_translit(outTran)
verbform = htmlunescape(verbform)
result = p.get_info(verbform, argument)
return result
# https://stackoverflow.com/questions/2087370/decode-html-entities-in-python-string
def htmlunescape(htmlencodedstring):
return html.unescape(htmlencodedstring)
@api.route('/' + apiversion + '/verbforms/<string:input_transliteration>/<string:verbform>')
@api.doc(params={'verbform': 'Verb form to be examined.', 'input_transliteration': 'devanagari/slp1/iast/hk/wx/itrans/kolkata/velthuis'})
class FullData(Resource):
"""Return the JSON data regarding verb information and derivation steps for given verb form."""
get_parser = reqparse.RequestParser()
get_parser.add_argument('output_transliteration', location='args', default='devanagari', help='devanagari/slp1/iast/hk/wx/itrans/kolkata/velthuis')
@api.expect(get_parser, validate=True)
def get(self, input_transliteration, verbform):
"""Return the verb information and derivation steps for given verb form.
This function reads the pregenerated JSON files.
If the JSON file exists, it is loaded back.
If the file is absent, an error is thrown.
The JSON files are created by the script at SanskritVerb repository.
See https://github.com/drdhaval2785/SanskritVerb/blob/master/jsongenerator.sh for details.
"""
output_transliteration = self.get_parser.parse_args()['output_transliteration']
return jsonify(wholedata(verbform,
input_transliteration,
output_transliteration))
@api.route('/' + apiversion + '/verbforms/<string:input_transliteration>/<string:verbform>/prakriya')
@api.doc(params={'verbform': 'Verb form to be examined.', 'input_transliteration': 'devanagari/slp1/iast/hk/wx/itrans/kolkata/velthuis'})
class GetPrakriya(Resource):
"""Return human readable derivation of a given verb form."""
get_parser = reqparse.RequestParser()
get_parser.add_argument('output_transliteration', location='args', default='devanagari', help='devanagari/slp1/iast/hk/wx/itrans/kolkata/velthuis')
@api.expect(get_parser, validate=True)
def get(self, verbform, input_transliteration):
"""Return step by step derivation of a given verb form.
Dependencies
This function has a dependency on sutrainfo.json.
sutrainfo.json has sutra number, sutra text key value pairs.
The data stored in JSON does not have sutra text. it only has sutra number.
sutra text is supplied from sutrainfo.json.
See https://github.com/drdhaval2785/prakriya/blob/master/data/sutrainfo.json.
"""
output_transliteration = self.get_parser.parse_args()['output_transliteration']
return jsonify(wholedata(verbform,
input_transliteration,
output_transliteration,
argument='prakriya'))
@api.route('/' + apiversion + '/verbforms/<string:input_transliteration>/<string:verbform>/<string:argument>')
@api.doc(params={'verbform': 'Verb form to be examined.', 'argument': 'See Implementation notes / docstring of GET method.', 'input_transliteration': 'devanagari/slp1/iast/hk/wx/itrans/kolkata/velthuis'})
class SpecificInfo(Resource):
"""Return the specific sought for information of a given verb form."""
get_parser = reqparse.RequestParser()
get_parser.add_argument('output_transliteration', location='args', default='devanagari', help='devanagari/slp1/iast/hk/wx/itrans/kolkata/velthuis')
@api.expect(get_parser, validate=True)
def get(self, verbform, argument, input_transliteration):
"""Return the specific sought for information of a given verb form.
Valid arguments and expected output are as follows.
"verb" - Return verb in Devanagari without accent marks.
"verbaccent" - Return the verb with accent marks. This item is not changed irrespective of transliteration choices.
"lakara" - Return the lakAra (tense / mood) in which this form is generated.
"purusha" - Return the purusha in which this form is generated.
"suffix" - Return the suffix by which this form is generated.
"vachana" - Return the vachana in which this form is generated.
"gana" - Return the gaNa (class) of the verb.
"meaning" - Return meaning of the verb in SLP1 transliteration.
"number" - Return number of the verb in dhAtupATha.
"madhaviya" - Return link to mAdhaviyadhAtuvRtti. http://sanskrit.uohyd.ac.in/scl/dhaatupaatha is the home page.
"kshiratarangini" - Return link to kSIrataraGgiNI. http://sanskrit.uohyd.ac.in/scl/dhaatupaatha is the home page.
"dhatupradipa" - Return link to dhAtupradIpa. http://sanskrit.uohyd.ac.in/scl/dhaatupaatha is the home page.
"jnu" - Return link to JNU site for this verb form. http://sanskrit.jnu.ac.in/tinanta/tinanta.jsp is the home page.
"uohyd" - Return link to UoHyd site for this verb form. http://sanskrit.uohyd.ac.in/cgi-bin/scl/skt_gen/verb/verb_gen.cgi is the home page.
"upasarga" - Return upasarga, if any. Currently we do not support verb forms with upasargas.
"padadecider_id" - Return the rule number which decides whether the verb is parasmaipadI, AtmanepadI or ubhayapadI.
"padadecider_sutra" - Return the rule text which decides whether the verb is parasmaipadI, AtmanepadI or ubhayapadI.
"it_id" - Returns whether the verb is seT, aniT or veT, provided the form has iDAgama.
"it_status" - Returns whether the verb form has iDAgama or not. seT, veT, aniT are the output.
"it_sutra" - Returns rule number if iDAgama is caused by some special rule.
"""
output_transliteration = self.get_parser.parse_args()['output_transliteration']
validArgs = ['verb', 'verbaccent', 'lakara', 'purusha', 'vachana',
'suffix', 'gana', 'meaning', 'number', 'madhaviya',
'kshiratarangini', 'dhatupradipa', 'jnu', 'uohyd',
'upasarga', 'padadecider_id', 'padadecider_sutra',
'it_id', 'it_status', 'it_sutra']
if argument not in validArgs:
return jsonify({'error': 'Not a valid argument.'})
else:
return jsonify(wholedata(verbform,
input_transliteration,
output_transliteration,
argument))
@app.errorhandler(404)
def not_found(error):
"""Give URIs if app hits 404."""
uris = giveuris()
return jsonify({'uris': uris, 'error': 'Use any of the URIs given below.'})
def giveuris(verbform='<verbform>', webserver='https://api.sanskritworld.in', version=apiversion):
"""Give the URIs list for RESTful service."""
uris = {'prakriya_human_readable': webserver + '/' + version + '/verbforms/<input_transliteration>/' + verbform + '/prakriya?output_transliteration=<output_transliteration>',
'prakriya_machine_readable': webserver + '/' + version + '/verbforms/<input_transliteration>/' + verbform + '/prakriya/machine?output_transliteration=<output_transliteration>',
'verb_devanagari': webserver + '/' + version + '/verbforms/<input_transliteration>/' + verbform + '/verb?output_transliteration=<output_transliteration>',
'verb_meaning': webserver + '/' + version + '/verbforms/<input_transliteration>/' + verbform + '/meaning?output_transliteration=<output_transliteration>',
'verb_number': webserver + '/' + version + '/verbforms/<input_transliteration>/' + verbform + '/number?output_transliteration=<output_transliteration>',
'verb_gana': webserver + '/' + version + '/verbforms/<input_transliteration>/' + verbform + '/gana?output_transliteration=<output_transliteration>',
'purusha': webserver + '/' + version + '/verbforms/<input_transliteration>/' + verbform + '/purusha?output_transliteration=<output_transliteration>',
'vachana': webserver + '/' + version + '/verbforms/<input_transliteration>/' + verbform + '/vachana?output_transliteration=<output_transliteration>',
'suffix': webserver + '/' + version + '/verbforms/<input_transliteration>/' + verbform + '/suffix?output_transliteration=<output_transliteration>',
'madhaviyadhatuvritti_link': webserver + '/' + version + '/verbforms/<input_transliteration>/' + verbform + '/madhaviya?output_transliteration=<output_transliteration>',
'kshiratarangini_link': webserver + '/' + version + '/verbforms/<input_transliteration>/' + verbform + '/kshiratarangini?output_transliteration=<output_transliteration>',
'dhatupradipa_link': webserver + '/' + version + '/verbforms/<input_transliteration>/' + verbform + '/dhatupradipa?output_transliteration=<output_transliteration>',
'UoHyd_link': webserver + '/' + version + '/verbforms/<input_transliteration>/' + verbform + '/uohyd?output_transliteration=<output_transliteration>',
'JNU_link': webserver + '/' + version + '/verbforms/<input_transliteration>/' + verbform + '/jnu?output_transliteration=<output_transliteration>',
'verb_slp': webserver + '/' + version + '/verbforms/<input_transliteration>/' + verbform + '/verbslp?output_transliteration=<output_transliteration>',
'lakAra_or_tense': webserver + '/' + version + '/verbforms/<input_transliteration>/' + verbform + '/lakara?output_transliteration=<output_transliteration>',
'upasarga': webserver + '/' + version + '/verbforms/<input_transliteration>/' + verbform + '/upasarga?output_transliteration=<output_transliteration>',
'padadecider_id': webserver + '/' + version + '/verbforms/<input_transliteration>/' + verbform + '/padadecider_id?output_transliteration=<output_transliteration>',
'padadecider_sutra': webserver + '/' + version + '/verbforms/<input_transliteration>/' + verbform + '/padadecider_sutra?output_transliteration=<output_transliteration>',
'it_sutra': webserver + '/' + version + '/verbforms/<input_transliteration>/' + verbform + '/it_sutra?output_transliteration=<output_transliteration>',
'it_id': webserver + '/' + version + '/verbforms/<input_transliteration>/' + verbform + '/it_id?output_transliteration=<output_transliteration>',
'it_status': webserver + '/' + version + '/verbforms/<input_transliteration>/' + verbform + '/it_status?output_transliteration=<output_transliteration>',
'all_details': webserver + '/' + version + '/verbforms/<input_transliteration>/' + verbform + '?output_transliteration=<output_transliteration>',
}
return uris
if __name__ == '__main__':
# app.run(debug=True)
app.run()