-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.py
74 lines (68 loc) · 2.12 KB
/
service.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
"""
Created on 2020-05-15
"""
import sys
from flask import Flask
from flask import request, abort
from flask import jsonify
import os.path
import time
import pickle
import pandas as pd
from sources.vietdict import TiengViet
from sources.matching import StringMatching
class VNcorrectAPI():
def __init__(self):
self.app = Flask(__name__)
self.app.config["DEBUG"] = True
self.app.config['JSON_AS_ASCII'] = False
@self.app.route('/')
def home():
return '''<h1>Here we go!</h1>
<p>A prototype API for Vietnamese name correction.</p>'''
@self.app.route('/v1', methods=['GET'])
def api_correct():
word = request.args['word']
if word is None:
return 'No name provided. Please specify a name!'
else:
result = {}
tv = TiengViet()
start = datetime.datetime.now()
word = tv.removePunctuation(word) # Remove punctuation
word = self.vn_correct(word) # Spelling correction
print(word)
result['request_datetime'] = start
result['fullname'] = word
return jsonify(result)
def vn_correct(self, word):
"""
Spelling checking and correction
"""
tv = TiengViet()
word = word.lower()
check, mispelling = tv.checkTiengViet(word)
for m in mispelling:
corrected = tv.correctTiengViet(m)
word = word.replace(m, corrected)
return word.upper()
def lastname_match(self, fullname):
"""
Matching lastname (optional)
"""
tokens = fullname.split(' ')
lastname = tokens[0]
sm = StringMatching()
best_match = sm.word_similarity(lastname)
tokens[0] = best_match
return ' '.join(tokens)
def run(self, port):
self.app.run(host='0.0.0.0',port=port)
if __name__=='__main__':
args = sys.argv
if len(args) > 1:
port = int(args[1])
else:
port = 8080
service = VNcorrectAPI()
service.run(port)