This repository has been archived by the owner on Feb 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
main.py
140 lines (123 loc) · 3.7 KB
/
main.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
#!/usr/bin/env python
from bottle import *
try:
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
isGAE = True
except:
isGAE = False
pass
import os
import suku
from hmmtagger import MainTagger
from tokenization import *
from html2text import *
from termextract import *
from summary import *
from capschunking import *
mt = None
@route('favicon.ico')
def favicon():
return static_file('favicon.ico', root='static/')
@route('/')
@view('newindex')
def index():
return { 'apptitle':'pebahasa', 'root':request.environ.get('SCRIPT_NAME') }
@post('/handler')
def default_handler():
task = request.forms.get('task', '')
if task=='htmltext':
return do_html2text()
elif task=='extractterm':
return do_terms()
elif task=='summary':
return do_summary()
elif task=='postag':
return do_tag()
elif task=='capschunk':
return do_caps()
elif task=='sents':
return do_sents()
response.content_type = 'text/plain'
return "NotImplemented"
@post('/sents')
def do_sents():
response.content_type = 'text/plain'
lines = request.forms.get('teks', '').strip().split("\n")
result = []
for l in lines:
if len(l) == 0: continue
out = sentence_extraction(cleaning(l))
for o in out:
result.append(o)
return "<br/><br/>".join(result)
@post('/capschunk')
def do_caps():
response.content_type = 'text/plain'
lines = request.forms.get('teks', '').strip().split("\n")
result = []
for l in lines:
if len(l) == 0: continue
out = sentence_extraction(cleaning(l))
for o in out:
tmp = group_caps(o)
tmp = [" ".join(g) for g in tmp]
strtmp = "<br/>".join(tmp)
result.append(strtmp)
return "<br/><br/>".join(result)
@post('/terms')
def do_terms():
response.content_type = 'text/plain'
lines = request.forms.get('teks', '').strip()
return "<br/>".join(extract_terms(lines))
@post('/summary')
def do_summary():
response.content_type = 'text/plain'
lines = request.forms.get('teks', '').strip()
return make_summary(lines)
@post('/html2text')
def do_html2text():
response.content_type = 'text/plain'
lines = request.forms.get('teks', '').strip()
return get_text(lines)
@post('/penggal')
@view('index')
def penggal():
kata = request.forms.get('word', '').strip()
fon = suku.pecah(kata)
return { 'apptitle':'pebahasa', 'content':'<div class="formsection">'+kata+" : <strong>"+"-".join(fon)+"</strong></div>", 'root':request.environ.get('SCRIPT_NAME') }
@route('/tag')
@route('sentence_tagging')
def postag():
return { 'apptitle':'pebahasa', 'root':request.environ.get('SCRIPT_NAME') }
def init_tag():
global mt
if mt is None:
mt = MainTagger("resource/Lexicon.trn", "resource/Ngram.trn", 0, 3, 3, 0, 0, False, 0.2, 0, 500.0, 1)
@post('/tag')
def do_tag():
response.content_type = 'text/plain'
lines = request.forms.get('teks', '').strip().split("\n")
result = []
try:
init_tag()
for l in lines:
if len(l) == 0: continue
out = sentence_extraction(cleaning(l))
for o in out:
strtag = " ".join(tokenisasi_kalimat(o)).strip()
result += [" ".join(mt.taggingStr(strtag))]
except:
return "Error Exception"
return "\n".join(result)
@route('/static/:fname#.+#')
def servestatic(fname):
return static_file(fname, root='static/')
def main():
if isGAE:
util.run_wsgi_app(default_app())
else:
init_tag()
run(port=8088, reloader=True)
if __name__ == '__main__':
main()