I have this token to detect a invalid invoice:
TOKEN : {
< #invalid : <number><client>(<mail> | <mailCom>)<wrongDoc><text><retorn> >
}
And this is my while loop:
while (ok) {
token = app.getNextToken();
switch (token.kind) {
case TextoConstants.client:
System.out.println(token.image);
break;
case TextoConstants.mailCom:
System.out.println(token.image);
break;
case TextoConstants.text:
System.out.println(token.image);
break;
case TextoConstants.invalid:
System.out.println("There is something wrong");
break;
ok = token.kind != TextoConstants.EOF;
}
The problem is when I try to analyze some text block it prints the client/(mailCom)?/text tokens, but no the invalid one.
This is the input:
Number 30003
Client MyName,Lastname
Mail myname#mail.com
DocId 346774
Text
some text block here.
END
The output should be There is something wrong, but instead it prints the other tokens.
Thanks.
Since #invalid starts with a #, it is not a real token kind. It's really just a macro that can be used to define other tokens.
Related
Started with the basic helloworld example this recognises speech correctly however I want to get intents back so modified the ViewController.swift as follows:
func recognizeFromMic() {
var speechConfig: SPXSpeechConfiguration?
do {
try speechConfig = SPXSpeechConfiguration(subscription: sub, region: region)
} catch {
print("error \(error) happened")
speechConfig = nil
}
speechConfig?.speechRecognitionLanguage = "en-US"
let audioConfig = SPXAudioConfiguration()
let languageUnderstandingModel = SPXLanguageUnderstandingModel(subscription: luisSub, withAppId: luisAppId, andRegion: luisRegion)
let reco = try! SPXIntentRecognizer(speechConfiguration: speechConfig!, audioConfiguration: audioConfig)
reco.addAllIntents(from: languageUnderstandingModel!)
reco.addRecognizingEventHandler() {reco, evt in
print("intermediate recognition result: \(evt.result?.text ?? "(no result)")")
self.logResultReason(reason: (evt.result?.reason)!);
self.updateLabel(text: evt.result?.text, color: .gray)
}
updateLabel(text: "Listening ...", color: .gray)
print("Listening...")
let result = try! reco.recognizeOnce()
print("recognition result: \(result.text ?? "(no result)")")
self.logResultReason(reason: result.reason);
updateLabel(text: result.text, color: .black)
}
func logResultReason(reason: SPXResultReason) {
switch reason {
case .canceled:
print("cancelled")
break
case .noMatch:
print("no Match")
break
case .recognizedIntent:
print("Intent")
break
case .recognizingIntent:
print("Recognizing Intent")
break
case .recognizingSpeech:
print("Recognizing Speech")
break
case .recognizedSpeech:
print("Speech")
break
default:
print("Other")
}
}
The output is
Token:
Listening...
2019-12-16 20:43:57.974675+1300 helloworld[51677:4869822] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x6000024d32c0> F8BB1C28-BAE8-11D6-9C31-00039315CD46
2019-12-16 20:43:58.156465+1300 helloworld[51677:4869886] PropertyID=1667788144 is NULL
intermediate recognition result: how
Recognizing Speech
intermediate recognition result: hello
Recognizing Speech
intermediate recognition result: hello world
Recognizing Speech
recognition result:
cancelled
There is long pause (30+ seconds) before the "recognition result" message is output. Looking on LUIS I can see that no utterances have been sent to endpoint. The LUIS subscription, appId I have used extensively for bots from C# and JS so reasonably confident that these are correct (I haven't included here for obvious reasons).
Hopefully something simple I have missed - any help greatly appreciated
Solution turned out to be related to setting up new paid for subscription and assigning that to LUIS model as resource.
I have a signal producer, when it's terminated I would like to know if a value was sent, I only need the last one, seems so simple ...
let myProducer: SignalProducer<MyObject, MyError> = getMyProducer()
myProducer.on(terminated: {
// I need the last value here
// Or I need to know if value was never called
}).start()
I've tried to store the value in a local var :
let myProducer: SignalProducer<MyObject, MyError> = getMyProducer()
var myValue: MyObject?
myProducer.on(value: { value in
myValue = value
}, terminated: {
guard let value = myValue else {
// value was never called
return
}
// value was called
}).start()
But sometimes terminated is called while value has been called but myValue is still nil...
First, are you really sure that you want the terminated event?
Under normal conditions, an event stream ends with a completed event. Exceptions are failed when a failure has occurred and interrupted, when the observation was ended before the stream could complete normally (E.g. cancellation).
Second: Can your SignalProducer fail, and in the failure case, do you still want the last value sent before the failure?
If not, its as easy as using the take(last:) operator:
enum MyError: Error {
case testError
}
let (signal, input) = Signal<Int, MyError>.pipe()
let observer = Signal<Int, MyError>.Observer(
value: { print("value: \($0)") },
failed: { print("error: \($0)") },
completed: { print("completed") },
interrupted: { print("interrupted") }
)
signal
.take(last: 1)
.observe(observer)
input.send(value: 1) // Nothing printed
input.send(value: 2) // Nothing printed
input.send(value: 3) // Nothing printed
input.sendCompleted() // value 3 printed
I'm using a Signal here so I can manually send events to it just for demonstration, the same works for SignalProducer as well.
Note: If we send interrupted or a failed event, the last value 3 will not be sent because those to terminating events short circuit the normal flow.
If your SignalProducer can fail, and you still want to get the last value before the failure, you can use flatMapError to ignore the Error before the last operator:
signal
.flatMapError { _ in
return .empty
}
.take(last: 1)
.observe(observer)
my answer :
producer
.flatMapError { _ in SignalProducer<Value, NoError>.empty }
.collect()
startWithResult( { result in
case let .success(results):
done(with: results.last)
case let .failure(error):
() // should not happen as errors are flatmapped
})
Today I've had majour problems with GameMakers switch statement being executed. When the HTTP ASYNC EVENT is executed, show_message(answer) is executed, but the switch statement under it does not.
I think this is a compiler error, because it seems that the string_length of the "answer variable" is 1, and using string_digits to make sure, that it escapes all the possible blankspaces or unnecessary characters that could appear when retrieving http callback.
Only possible returning values of variable answer are: 0, 1, 2, 3, 4.
So here is the code of HTTP ASYNC EVENT:
if(ds_map_find_value(async_load, "id") == request_auth) {
if(ds_map_find_value(async_load, "status") == 0) {
callback = ds_map_find_value(async_load, "result");
var answer = string_digits(callback);
show_message(answer);
switch(answer) {
case USERNAME_EXISTS:
show_message("username already exists");
break;
case ACCOUNT_CREATED:
show_message("Your account has been successfully created!");
break;
case LOGGED_IN:
buffer_seek(global.buffer, buffer_seek_start, 0);
buffer_write(global.buffer, buffer_u8, 0);
buffer_write(global.buffer, buffer_string, "username");
Send();
break;
case INCORRECT_PASSWORD:
show_message("your password is incorrect");
break;
case INCORRECT_USERNAME:
show_message("your username is invalid");
break;
}
}
else {
callback = noone;
}
}
The string_digits returns a string, not a numeric value. So for example, if your string says "1", the returned value will be 49, which is the ASCII code for "1".
I believe that your macros are assigned to numbers (1, 2, 3 ...) and not to their ASCII code ("1", "2", "3" ... = 49, 50, 51 ...). This could explain why the cases are not triggered.
If you are sure that the switch is read, try putting a "default" at the end of the cases. It will be triggered if the cases were not. You could use it to display the value of "answer", for example, and verify that it is what you want.
So I have some code running an IP check to ensure an ADMIN account cannot have access from outside my network.
string strIP = Request.ServerVariables["REMOTE_ADDR"];
if (
(strIP.Substring(0, 9) != "XXX.XX.X.")
&& (strIP.Substring(0, 10) != "XXX.XX.XX.")
&& (strIP.Substring(0, 6) != "XX.XX.")
&& (strIP.Substring(0, 6) != "XX.XX.")
)
{
..// Check user for being an ADMIN // ....
}
This code was working fine for several weeks, but suddenly has started consistently to err out. The error message is:
Exception
Exception Type: System. ArgumentOUtOfRangeException
Exception Message: Index and length must refer to a location within the string. Parameter name: length.
When I remove the line with "Substring(0,10)", everything works. Also, when I change the line "Substring(0,10)" to "Substring(0,9)" and remove the last ".", everything works.
Can anyone tell me why or perhaps instruct on what is being done incorrectly? For the life of me I can't figure out what is going on.
The problem is that strIP doesn't have 10 characters because your configuration changed for some reason. You could do something like:
(strIP.Length >= 9 && strIP.Substring(0, 9) != "XXX.XX.X.")
|| (strIP.Length >= 10 && strIP.Substring(0, 10) != "XXX.XX.XX.")
|| (strIP.Length >= 6 && strIP.Substring(0, 6) != "XX.XX.")
Notice that the fourth line was a duplicate of the third.
Do not allow an out of bounds error to happen by putting a check for length of strIP before you try to do any of the sub-string comparisons, like this:
if (strIP.Length == 10)
{
if ((strIP.Substring(0, 9) != "XXX.XX.X.")
&& (strIP.Substring(0, 10) != "XXX.XX.XX.")
&& (strIP.Substring(0, 6) != "XX.XX.")
&& (strIP.Substring(0, 6) != "XX.XX."))
{
..// Check user for being an ADMIN // ....
}
}
else
{
// Do something here, error, message to user, deny access, etc.
}
UPDATE:
If you want to only apply checks, based upon the length of the string, then use a switch statement, like this:
switch (strIP.Length)
{
case 6:
if(strIP.Substring(0, 6) != "XX.XX.")
{
// Check user for being an ADMIN
}
break;
case 9:
if(strIP.Substring(0, 9) != "XXX.XX.X.")
{
// Check user for being an ADMIN
}
break;
case 10:
if(strIP.Substring(0, 10) != "XXX.XX.XX.")
{
// Check user for being an ADMIN
}
break;
default:
// IP string is not in format expected, do something here
// Most likely would want to err on the side of caution and deny access
break;
}
I have a switch statement in my action script class.I have a function onKeyDown(event: KeyboardEvent) which contain a switch statement.which control the different keys.
But now I want to handle the same functionality which TAB key is doing using two keys CTRL and SPACE key.
I want add a case more in my switch statement first I pressed CTRL and then SPACE key then a a specific function is called.
Anyone can tell me how two keys used in my switch statement?
private function onKeyDown(event: KeyboardEvent) : void{
if (popUp.displayPopUp){
switch (event.keyCode){
case Keyboard.UP:
case Keyboard.DOWN:
case Keyboard.END:
case Keyboard.HOME:
case Keyboard.PAGE_UP:
case Keyboard.PAGE_DOWN:
inputTxt.selectRange(text.length, text.length)
list.dispatchEvent(event)
break;
case Keyboard.ENTER:
acceptCompletion();
break;
case Keyboard.TAB:
if (requireSelection)
acceptCompletion();
else
popUp.displayPopUp = false
break;
case Keyboard.ESCAPE:
popUp.displayPopUp = false
break;
case Keyboard.CONTROL && Keyboard.SPACE:
if (requireSelection)
acceptCompletion();
else
popUp.displayPopUp = false
break;
}
}
}
This is a pretty common problem.
event.keyCode is only going to give you the keyCode for the most recent key pressed. So you will have to store key presses and key releases. Fortunately, AS3 is a pretty sweet Array implementation that allows this to be done easily.
var _keys:Array;
addEventListener(KeyboardEvent.KEY_DOWN, keyDownEvent);
addEventListener(KeyboardEvent.KEY_UP, keyUpEvent);
function keyDownEvent(e:KeyboardEvent)
{
// turn the key on
_keys[e.keyCode] = true;
// perform logic
keyLogic()
}
function keyUpEvent(e:KeyboardEvent)
{
// turn the key off
_keys[e.keyCode] = false;
}
function keyLogic()
{
// this is where the actual logic is
if(_keys[Keyboard.SPACE] && _keys[Keyboard.CONTROL])
{
if (requireSelection)
acceptCompletion();
else
popUp.displayPopUp = false
}
}
This allows you to have a neat little array that always tells you what keys are down. It also separates your program logic from your input logic. Double win.
In your case you can just check ctrlKey property on KeyboardEvent. But #DingoEatingFuzz's answer will allow check combinations like 'space' + 'letter' and so on.
case Keyboard.SPACE:
if (event.ctrlKey)
{
}
break;
i think this one approch is good........