-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlmorphweb.py
170 lines (154 loc) · 4.57 KB
/
mlmorphweb.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
import sys
import regex
import os
from flask import Flask, jsonify, render_template, request
from flask_swagger import swagger
os.chdir(os.path.dirname(os.path.realpath(__file__)))
from mlmorph import Generator, Analyser
from mlmorph_spellchecker import SpellChecker
app = Flask(__name__,
static_folder = "./dist/",
static_url_path='',
template_folder = "./dist")
generator = Generator()
analyser = Analyser()
spellchecker = SpellChecker()
@app.route("/",defaults={'path': ''})
@app.route('/ner', defaults={'path':'ner'})
@app.route('/spellcheck', defaults={'path':'spellcheck'})
@app.route('/number', defaults={'path':'number'})
@app.route('/about', defaults={'path':'about'})
@app.route('/generator', defaults={'path':'generator'})
def index(path):
return render_template('index.html',)
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
return response
@app.route("/api/analyse", methods=['POST', 'GET'])
def do_analyse():
"""
Analyse the given text
---
tags:
- analysis
consumes:
- application/json
produces:
- application/json
parameters:
- name: text
in: body
description: The text to be analysed. Can be multiple words.
type: string
required: true
"""
text = None
analyse_results = {}
if request.method == 'POST':
text = request.json.get('text')
else:
text = request.args.get('text')
text = text.strip()
words = regex.split(r'\s+', text)
# real analysis
for windex in range(len(words)):
word = words[windex]
anals = analyser.analyse(word, False)
anals_results = []
if len(anals) == 0:
anals_results = []
else:
for aindex in range(len(anals)):
anals_results.append(analyser.parse_analysis(anals[aindex][0]))
anals_results.sort(key=lambda analysis: analysis['weight'])
analyse_results[word] = anals_results
return jsonify(result=analyse_results)
@app.route("/api/generate", methods=['GET'])
def do_generate():
"""
Generate a word from given morpheme sequence
---
tags:
- generate
consumes:
- application/json
produces:
- application/json
parameters:
- name: word
in: query
description: The morpheme string
type: string
required: true
- name: type
in: query
description: The word POS
type: string
required: false
- name: infl
in: query
description: The inflection
type: string
required: false
"""
generate_results = []
word = request.args.get('word')
wordtype = request.args.get('type')
genInput = word
if wordtype:
genInput += '<' + wordtype + '>'
infl = request.args.get('infl')
if infl:
genInput += '<' + infl + '>'
gens = generator.generate(genInput)
if len(gens) == 0:
generate_results.append(genInput)
for gindex in range(len(gens)):
generate_results.append(gens[gindex][0])
return jsonify(word=word, type=wordtype, infl=infl, result=generate_results)
@app.route("/api/spellcheck", methods=['POST', 'GET'])
def do_spellcheck():
"""
Spellcheck the words given text
---
tags:
- spellcheck
consumes:
- application/json
produces:
- application/json
parameters:
- name: text
in: body
description: The morpheme string
type: string
required: true
"""
result = {}
if request.method == 'POST':
text = request.json.get('text')
else:
text = request.args.get('text')
text = text.strip()
words = regex.split(r'\s+', text)
result = {}
# real analysis
for windex in range(len(words)):
word = words[windex]
isCorrect = spellchecker.spellcheck(word)
suggestions = []
if not isCorrect:
suggestions = spellchecker.candidates(word)
result[word] = {'correct': isCorrect, 'suggestions': suggestions}
return jsonify(result)
@app.route("/spec")
def spec():
swag = swagger(app)
swag['info']['version'] = "1.0"
swag['info']['title'] = "mlmorph web api"
return jsonify(swag)
if __name__ == "__main__":
app.run(debug=True,host='0.0.0.0',port=int(os.environ.get('PORT', 8080)))