contradb / contra

Find and share contra dances.
http://contradb.com
GNU Affero General Public License v3.0
13 stars 5 forks source link

Translator from Cary Ravitz html format to ContraDB format #106

Open dcmorse opened 8 years ago

dcmorse commented 8 years ago

See the dances at http://www.dance.ravitz.us/ and imagine screen-scraping them to contradb format.

jmdyck commented 7 years ago

By "ContraDB format", do you mean a textual format that can be easily imported into ContraDB? If so, is that format documented?

dcmorse commented 7 years ago

@jmdyck the hard part is translating the figures. Internally, the db stores a string of JSON that encodes the figures. The code is written in a declarative style in JS in figure.es6

defineFigure( "circle", [param_spin_left,  param_four_places, param_beats_8])

You can see that for the figure 'circle', the json is going to encode three parameter values: which way to spin the circle (defaults to left not right), what angle to spin the circle (defaults to four places), and how many beats to do it (defaults to 8).

The general strategy for the one importer I've written was:

  1. figure out what move we're looking at
  2. call up the parameters needed to encode that figure (for circle that'd be spin, places, and beats, for other figures it's different)
  3. fish those values out of the other format using sorcery specific to that format. ....
  4. profit!

Still waiting on the 4 bit. ;)

PS: who are you?

jmdyck commented 7 years ago

the hard part is translating the figures.

Probably, but if I don't know what to translate them to, 'hard' becomes 'impossible'.

The defineFigure() calls help, but they don't tell me how to JSON-encode a particular circle figure (e.g., the ubiquitous circle left 3/4), or all the figures in a sequence, or all the properties of a dance.

PS: who are you?

Michael Dyck, responsible for http://www.ibiblio.org/contradance/index/

cranhandler commented 7 years ago

Neat, I was looking at your index this morning! On May 13, 2017 2:27 PM, "jmdyck" notifications@github.com wrote:

the hard part is translating the figures.

Probably, but if I don't know what to translate them to, 'hard' becomes 'impossible'.

The defineFigure() calls help, but they don't tell me how to JSON-encode a particular circle figure (e.g., the ubiquitous circle left 3/4), or all the figures in a sequence, or all the properties of a dance.

PS: who are you?

Michael Dyck, responsible for http://www.ibiblio.org/contradance/index/

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/dcmorse/contra/issues/106#issuecomment-301275855, or mute the thread https://github.com/notifications/unsubscribe-auth/APbjzXjbXZh-USzcl44_eTef7Viz_t6sks5r5iAogaJpZM4Hn3aw .

dcmorse commented 7 years ago

This part of contradb is called libfigure or jslibfigure, and it's pretty well stand-alone code in app/assets/javascripts/libfigure/

Here's a specific example of a dance: the figures of The Baby Rose (whitespace embellished for clarity):

[{"parameter_values":["neighbors",true,16],"move":"swing"},
 {"parameter_values":[true,270,8],"move":"circle"},
 {"parameter_values":["partners",true,360,8],"move":"do si do"},
 {"parameter_values":["partners",true,16],"move":"swing"},
 {"parameter_values":["ladles","across",8],"move":"chain"},
 {"parameter_values":[false,360,"",8],"move":"star","note":"to new neighbor"}]

In jslibfigure langauge, the above json represents 6 figures. Figures have moves - always simple strings, and the list of defined moves is given by the jslibfigure function moves(). You can also see in the last figure the presence of an optional note property. That's free text for the person/robot to encode information that isn't fully representable in the stifling libfigure format. Lastly figures have parameter_values.

In showing the figure definition for circle in the previous message, that showed the formal parameters to circle (which, due to notational sloppiness, are sometimes just called "parameters"). You can get a list of a given move's formal parameters by calling, e.g. parameters('swing')

If you look at the second line of The Baby Rose, there's our old friend circle, and you can get the gist of the encodings of the parameters: the circle direction is true if it's to the left - false if it's to the right. three places is encoded as 270 (degrees), and beats are encoded as an integer as well. But how to drill into the why?

Let's look at one of the parameters to circle. A good one to start with is the param_beats_8 one. That's defined in a file app/assets/javascripts/libfigure/param.js

var param_beats_8 = {name: "beats", value: 8, ui: chooser_beats, string: stringParamBeatsNotN(8)};

The name and string properties aren't important right now. The ui property determines which user interface widget the user interacts with to choose the value. We finally are coming to your encodings!

and then if you look in app/views/dances/_chooser.html.erb, you'll see a very repetitive file that defines ui chrome and encodings for each chooser. I'll extract chooser_beats

                <div ng-if="p.ui == chooser_beats">
                  <select
                     ng-model="figure.parameter_values[$index]" 
                     ng-options="b for b in [8,16,0,1,2,3,4,6,8,10,12,14,16,20,24,32,48,64]"
                     ng-change="user_changed_parameter(figure,$index)"
                     class='form-control'>
                  </select>
                </div>

Enh, after all, I picked a bad one to highlight. It's really angular. But it's a select box where both the label and the values are ints. Lemme pick another one.

                <div ng-if="p.ui == chooser_boolean">
                  <input type="checkbox" class="balance_edit" 
                         ng-model="figure.parameter_values[$index]"
                         ng-change="user_changed_parameter(figure,$index)"
                         class='form-control'/>
                </div>

Boolean choosers are implemented as a checkbox, and the values are true and false.

And here's another one - chooser_pairs, which selects how to group four dancers into two pairs of two:

                <div ng-if="p.ui == chooser_pairs">
                  <select ng-model="figure.parameter_values[$index]"
                          ng-change="user_changed_parameter(figure,$index)"
                          class='form-control'>
                    <option value="partners">partners</option>
                    <option value="neighbors">neighbors</option>
                    <option value="shadows">shadows</option>
                    <option value="same roles">same roles</option>
                  </select>
                </div>

If you squint at this, you'll see that partners is encoded as the string "partners" and so forth.

Is this helping?

jmdyck commented 7 years ago

Is this helping?

Yes, the "Baby Rose" example is quite helpful for this issue. (The HTML code not so much, I think.)

That shows how to textually represent the figures of a dance. But what about the other properties of a dance (title, author, formation)?

E.g., if I gave you

{
  "title": "The Baby Rose",
  "author": "David Kaynor",
  "formation": "improper",
  "figures": [
    {"parameter_values":["neighbors",true,16],"move":"swing"},
    ...
  ]
}

would you be able to easily import the data into your DB?

dcmorse commented 7 years ago

Something like that would be pretty easy to build, yeah.