-
Notifications
You must be signed in to change notification settings - Fork 73
/
test_recobs_doctest.txt
217 lines (181 loc) · 6.78 KB
/
test_recobs_doctest.txt
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
.. _Recobs Doc Tests:
RecognitionObserver base class
==========================================================================
If you're looking for the class and function reference for recognition
observers, you want :ref:`this page <Recobs>`.
.. note:: :class:`RecognitionObserver` instances can be used for both
the DNS and the WSR backend engines. However, WSR will sometimes call
observer methods multiple times, so be careful using observers with it.
Test fixture initialization::
>>> from dragonfly import *
>>> from dragonfly.test import ElementTester
Trivial demonstration of RecognitionObserver class
----------------------------------------------------------------------------
The following class is derived from RecognitionObserver and prints
when its callback methods are called::
>>> class RecognitionObserverDemo(RecognitionObserver):
... def on_begin(self):
... print("on_begin()")
... def on_recognition(self, words):
... print("on_recognition(): %s" % (words,))
... def on_failure(self):
... print("on_failure()")
... def on_end(self):
... print("on_end()")
... def on_post_recognition(self, words, rule):
... print("on_post_recognition(): %r from %r" % (words, rule))
...
>>> recobs_demo = RecognitionObserverDemo()
>>> recobs_demo.register()
>>> test_lit = ElementTester(Literal("hello world"))
>>> test_lit.recognize("hello world")
on_begin()
on_recognition(): (u'hello', u'world')
on_end()
on_post_recognition(): (u'hello', u'world') from _ElementTestRule(rule)
u'hello world'
>>> test_lit.recognize("hello universe")
on_begin()
on_failure()
on_end()
RecognitionFailure
>>> recobs_demo.unregister()
Tests for RecognitionObserver class
----------------------------------------------------------------------------
A class derived from RecognitionObserver which will be used here for
testing it::
>>> class RecognitionObserverTester(RecognitionObserver):
... def __init__(self):
... RecognitionObserver.__init__(self)
... self.waiting = False
... self.words = None
... def on_begin(self):
... self.waiting = True
... def on_recognition(self, words):
... self.waiting = False
... self.words = words
... def on_failure(self):
... self.waiting = False
... self.words = False
...
>>> test_recobs = RecognitionObserverTester()
>>> test_recobs.register()
>>> test_recobs.waiting, test_recobs.words
(False, None)
Simple literal element recognitions::
>>> test_lit = ElementTester(Literal("hello world"))
>>> test_lit.recognize("hello world")
u'hello world'
>>> test_recobs.waiting, test_recobs.words
(False, (u'hello', u'world'))
>>> test_lit.recognize("hello universe")
RecognitionFailure
>>> test_recobs.waiting, test_recobs.words
(False, False)
Integer element recognitions::
>>> test_int = ElementTester(Integer(min=1, max=100))
>>> test_int.recognize("seven")
7
>>> test_recobs.waiting, test_recobs.words
(False, (u'seven',))
>>> test_int.recognize("forty seven")
47
>>> test_recobs.waiting, test_recobs.words
(False, (u'forty', u'seven'))
>>> test_int.recognize("one hundred")
RecognitionFailure
>>> test_recobs.waiting, test_recobs.words
(False, False)
>>> test_lit.recognize("hello world")
u'hello world'
RecognitionHistory class
==========================================================================
Basic usage of the RecognitionHistory class::
>>> history = RecognitionHistory()
>>> test_lit.recognize("hello world")
u'hello world'
>>> # Not yet registered, so didn't receive previous recognition.
>>> history
[]
>>> history.register()
>>> test_lit.recognize("hello world")
u'hello world'
>>> # Now registered, so should have received previous recognition.
>>> history
[(u'hello', u'world')]
>>> test_lit.recognize("hello universe")
RecognitionFailure
>>> # Failed recognitions are ignored, so history is unchanged.
>>> history
[(u'hello', u'world')]
>>> test_int.recognize("eighty six")
86
>>> history
[(u'hello', u'world'), (u'eighty', u'six')]
The RecognitionHistory class allows its maximum length to be set::
>>> history = RecognitionHistory(3)
>>> history.register()
>>> history
[]
>>> for i, word in enumerate(["one", "two", "three", "four", "five"]):
... assert test_int.recognize(word) == i + 1
>>> history
[(u'three',), (u'four',), (u'five',)]
The length must be a positive integer. A length of 0 is not allowed::
>>> history = RecognitionHistory(0)
Traceback (most recent call last):
...
ValueError: length must be a positive int or None, received 0.
Minimum length is 1::
>>> history = RecognitionHistory(1)
>>> history.register()
>>> history
[]
>>> for i, word in enumerate(["one", "two", "three", "four", "five"]):
... assert test_int.recognize(word) == i + 1
>>> history
[(u'five',)]
Recognition callback functions
==========================================================================
Register recognition callback functions::
>>> # Define and register functions for each recognition state event.
>>> def on_begin():
... print("on_begin()")
...
>>> def on_recognition(words):
... print("on_recognition(): %s" % (words,))
...
>>> def on_failure():
... print("on_failure()")
...
>>> def on_end():
... print("on_end()")
...
>>> def on_post_recognition(words, rule):
... print("on_post_recognition(): %r from %r" % (words, rule))
...
>>> on_begin_obs = register_beginning_callback(on_begin)
>>> on_success_obs = register_recognition_callback(on_recognition)
>>> on_failure_obs = register_failure_callback(on_failure)
>>> on_end_obs = register_ending_callback(on_end)
>>> on_post_recognition_obs = register_post_recognition_callback(on_post_recognition)
Callback functions are called during recognitions::
>>> test_lit = ElementTester(Literal("hello world"))
>>> test_lit.recognize("hello world")
on_begin()
on_recognition(): (u'hello', u'world')
on_end()
on_post_recognition(): (u'hello', u'world') from _ElementTestRule(rule)
u'hello world'
>>> test_lit.recognize("hello universe")
on_begin()
on_failure()
on_end()
RecognitionFailure
Callback functions are unregistered using the observer objects returned by
each function::
>>> on_begin_obs.unregister()
>>> on_success_obs.unregister()
>>> on_failure_obs.unregister()
>>> on_end_obs.unregister()
>>> on_post_recognition_obs.unregister()