I would like to write a custom constructor for the ChoiceDict class in the Matrix’s Choices.py (I am working with a copy of that for MOM but I could eventually merge it into the Matrix’s copy if it seems useful).

Currently, ChoiceDict, which it the outer structure for any choices file as it gets loaded, can either be initialized empty or from a choices file. If it is initialized from a choices file, then it is assumed that on the LHS of every line there will be a full dictionary key (such as “verb-pc1_lrt1_lri1_feat1_name”).

I need a constructor which would accept a python dictionary (to remind, a ChoiceDict inherits from dict but is not a dict; it does various clever things with counting (and by the same token with adding and removing items) which drive me mad but I think this is needed for proper interaction with the website or something; similarly a ChoiceList inherits from list but is not a list).

I wrote an iterative function which assumes the morphotactics choices structure as the deepest: a position class (a dict) can contain a list of lexical rule types which can in turn contain lists of things (features and instances). So, three-level deep.

Questions:

(1) Is there a deeper possible structure?

(2) What would be the base case if I wanted to write a recursive function? I first thought that the deepest level won’t have a “name” attribute but for example features do… Perhaps it’s just the fact that “there are no more lists among the members of this dict”? Doesn’t sound like a very satisfying base case…

When I joined the Matrix project, one of my first changes was to parse the choices into a nested structure (roughly as we have now), but while keeping the original choices file format. In order to handle the dictionary-type access (choices.get() above) and the list enumeration (choices.iter_begin(), above) we made the ChoiceDict and ChoiceList classes and changed their basic access methods to be aware of the existing structure in the choices file format. In retrospect I should have pushed harder to just switch to a more standard format like JSON (see here for my thoughts at the time), but oh well.

Anyway, I guess I’m not sure what you’re trying to do now. You want to initialize something like a partial choices file? Or you want to create a ChoiceDict from the nested structure and not from the full keys?

I am trying to initialize from JSON, as it happens :). From dict, but I get that by loading JSON.

We’ve been handing a JSON serialization to the MOM GUI, and assuming something might have been changed by the user via that GUI, we would like to be able to load the result back. Obviously we don’t want to manipulate strings; having a dict which can be serialized directly as JSON is much more preferable. So, I end up with a dict which I loaded from JSON which came to me from the GUI, and I need to turn that into a choices file.

I think I succeeded with the above, though I haven’t thoroughly tested it.