Wipe / Delete / Clear Simple Response Text to Replace (not Concatenate) - dialogflow-es

In the nodejs actions on google library, calling .add(text), .ask(text), and .close(text) on the conversation concatenates that text onto the simple text response, i.e.
conv.add('hello');
conv.close('world');
// yields text response "hello world"
This is normally just fine. But, I'm hitting a case where I need to clear out everything that I've added onto the simple response text to replace it with some error handling response, i.e.
conv.add('hello');
// something went wrong, need to clear "hello" and replace
conv.ask('sorry, could you repeat your command?');
// yields text reponse "sorry, could you repeat your command?"
// *does not* yield "hello sorry, could you repeat your command?"
I cannot find a function on the conversation object that achieves this behavior. Is there a way I can manually clear out the responses in order to replace them with something new?

It looks like I can manually access the Response[] array on the conversation object and clear it out with an empty array. So, in the case of my example, I could do:
conv.add('hello');
conv.responses = []; // clears out the previously added "hello"
conv.ask('sorry, could you repeat your command?');
// yields text response "sorry, could you repeat your command?"

Related

NodeJS why is object[0] returning '{' instead of the first property from this json object?

So I have to go through a bunch of code to get some data from an iframe. the iframe has a lot of data but in there is an object called '_name'. the first key of name is 'extension_id' and its value is a big long string. the json object is enclosed in apostrophes. I have tried removing the apostrophes but still instead of 'extension_id_output' I get a single curly bracket. the json object looks something like this
Frame {
...
...
_name: '{"extension_id":"a big huge string that I need"} "a bunch of other stuff":"this is a valid json object as confirmed by jsonlint", "globalOptions":{"crev":"1.2.50"}}}'
}
it's a whole big ugly paragraph but I really just need the extension_id. so this is the code I'm currently using after attempt 100 or whatever.
var frames = await page.frames();
// I'm using puppeteer for this part but I don't think that's relevant overall.
var thing = frames[1]._name;
console.log(frames[1])
// console.log(thing)
thing.replace(/'/g, '"')
// this is to remove the apostrophes from the outside of the object. I thought that would change things before. it does not. still outputs a single {
JSON.parse(thing)
console.log(thing[0])
instead of getting a big huge string that I need or whatever is written in extension_id. I get a {. that's it. I think that is because the whole object starts with a curly bracket. this is confirmed to me because console.log(thing[2]) prints e. so what's going on? jsonlint says this is a valid json object but maybe it's just a big string and I should be doing some kind of split to grab whaat's between the first : and the first ,. I'm really not sure.
For two reasons:
object[0] doesn't return the value an object's "first property", it returns the value of the property with the name "0", if any (there probably isn't in your object); and
Because it's JSON, and when you're dealing with JSON in JavaScript code, you are by definition dealing with a string. (More here.) If you want to deal with the object that the JSON describes, parse it.
Here's an example of parsing it and getting the value of the extension_id property from it:
const parsed = JSON.parse(frames[1]._name);
console.log(parsed.extension_id); // The ID

Is there a way to use the result of last run in the current run in Shiny?

I am using Shiny to build a dynamic dashboard for the user. I used if statement to divide my code into two main parts. There is an input called "New Line" that user needs to enter "Yes" or "No" as the value for this parameter. If user selects "Yes" the first part of the code will be ran and if he selects "No", it means that the first part is ran before and just the second part of the code needs to be ran. In the second part, there are some parameters from the first part. I want for Shiny to save the results and parameters calculate from ("New Line"==Yes) and use it for the next runs("New Line"==No) until the user change the answer for "New Line" to "Yes" again.
I used observeEvent function and an action button in Shiny. But the problem is that the results of ("New Line"==Yes) can not be used for the next runs.
shinyserver(function(input,output)){
observeEvent(input$click,{
if(input$'New Line' == 'Yes'){}
if(input$'New Line' == 'No'){}
})
}
I expect whenever user enters No for the New Line and hits the action button, the code can use the variables that are calculated when the New Line was Yes. But now whenever I set New Line as No and hit the action button , R just runs the second if. I can copy and paste all the lines from first if to tell R to run all of them again but those line takes about 30 minutes and I do not want to repeat them.
You need to serialise your values somewhere.
One example:
shinyserver(function(input,output)){
observeEvent(input$click,{
if(input$'New Line' == 'Yes'){
data <- result_of_some_function()
some_other_things()
write.csv(data, "file.csv") # saving the data somewhere on disk
}
if(input$'New Line' == 'No'){
mypreviousdata = read.csv("file.csv")
something <- somefunction(mypreviousdata)
}
})
}
Other than saving to CSV, you might prefer a database or the user's cookies, or whatever.

How to get first word from sentence in Swift

I am trying to get first word from String sentence as variable. How to do that? I have Local notification and I need to use it's message first word as variable, is it possible? For LocalNotifications I use LocalNotificationHelper library for easier handling. Maybe there is logic problem but I do not think so. More like I do not know enough options in Swift language.
Edit: I also have the nameField.text! in NSUserDefaults as an array. I need to delete the message first word from that array. Right now I remove only the first object from that array but that is not solution for me because I need to delete the nameField.text! from that array when LocalNotification pops out and user click on button.
Here I create notification message:
LocalNotificationHelper.sharedInstance().scheduleNotificationWithKey("text", title: "see options(left)", message: nameField.text!+"some text", date: deadlinePicker.date, userInfo: userInfo)
Now I need the message:nameField.text! to be variable at app launch.
This is how I trigger notification button actions:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "someFunction:", name: IDENTIFIER, object: nil)
Swift 3
let sentence = "First word needed"
let word = sentence.components(separatedBy: " ").first
print(word) // First
You can split your sentence using the white space char and then extracting the first element.
let sentence = "What a wonderful world"
if let firstWord = sentence.characters.split(" ").first.map(String.init) {
print(firstWord) // "What"
}

Is there any way when I choose an option not to clear previously typed data in the input

The problem is this:
In my programme at first the user gets options for a first name - so hopefully he likes something from the options and he chooses it -so far everything is OK!
But then when he types space he starts receiving options for second name and a if he likes something and chooses it - then the Autocomplete just erases the first name. Is there any way I can change that?
hello Rich thank you very much or your response - now i've decided to change my task and here is what I made when a user types for example I character i get all the first names that start with I- so far no problem! ANd when he types the white space and K for example I make request to my web service that gets the middle names that starts with K or the last names that start with K (one of them should start with K for Iwelina), so in this case for Iwelina Ive got RADULSKA KOSEWA and KOSEWA NEDEWA! For the source of autocomplete I concatenate iwelina with (radulska kosewa)and iwelina with (KOSEWA NEDEWA) so at the end I've got IWELINA IELINA RADULSKA KOSEWA and IWELINA KOSEWA NEDEWA!!! the only problem is that when i type Iwelina K i get only IWELINA KOSEWA NEDEWA!!!here is the code for autocomlete
$('#input').autocomplete({
source: function(request, response) {
var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term, " "));
var data = $.grep( srcAutoComp, function(value) {
return matcher.test( value.label || value.value || value );
});
response(data);
}
});
if you know how i can change it I will be glad for the help
I don't understand how, when the user begines to type the second name, he's getting results that are only the last name. For example, if he types "Joh" and selects "John" from the options, and then continues to type "John Do", then how is it possible that your drop down gives him results for only the last name, like "Doe"?
At any rate, assuming this is truly happening, you could just combine all combinations of first and last names in your source data and that will show "John Doe" in the drop down when the user types "Joh" selects "John" and then continues to type "John Do".
Another way to do this is with a complicated change to the search and response events to search after a space if it is there, and recombine it with the first string after the search for the last name is complete. If you give me your source data, I could put something together for this.

In Watir, how to get the full text, from a portion of text?

I have a portion of HTML that looks similar to:
<table><tbody><tr>
<td><div> Text Goes Here </div></td>
<td> ... rest of table
There are no IDs, no Titles, no descriptors of any kind to easily identify the div that contains the text.
When an error occurs on the page, the error is inserted into the location where "Text Goes Here" is at (no text is present unless an error occurs). Each error contains the word "valid".
Examples: "The form must contain a valid name" or "Invalid date range selected"
I currently have the Watir code looking like this:
if browser.frame(:index => 0).text.includes? "valid"
msg = # need to get full text of message
return msg
else
return true
end
Is there any way to get the full text in a situation like this?
Basically: return the full text of the element that contains the text "valid" ?
Using: Watir 2.0.4 , Webdriver 0.4.1
Given the structure you provided, since divs are so often used I would be inclined to look for the table cell using a regular expression as Dave shows in his answer. Unless you have a lot of nested tables, it is more likely to return just the text you want.
Also if 'valid' may appear elsewhere then you might want to provide a slightly larger sample of the text to look for
. browser(:cell => /valid/).text
Try this
return browser.div(:text => /valid/).text
or
return browser.table.div(:text => /valid/).text
if the valid is not found, it should return nil.

Resources