-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
98 lines (85 loc) · 1.72 KB
/
app.rb
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
require 'sinatra'
require 'json'
require 'jumanpp_ruby'
require 'yaml'
PARSE_ELEMTS = [
'表層形', '読み', '見出し語', '品詞大分類', '品詞大分類ID',
'品詞細分類', '品詞細分類ID', '活用型', '活用型ID', '活用形',
'活用形ID', '意味情報'
]
config = YAML.load_file('./config.yml')
juman = JumanppRuby::Juman.new(**config)
def string_param
if request.env['CONTENT_TYPE'] == 'application/json'
req = request.body.read
JSON.parse(req)['string']
else
params[:string]
end
end
post '/split', provides: :json do
content_type :json
data = {}
begin
str = string_param
raise unless str
rescue
data[:status] = 'fail'
status 400
return data.to_json
end
begin
words = juman.parse(str)
if words.last == 'EOS'
words = words.slice(0...-1)
end
rescue
data[:status] = 'fail'
status 500
return data.to_json
end
data[:status] = 'success'
data[:results] = words
data.to_json
end
post '/parse', provides: :json do
content_type :json
data = {}
begin
str = string_param
raise unless str
rescue
data[:status] = 'fail'
status 400
return data.to_json
end
begin
words = []
juman.parse(str) do |word|
words << word
end
if words.last == ['EOS']
words = words.slice(0...-1)
end
rescue
data[:status] = 'fail'
status 500
return data.to_json
end
data = {}
data[:status] = 'success'
data[:elements] = PARSE_ELEMTS
data[:results] = words
data.to_json
end
get '/version', provides: :json do
data = {}
begin
data[:results] = JumanppRuby::Juman.version
data[:status] = 'success'
rescue
data[:status] = 'fail'
status 500
end
data.to_json
end