-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial07.py
More file actions
92 lines (69 loc) · 3.31 KB
/
tutorial07.py
File metadata and controls
92 lines (69 loc) · 3.31 KB
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
from PySide2.QtWidgets import QApplication
from nlScript.core.autocompletion import Autocompletion
from nlScript.ebnf.ebnfparser import ParseStartListener
from nlScript.parser import Parser
from nlScript.ui.ui import ACEditor
from preprocessing import Preprocessing
"""
The previous tutorial showed how to use a custom Autocompleter, but suffered from one
autocompletion issue:
Once the user starts typing the text for the unit (let's say a 'p' for 'pixel(s)'), completion
should stop (or even better, only suggest 'pixel(s)', because 'mm' doesn't start with 'p').
However, here it will just continue to suggest 'pixel(s)' as well as 'mm'.
This tutorial demonstrates how to stop autocompletion once the user started typing a value.
In the case here, it would actually be better to filter suggested options according to what the user typed,
(and this will be shown in the next tutorial), but there are cases where stopping auto-completion after the
user started to type is important: This is particularly the case if e.g. entering numbers: As long as nothing
is entered, auto-completion should indicate what needs to be entered (e.g. a placeholder with a name), but once
the user started typing a number, auto-completion should be quiet. BTW: This is not only true for numbers, but
for also e.g. when entering a name for something.
For details, see
https://nlScript.github.io/nlScript-java/#prohibit-further-autocompletion-autocompleterveto
"""
if __name__ == '__main__':
# Needed for running a PySide application
app = QApplication([])
preprocessing = Preprocessing(None)
preprocessing.open('http://imagej.net/images/clown.jpg')
preprocessing.show()
preprocessing.setPixelWidth(0.25, "mm")
parser = Parser()
imageUnits = ""
def parsingStarted():
global imageUnits
imageUnits = preprocessing.getUnits()
parser.addParseStartListener(listener=ParseStartListener(parsingStarted))
# The only change here, compared to the previous version, is
# to check whether the user has started typing for 'units', in
# which case pn.getParsedString() returns what's already entered.
# If something was entered, a special 'veto' autocompleter is returned,
# which prohibits further auto-completion.
def getAutocompletion(pn, justCheck):
if len(pn.getParsedString()) == 0:
return Autocompletion.literal(pn, ["pixel(s)", imageUnits])
return Autocompletion.veto(pn)
parser.defineType("units", "{unistring:[a-zA-Z()]:+}",
evaluator=lambda pn: pn.getParsedString() != "pixel(s)",
autocompleter=getAutocompletion)
def evaluateFilterSize(pn):
stddev = pn.evaluate("stddev")
units = pn.evaluate("units")
if units:
stddev /= preprocessing.getPixelWidth()
return stddev
parser.defineType(
"filter-size",
"{stddev:float} {units:units}",
evaluator=evaluateFilterSize,
autocompleter=True
)
def evaluateSentence(pn):
stddev = pn.evaluate("stddev")
preprocessing.gaussianBlur(stddev)
parser.defineSentence(
"Apply Gaussian blurring with a standard deviation of {stddev:filter-size}.",
evaluator=evaluateSentence)
editor = ACEditor(parser)
editor.show()
# Needed for running a PySide application
exit(app.exec_())