-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsumma-web.py
97 lines (77 loc) · 2.54 KB
/
summa-web.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
#! /usr/bin/env python
import web
import json
import time
import random
import os
from summa.summarizer import summarize
from summa.keywords import keywords
from summa.export import gexf_export
SAMPLES_DIRECTORY = "samples"
render = web.template.render('templates/', base='layout')
urls = (
'/', 'autosummarize',
'/about', 'about',
'/gexf','gexf',
'/sample', 'text_sample'
)
LANGUAGES = {
'danish': 'Danish',
'dutch': 'Dutch',
'english': 'English',
'finnish': 'Finnish',
'french': 'French',
'german': 'German',
'hungarian': 'Hungarian',
'italian': 'Italian',
'norwegian': 'Norwegian',
'porter': 'Porter',
'portuguese': 'Portuguese',
'romanian': 'Romanian',
'russian': 'Russian',
'spanish': 'Spanish',
'swedish': 'Swedish'
}
def format_results(result, show_scores, method):
separator = "<br>" if method == "summarize" else "<br> - "
result = ["{:.3f}".format(score) + " - " + phrase for phrase, score in result ]if show_scores else result
return separator[4:] + separator.join(result)
class autosummarize:
def GET(self):
return render.autosummarize(languages=LANGUAGES)
def POST(self):
args = web.input()
show_scores = args.scores == "true"
length = int(args.length) / 100.0
language = args.language
text = args.text
method = summarize if args.method == "summarize" else keywords
try:
result = method(text=text, ratio=length, language=language, split=True, scores=show_scores)
return format_results(result, show_scores, args.method)
except:
return ""
class text_sample:
def GET(self):
filename = random.choice(os.listdir(SAMPLES_DIRECTORY))
with open(os.path.join(SAMPLES_DIRECTORY, filename)) as textfile:
return textfile.read()
class gexf:
def GET(self):
return render.gexf()
def POST(self):
args = web.input()
language = args.language
text = args.text
by_sentence, by_word = (True, False) if args.method == "summarize" else (False, True)
path = "export-" + str(time.time()) + ".gexf"
gexf_export(text=text, path=path, language=language, by_sentence=by_sentence, by_word=by_word)
os.system("mv {0} static/{0}".format(path))
return render.gexf(path="./static/" + path)
class about:
def GET(self):
return render.about()
app = web.application(urls, globals())
if __name__ == "__main__":
app.run()
wsgiapp = app.wsgifunc()