how to get matrix itens by a count variable <? #entity[$count].literal ?> in Watson assistant (conversation) - ibm-watson

I need to get all user input literals from a entity array. For example,
The user input:
I want to see dolphins, an elephant and dogs
The entity:
{
"type": "synonyms",
"value": "animalcheck",
"synonyms": [
"dolphins",
"elephant",
"dogs",
"dog",
"dolphin",
"girafe"
]
}
So.. need the user inputs literal: ["dolphins","elephant","dogs"]
I'm trying with this:
<? #entity[$count].literal ?>
where $count=1 and will be incremented until reach #entity.values.size() but this $count inside brackets return error, it's not works.
Any suggestion?

I do say you seem to be making hard work for yourself. Is there any reason you cannot have an entity group that's its own list of animal types, i.e. animals, which itself contains the values ["dolphins","elephant","dogs"]. In this way, if a users question contains any of these values, they will be in the entity array animals:["dolphins","dogs"] etc.
Which in turn is easier to handle within assistant. There is nothing to stop you having both entity groups "animals" and "animalcheck".
(Although animalcheck seems to be your value, not sure what your entity is actually called. You example uses #entity - but sure that would be allowed as an entity name. )
Also to access your entity, say if it was called "entitylist", you would need to use;
See Docs; https://cloud.ibm.com/docs/services/assistant?topic=assistant-expression-language

The issue might be with the $count variable not being treated as an integer and only integer can be used to access an array in WA.
This expression will assure that the content of $count variable will be treated as an integer and hence be ok to access arrays in WA:
<? #entity[$count.toInt()].literal ?>.

Related

watson conversation entities array

I create one entities with a few fruits (apple, banana, orange, avocado)
When my user say any intent that I need to check if have one #Fruits work fine, but if my user say 2 or more fruits I need to save all in one array. how can I does this using slots? because in my test he save only the last (if I print $myFruits)
tks
When the user types two values or more, and this values was inside one entity, the values will be save inside array, and you can access the entity. For example...
You can see in my example, if I types two flavor's, will appear in my console the two values in one array...
Dialog:
Console:
So, if you want all values from the entity #fruits. you can use this method for saves inside one context variable (E.g: $fruits):
<? entities['fruits'][0].value + entities['fruits'][1].value ?> //if types two fruits
And for this to be shown in your dialog, you can use this method:
{
"output": {
"text": "This is the array: <? $fruits.join(', ') ?>"
}
}
The return will be:
This is the array: calabresa, marguerita,
If you want to access all values from your entity with code, you need to access the return from the calling message (for access entities, intents, context variables, etc), and use the following code:
var arrayEntitie = response.entities
for (var i=0; i < arrayEntitie.length; i++) {
if (arrayEntitie[i].name === 'calabreza') { //make your condition
//do something
}
}
Official documentation for accessing methods here.
You can see this Github repository by IBM Developer using context variables here.
Simple way to do is by using #EntityName.values . It will store all the values of given entity in context in form of array.

Use Blade Array/Objects like Twig

Is there a way to treat objects and arrays the same in Blade (like twig).
So that no matter what, you access the object and array with dot-notation, or even "->" notation would be fine. I would like to not have to differentiate between an object and an array. Twig allows this, but blade does not. I want to know if there is an extension/change I can make to blade to allow the same feature as twig.
For example an object containing an array:
DATA:
$object = [ (object)
part_1 => [
part_2 => [ (array)
array_1 => "test"
]
]
]
BLADE:
$object->part_1->part_2['array_1']
//returns "test"
$object->part_1->part_2->array_1
//fails "Trying to get property of non-object"
TWIG:
object.part_1.part_2.array_1
//returns "test"
Also explained as so:
Twig makes it easy to access variables using the dot notation.
This can be used on either a object or array.
In Blade, it’s the same as plain PHP.
BLADE ----------------- TWIG
$user->name -------> user.name
$user['name'] -------> user.name
To use "->" -notation, the variable has to be of PHP's type StdClass. An array (or associative array) is not. Although, you can easily cast it :)
$stdObj = (object) ["prop" => "value"];
As #Bryce said,
this will not work for multidimensional array
. However, you can find your answer of converting it here https://www.if-not-true-then-false.com/2009/php-tip-convert-stdclass-object-to-multidimensional-array-and-convert-multidimensional-array-to-stdclass-object/

Referencing multiple entities of the same type in a response

My user input has two different entity references of the same type and I'd like to be able to independently reference them, ideally in both condition checking and output. Is this possible?
For example a user might enter "between 1pm and 3pm" and conversation shows there's #sys-time:13:00:00 and #sys-time:15:00:00. I want to set a context variable $start and another $end. How do I individually reference the entities?
Tried the obvious and it works -> #sys-time[0] and #sys-time[1]. Though #sys-time doesn't appear to reference the array rather it looks like shorthand for #sys-time[0]. So solution is
Condition
#Fixture_Future AND #sys-time.length > 1
Response
{
"output": {
"text": "<fixtures></fixtures>"
},
“context”: {
“start”: #sys-time[0],
“end”: #sys-time[1]
},
}
and this works

In Puppet, can I realize a defined type for each element in an array?

After removing my variable names and everything extraneous for readability, my code comes down to this. I already set up a custom fact that returns an array of users that I need to set up some configuration files for. I'm trying to use a defined resource type and realize it with an array argument to do the configs for each user because Puppet lacks a basic for loop, so the code I have simplifies to this:
define modulename::pushconfigs{
user {"$name":
ensure => present
parameter => value
parameter => value
parameter => value
}
}
modulename::setconfigs{$::userlist: }
# $::userlist is an array of users, in the form [user1 user2 user3...]
for a couple parameters. However, when I try to run it, it says couldn't[do configs]for user user1 user2 user3. In other words, it's realizing the defined type only once, and it's trying to do so for a user whose name is the concatenated array.
How can I instead realize the defined type for each one in the array?
This should work...
define createuser{
user { $name :
ensure => present,
group => "gp",
home => "/home/$name",
shell => "/bin/bash",
}
}
$allusers = [ "user1", "user2", "user3", "user4" ]
createuser{$allusers:}
$::userlist is a string.
Its a fact, and facts are strings.
You need to turn it into an array before you can iterate over it.
If $::userlist = "user1,user2,user3"
$a_userlist=split($::userlist,',')
modulename::setconfigs{$a_userlist: }

Rendering a calculated value that is not in the database

I have a simple table with 3 values principle, interest, period. I want to show these 3 values
in a table along with calculated simple interest. This interest is for display only, wont be stored in the DB. How can i achieve this ? I know I can use Javascript to compute the interest and render it, but can I do it via Cakephp?
A few things you can do. One is to calculate it in the database. This is optimal if you can do it (IMO, I prefer to push as much data logic to the db as possible.) Of course, if you're doing complex calculations and database scalability is an issue you'll face soon, you can ease the load on your db by putting this in code.
Option 2 is to just calculate it before you display it. In the view, just write the PHP code to calculate what you need. This might be kinda hacky, depending on how frequently you need to display this (calculated) data.
Option 3 is to make that same calculation in the model. This is probably the most Cake-y way. Override your model's afterFind() method, and just run your calculations on the data retrieved. Add a new array index as needed. Then whenever any controller/view requests data from your model, said data will be returned with the calculated rows. Most likely this is how I'd implement it.
Check this link: Virtual fields
Basically you can define in your model virtual fields and you can use them as regular field in controller and view. You can use them in pagination too :)
You can do it in the SQL select statement, something like:
select Principle, Interest as [interest rate], Period,
principle * interest * period / 12 as [Interest Amount]
from your_table
When it gets back to PHP, this will look just like you'd selected from a table with four columns instead of three (i.e., the calculated value won't look any different from the others).
In the view:
<?php echo $a * $b / $c ?>
OR
In the controller:
$calcVal = $a * $b / $c;
$this->set('calcVal');
then in the view:
<?php echo $calcVal ?>
Or any of the options listed above.
For simple stuff where I have the values in the view anyway, I'd go with the first one. On the other hand, for a complex calculation using values that I would otherwise not have a use for in the view, I'd go with the second.
calculating from the database will be easier using the virtual fields
see this
http://book.cakephp.org/view/1588/virtualFields
http://book.cakephp.org/2.0/en/models/callback-methods.html#afterfind
class Account extends AppModel {
public $name = "Account";
public function afterFind( $results, $primary = false ){
parent::afterFind( $results, $primary );
if( $primary ){
foreach( $results as $key => $value ){
if( isset( $value[ $this->alias ] )){
// set the a new index for each row returned to the value of Account::__calculateSomething at the correct key
$results[ $key ][ $this->alias ][ 'calulated_field_name' ] = $this->__calculateSomething( $results[ $key ][ $this->alias ] );
}
}
}
return $results;
}
}
The just do your aggregating or caluculations in the __calulateSomething( $data ) function in the Account model.
This is just an example of course, but it shows how to use the $primary parameter that gets passed to the afterFind callback ( meaning the afterFind is being triggered for find calls made directly to this model, not as part of an association. )
You might need to check the format of the array - I didn't do that here.

Resources