This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pySummary.py
executable file
·109 lines (92 loc) · 3.52 KB
/
pySummary.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# generated by wxGlade 0.6.8 on Thu Sep 17 10:37:52 2015
#
import wx
# begin wxGlade: dependencies
import gettext
# end wxGlade
# begin wxGlade: extracode
# end wxGlade
import tempfile
import subprocess
import os
class MainFrame(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: MainFrame.__init__
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.panel_1 = wx.Panel(self, wx.ID_ANY)
self.label_1 = wx.StaticText(self.panel_1, wx.ID_ANY, _("Input text"))
self.inputField = wx.TextCtrl(self.panel_1, wx.ID_ANY, "", style=wx.TE_PROCESS_ENTER | wx.TE_MULTILINE | wx.TE_WORDWRAP)
self.panel_2 = wx.Panel(self, wx.ID_ANY)
self.label_2 = wx.StaticText(self.panel_2, wx.ID_ANY, _("Output summary"))
self.outputField = wx.TextCtrl(self.panel_2, wx.ID_ANY, "", style=wx.TE_MULTILINE | wx.TE_READONLY | wx.TE_WORDWRAP)
self.__set_properties()
self.__do_layout()
self.Bind(wx.EVT_TEXT_ENTER, self.makeSummary, self.inputField)
# end wxGlade
def __set_properties(self):
# begin wxGlade: MainFrame.__set_properties
self.SetTitle(_("pySummary"))
self.SetSize((750, 605))
self.inputField.SetFocus()
# end wxGlade
def __do_layout(self):
# begin wxGlade: MainFrame.__do_layout
sizer_2 = wx.BoxSizer(wx.VERTICAL)
sizer_4 = wx.BoxSizer(wx.VERTICAL)
sizer_3 = wx.BoxSizer(wx.VERTICAL)
sizer_3.Add(self.label_1, 0, 0, 0)
sizer_3.Add(self.inputField, 1, wx.EXPAND, 0)
self.panel_1.SetSizer(sizer_3)
sizer_2.Add(self.panel_1, 1, wx.EXPAND, 0)
sizer_4.Add(self.label_2, 0, 0, 0)
sizer_4.Add(self.outputField, 1, wx.EXPAND, 0)
self.panel_2.SetSizer(sizer_4)
sizer_2.Add(self.panel_2, 1, wx.EXPAND, 0)
self.SetSizer(sizer_2)
self.Layout()
# end wxGlade
def makeSummary(self, event): # wxGlade: MainFrame.<event_handler>
itext = self.inputField.GetValue()
# Code below mostly copied from
# https://wolfpaulus.com/journal/software/ots/
content = itext.encode('utf-8')
content = str(content.decode('ascii', 'ignore'))
content = " ".join(content.split())
temp_dir = tempfile.mkdtemp()
temp1 = tempfile.NamedTemporaryFile(suffix=".txt", dir=temp_dir, delete=False)
temp2 = tempfile.NamedTemporaryFile(suffix=".txt", dir=temp_dir, delete=False)
result = None
try:
temp1.write(content)
temp1.close()
r = 20
cmdline = '{0} -r {1} -o {2} {3}'.format('/usr/bin/ots', str(20), temp2.name, temp1.name)
subprocess.Popen(cmdline, shell=True, stdout=subprocess.PIPE).communicate()[0]
with temp2 as f:
result=f.read().replace('\n', ' ')
except:
raise
finally:
temp1.close()
os.remove(temp1.name)
temp2.close()
os.remove(temp2.name)
os.rmdir(temp_dir)
self.outputField.SetValue(result)
# end of class MainFrame
class PySummary(wx.App):
def OnInit(self):
wx.InitAllImageHandlers()
mainFrame = MainFrame(None, wx.ID_ANY, "")
self.SetTopWindow(mainFrame)
mainFrame.Show()
return 1
# end of class PySummary
if __name__ == "__main__":
gettext.install("app") # replace with the appropriate catalog name
app = PySummary(0)
app.MainLoop()