Author Topic: Grabbing values from a "Nil" subscript?  (Read 121 times)

Jingo

  • Super Hero
  • ****
  • Posts: 1036
Grabbing values from a "Nil" subscript?
« on: July 06, 2019, 06:13:55 PM »
Hi Mario (and all).... I'm trying to grab some data from an extended nested array (ie: thesaurus/paths) and build a table of the results (basically a keyword/synonym pair) - it looks like this:

matches: (2) […]
​0: {…}
  elems: (4) […]
​​​     0: {…}
​​​     1: {…}
​​​​       elems: (1) […]
​​​​       paths: {…}
          "": "Cuba"
   ​​​​    synonyms: (1) […]
​​​​​          0: {…}
​​​​​​             "": "Cuba|Cuban"

I have no issue grabbing the elements from matches[0].elems[1].paths[0] - gives me "Cuba".  However, how do you access the data from synonyms?  Ie: matches[0].elems[1].synonyms[0] gives me the value {"":  "Cuba|Cuban"} when I just want to grab the nil element "Cuba|Cuban".... I'm sure it is simple.. but my attempts such as:

matches[0].elems[1].synonyms[0][""], matches[0].elems[1].synonyms[0]."", matches[0].elems[1].synonyms[0].NULL, etc all give a syntax error.  I also tried to convert it to a string and the split the string... but that too seemed to fail...

Thx Mario!

Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 22281
Re: Grabbing values from a "Nil" subscript?
« Reply #1 on: July 06, 2019, 06:20:22 PM »
Please show the original JSON. That looks more like something you have copied/pasted from a debugger?
synonyms is an array, like elems.

Jingo

  • Super Hero
  • ****
  • Posts: 1036
Re: Grabbing values from a "Nil" subscript?
« Reply #2 on: July 06, 2019, 06:28:39 PM »
That was pulled from the debugger as a watch expression - Here it is from the console...


Code: [Select]
"elems": [
      {
        "paths": {
          "": "Cuba"
        },
        "synonyms": [
          {
            "": "Cuba|Cuban"
          }
        ],
        "elems": [
          {
            "paths": {
              "": "Cuba|south Cuba"
            },
            "synonyms": [
              {
                "": "Cuba|south Cuba|southern Cuba"
              }
            ],
            "elems": [
              {
                "paths": {
                  "": "Cuba|south Cuba|south coast"
                },
                "synonyms": [],
                "elems": [
                  {
                    "paths": {
                      "": "Cuba|south Cuba|south coast|Cuban south coast place"
                    },
                    "synonyms": [],
                    "elems": [
                      {
                        "paths": {
                          "": "Cuba|south Cuba|south coast|Cuban south coast place|Cuban beach resort"
                        },
                        "synonyms": []
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      },



I know I'm just missing something really easy/silly..... Thx!
« Last Edit: July 06, 2019, 06:36:56 PM by Jingo »

Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 22281
Re: Grabbing values from a "Nil" subscript?
« Reply #3 on: July 06, 2019, 06:51:10 PM »
synonyms are an array of objects. A keyword can have any number of synonyms, optionally (rarely) in multiple languages.
Each synonym object contains a map of language=word pairs.

Remember that the thesaurus is hierarchical and nested. You need to recurse down, iterating over all elements on all levels
Here is a quick example (I've dumped your data into a JSON file, loaded it and then iterated over it):



The dumpElem function "processes" the data (in whatever way you need) and then calls itself recursively to process child elements.


Since you already have the JSON in memory, you would start with the line below "Begin to iterate..."
« Last Edit: July 06, 2019, 06:53:30 PM by Mario »

Jingo

  • Super Hero
  • ****
  • Posts: 1036
Re: Grabbing values from a "Nil" subscript?
« Reply #4 on: July 06, 2019, 07:23:55 PM »
synonyms are an array of objects. A keyword can have any number of synonyms, optionally (rarely) in multiple languages.
Each synonym object contains a map of language=word pairs.

Remember that the thesaurus is hierarchical and nested. You need to recurse down, iterating over all elements on all levels
Here is a quick example (I've dumped your data into a JSON file, loaded it and then iterated over it):



The dumpElem function "processes" the data (in whatever way you need) and then calls itself recursively to process child elements.


Since you already have the JSON in memory, you would start with the line below "Begin to iterate..."

Thanks Mario... I knew it was something silly.... matches[0].elems[1].synonyms[0]['']... I was using double quotes!!  Many thx!!

Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 22281
Re: Grabbing values from a "Nil" subscript?
« Reply #5 on: July 06, 2019, 07:36:08 PM »
That's usually irrelevant, JS supports both.

Jingo

  • Super Hero
  • ****
  • Posts: 1036
Re: Grabbing values from a "Nil" subscript?
« Reply #6 on: July 06, 2019, 10:56:59 PM »
Thx Mario - I have it working now...!

For those curious - I am just playing with an app idea that would let a user choose a keyword from the thesaurus, show all synonyms for subkeywords in that same branch and then let you add the sub-synonyms.  From a recent discussion in the forums (https://www.photools.com/community/index.php?topic=9125.0), this functionality does not exist but I think I can accomplish it in an app..... just playing around to see what I can present to the user... and so far, I have a list of all keywords/synonyms displaying in a table... so, I just need to add the part where I locate the keywords in the array and write it out as a keyword to the images.

Stay tuned!!

Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 22281
Re: Grabbing values from a "Nil" subscript?
« Reply #7 on: July 07, 2019, 09:33:13 AM »
Very well.

But that feature does not exist for good reasons, because synonyms on "sub-levels" would become real keywords, probably messing up the re-import and mapping.

Jingo

  • Super Hero
  • ****
  • Posts: 1036
Re: Grabbing values from a "Nil" subscript?
« Reply #8 on: July 07, 2019, 01:45:46 PM »
Very well.

But that feature does not exist for good reasons, because synonyms on "sub-levels" would become real keywords, probably messing up the re-import and mapping.

Not sure I follow Mario...  if you have keywords and synonyms setup as follows:

   Country ->  USA  -> State  -> Texas
                     |                         |
                    America            Bluebonnet State   
                    US

If you currently add Texas as a keyword and things are setup as such, you will get Texas, Bluebonnet State, State, USA  set to an image.  The App would also assign America and US as keywords because they are synonyms or a branch keywords being added.   During the "re-import/mapping" process, I would think IM would see these as flat keywords and not need to do anything further? 

I certainly don't want to create an app which will then break something in the system - this is one of the main obstacles I face all the time about creating IM Apps.  Since I don't fully know the inner workings of IM, sometimes creating something that seems "reasonable" or innocuous can indeed wreck havoc!
               

Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 22281
Re: Grabbing values from a "Nil" subscript?
« Reply #9 on: July 07, 2019, 02:09:08 PM »
Just keep in mind that synonyms become normal keywords. Synonyms are something that exists only in the IMatch thesaurus.
You want to avoid creating 'new' hierarchical keywords by adding synonyms from other levels. I don't know what your app does but I'm sure you will test it. I don't look at, use, test or endorse apps written by users.

A synonym for the keyword "Country|USA" is not the same as a synonym for the keyword ""Country|USA|State|Texas", which is why IMatch always assigns the synonyms of the assigned keyword, but not goes up the hierarchy path to assign other keywords or synonyms. And consider the mapping and re-mapping between flat and hierarchical keywords, which works on the level the keyword/synonym was found in the thesaurus. If you patch all synonyms 'found' on the same level as the keyword assigned, this could work, probably.
« Last Edit: July 07, 2019, 02:15:33 PM by Mario »