9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import helpers
import enum_strings
import grammalecte.graphspell as sc
from com.sun.star.task import XJobExecutor
from com.sun.star.awt import XActionListener
def hexToRBG (sHexa):
r = int(sHexa[:2], 16)
g = int(sHexa[2:4], 16)
b = int(sHexa[4:], 16)
return (r & 255) << 16 | (g & 255) << 8 | (b & 255)
|
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
import helpers
import enum_strings
import grammalecte.graphspell as sc
from com.sun.star.task import XJobExecutor
from com.sun.star.awt import XActionListener
from com.sun.star.awt.MessageBoxButtons import BUTTONS_OK
# BUTTONS_OK, BUTTONS_OK_CANCEL, BUTTONS_YES_NO, BUTTONS_YES_NO_CANCEL, BUTTONS_RETRY_CANCEL, BUTTONS_ABORT_IGNORE_RETRY
# DEFAULT_BUTTON_OK, DEFAULT_BUTTON_CANCEL, DEFAULT_BUTTON_RETRY, DEFAULT_BUTTON_YES, DEFAULT_BUTTON_NO, DEFAULT_BUTTON_IGNORE
from com.sun.star.awt.MessageBoxType import INFOBOX, ERRORBOX # MESSAGEBOX, INFOBOX, WARNINGBOX, ERRORBOX, QUERYBOX
def MessageBox (xDocument, sMsg, sTitle, nBoxType=INFOBOX, nBoxButtons=BUTTONS_OK):
xParentWin = xDocument.CurrentController.Frame.ContainerWindow
ctx = uno.getComponentContext()
xToolkit = ctx.ServiceManager.createInstanceWithContext("com.sun.star.awt.Toolkit", ctx)
xMsgBox = xToolkit.createMessageBox(xParentWin, nBoxType, nBoxButtons, sTitle, sMsg)
return xMsgBox.execute()
def hexToRBG (sHexa):
r = int(sHexa[:2], 16)
g = int(sHexa[2:4], 16)
b = int(sHexa[4:], 16)
return (r & 255) << 16 | (g & 255) << 8 | (b & 255)
|
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
self._addWidget('count_button', 'Button', nX, nY1+12, 70, 11, Label = self.dUI.get('count_button', "#err"))
self._addWidget('count2_button', 'Button', nX+75, nY1+12, 70, 11, Label = self.dUI.get('count2_button', "#err"))
self._addWidget('unknown_button', 'Button', nX+150, nY1+12, 70, 11, Label = self.dUI.get('unknown_button', "#err"))
self.xGridModel = self._addGrid("list_grid", nX, nY1+25, nWidth, 181, \
[ {"Title": self.dUI.get("words", "#err"), "ColumnWidth": 175}, {"Title": "Occurrences", "ColumnWidth": 45} ], \
SelectionModel = uno.Enum("com.sun.star.view.SelectionType", "MULTI") \
)
self._addWidget('num_of_entries', 'FixedText', nX, nY1+210, 60, nHeight, Label = self.dUI.get('num_of_entries', "#err"), Align = 2)
self.xNumWord = self._addWidget('num_of_entries_res', 'FixedText', nX+65, nY1+210, 25, nHeight, Label = "—")
self._addWidget('tot_of_entries', 'FixedText', nX+90, nY1+210, 60, nHeight, Label = self.dUI.get('tot_of_entries', "#err"), Align = 2)
self.xTotWord = self._addWidget('tot_of_entries_res', 'FixedText', nX+155, nY1+210, 30, nHeight, Label = "—")
self.xSearch = self._addWidget('search_button', 'Button', nX+190, nY1+210, 30, nHeight, Label = ">>>", Enabled = False)
# Tag
# Note: the only way to group RadioButtons is to create them successively
self._addWidget("charstyle_section", 'FixedLine', nX, nY2, 200, nHeight, Label = self.dUI.get("charstyle_section", "#err"), FontDescriptor = xFDTitle)
self.xAccent = self._addWidget('emphasis', 'RadioButton', nX, nY2+12, 55, nHeight, Label = self.dUI.get('emphasis', "#err"))
self.xStrongAccent = self._addWidget('strong_emphasis', 'RadioButton', nX+60, nY2+12, 70, nHeight, Label = self.dUI.get('strong_emphasis', "#err"))
self.xNoAccent = self._addWidget('nostyle', 'RadioButton', nX+140, nY2+12, 45, nHeight, Label = self.dUI.get('nostyle', "#err"))
|
|
|
|
|
|
>
|
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
self._addWidget('count_button', 'Button', nX, nY1+12, 70, 11, Label = self.dUI.get('count_button', "#err"))
self._addWidget('count2_button', 'Button', nX+75, nY1+12, 70, 11, Label = self.dUI.get('count2_button', "#err"))
self._addWidget('unknown_button', 'Button', nX+150, nY1+12, 70, 11, Label = self.dUI.get('unknown_button', "#err"))
self.xGridModel = self._addGrid("list_grid", nX, nY1+25, nWidth, 181, \
[ {"Title": self.dUI.get("words", "#err"), "ColumnWidth": 175}, {"Title": "Occurrences", "ColumnWidth": 45} ], \
SelectionModel = uno.Enum("com.sun.star.view.SelectionType", "MULTI") \
)
self._addWidget('num_of_entries', 'FixedText', nX, nY1+210, 30, nHeight, Label = self.dUI.get('num_of_entries', "#err"), Align = 2)
self.xNumWord = self._addWidget('num_of_entries_res', 'FixedText', nX+35, nY1+210, 25, nHeight, Label = "—")
self._addWidget('tot_of_entries', 'FixedText', nX+60, nY1+210, 30, nHeight, Label = self.dUI.get('tot_of_entries', "#err"), Align = 2)
self.xTotWord = self._addWidget('tot_of_entries_res', 'FixedText', nX+95, nY1+210, 30, nHeight, Label = "—")
self.xSearch = self._addWidget('search_button', 'Button', nX+145, nY1+210, 30, nHeight, Label = ">>>", Enabled = False)
self.xExport = self._addWidget('export_button', 'Button', nX+180, nY1+210, 40, nHeight, Label = self.dUI.get('export', "#err"), Enabled = False)
# Tag
# Note: the only way to group RadioButtons is to create them successively
self._addWidget("charstyle_section", 'FixedLine', nX, nY2, 200, nHeight, Label = self.dUI.get("charstyle_section", "#err"), FontDescriptor = xFDTitle)
self.xAccent = self._addWidget('emphasis', 'RadioButton', nX, nY2+12, 55, nHeight, Label = self.dUI.get('emphasis', "#err"))
self.xStrongAccent = self._addWidget('strong_emphasis', 'RadioButton', nX+60, nY2+12, 70, nHeight, Label = self.dUI.get('strong_emphasis', "#err"))
self.xNoAccent = self._addWidget('nostyle', 'RadioButton', nX+140, nY2+12, 45, nHeight, Label = self.dUI.get('nostyle', "#err"))
|
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
if not self.oSpellChecker:
self.oSpellChecker = sc.SpellChecker("fr")
self._setTitleOfFirstColumn(sTitle)
self.xProgressBar.ProgressValueMax = self._countParagraph() * 2
self.xProgressBar.ProgressValue = 0
xGridDataModel = self.xGridModel.GridDataModel
xGridDataModel.removeAllRows()
dWord = {}
for sParagraph in self._getParagraphsFromText():
dWord = self.oSpellChecker.countWordsOccurrences(sParagraph, bByLemma, bOnlyUnknownWords, dWord)
self.xProgressBar.ProgressValue += 1
self.xProgressBar.ProgressValueMax += len(dWord)
i = 0
nTotOccur = 0
for k, w in sorted(dWord.items(), key=lambda t: t[1], reverse=True):
xGridDataModel.addRow(i, (k, w))
self.xProgressBar.ProgressValue += 1
i += 1
nTotOccur += w
self.xProgressBar.ProgressValue = self.xProgressBar.ProgressValueMax
self.xNumWord.Label = str(i)
self.xTotWord.Label = nTotOccur
@_waitPointer
def tagText (self, aWord, sAction=""):
if not sAction:
return
self.xProgressBar.ProgressValueMax = self._countParagraph()
self.xProgressBar.ProgressValue = 0
if not self.oTokenizer:
|
|
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
if not self.oSpellChecker:
self.oSpellChecker = sc.SpellChecker("fr")
self._setTitleOfFirstColumn(sTitle)
self.xProgressBar.ProgressValueMax = self._countParagraph() * 2
self.xProgressBar.ProgressValue = 0
xGridDataModel = self.xGridModel.GridDataModel
xGridDataModel.removeAllRows()
self.dWord.clear()
for sParagraph in self._getParagraphsFromText():
self.dWord = self.oSpellChecker.countWordsOccurrences(sParagraph, bByLemma, bOnlyUnknownWords, self.dWord)
self.xProgressBar.ProgressValue += 1
self.xProgressBar.ProgressValueMax += len(self.dWord)
i = 0
nTotOccur = 0
for sWord, nOccur in sorted(self.dWord.items(), key=lambda t: t[1], reverse=True):
xGridDataModel.addRow(i, (sWord, nOccur))
self.xProgressBar.ProgressValue += 1
i += 1
nTotOccur += nOccur
self.xProgressBar.ProgressValue = self.xProgressBar.ProgressValueMax
self.xNumWord.Label = str(i)
self.xTotWord.Label = nTotOccur
def export (self):
if not self.dWord:
return
sText = ""
for sWord, nOccur in sorted(self.dWord.items(), key=lambda t: t[1], reverse=True):
sText += sWord + "\t" + str(nOccur) + "\n"
try:
xFilePicker = self.xSvMgr.createInstanceWithContext('com.sun.star.ui.dialogs.FilePicker', self.ctx) # other possibility: com.sun.star.ui.dialogs.SystemFilePicker
xFilePicker.initialize([uno.getConstantByName("com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_SIMPLE")]) # seems useless
xFilePicker.appendFilter("Supported files", "*.txt")
xFilePicker.setDefaultName("word_count.txt") # useless, doesn’t work
xFilePicker.setDisplayDirectory("")
xFilePicker.setMultiSelectionMode(False)
nResult = xFilePicker.execute()
if nResult == 1:
# lFile = xFilePicker.getSelectedFiles()
lFile = xFilePicker.getFiles()
spfExported = lFile[0][8:] # remove file:///
#spfExported = os.path.join(os.path.expanduser("~"), "fr.personal.json")
with open(spfExported, "w", encoding="utf-8") as hDst:
hDst.write(sText)
except:
sMessage = traceback.format_exc()
MessageBox(self.xDocument, sMessage, self.dUI.get('export_title', "#err"), ERRORBOX)
@_waitPointer
def tagText (self, aWord, sAction=""):
if not sAction:
return
self.xProgressBar.ProgressValueMax = self._countParagraph()
self.xProgressBar.ProgressValue = 0
if not self.oTokenizer:
|