Ejemplo n.º 1
0
var postProcessModel = function (results, model, topics) {
  var num;
  var formattedNumber;
  var item;
  //console.log("POST: %s", model);
  var topicReference = getNextTopicReference(model, true);
  if (topicReference) {
    var topicReferenceKey = topicReference;
    var topicCategory = (topics || {})[topicReferenceKey];
    if (topicCategory) {
      if (topicCategory.type === 'dictionary') {
        var topicIndex = Math.floor((Math.random() * topicCategory.entries.length));
        var topic = topicCategory.entries[topicIndex];
        var topicValues = _.isObject(topic) ? (_.isArray(topic) ? topic : (_.isArray(topic.values) ? topic.values : [topic])) : [topic];
        var valueIndex = Math.floor((Math.random() * topicValues.length));
        var topicValue = topicValues[valueIndex];
        item = replaceTopicReferenceWithValue(model, topicReference, topicValue, true);
        //results.push(item);
        postProcessModel(results, item, topics);
      }
      else if (topicCategory.type === 'number') {
        var min = topicCategory.min || 0;
        var max = topicCategory.max || 9;
        var format = topicCategory.format || 'decimal';
        num = Math.floor((Math.random() * (max - min))) + min;
        if (format === 'spelled') {
          formattedNumber = spoken.toSpoken(num, 'dd:o');
        }
        else {
          formattedNumber = num;
        }
        item = replaceTopicReferenceWithValue(model, topicReference, formattedNumber, true);

        //console.log('$: %s\n%s', topicReference, item);
        //results.push(item);
        postProcessModel(results, item, topics);
      }
    }
  }
  else {
    var newModel = model.replace(/\(/g, '{').replace(/\)/g, '}').replace(/@/g, '');
    results.push(newModel);
  }
};
Ejemplo n.º 2
0
var bindModel = function (results, model, topics, isFinal) {
  var num;
  var formattedNumber;
  var count;
  var item;
  var i;

  var topicReference = getNextTopicReference(model);
  if (topicReference) {
    var topicReferenceKey = (topicReference[0] === '{') ? topicReference.slice(1, topicReference.length - 1) : topicReference;
    var topicCategory = (topics || {})[topicReferenceKey];
    if (topicCategory) {
      if (topicCategory.random) {
        count = topicCategory.count || 1;
        for (i = 0; i < count; i++) {
          item = replaceTopicReferenceWithValue(model, topicReference, '*');
          bindModel(results, item, topics, isFinal);
          //results.push(item);
        }
      }
      else {
        if (topicCategory.type === 'dictionary') {
          count = topicCategory.count || 0;
          var prob = count ? (count / ((topicCategory.entries || []).length || 1)) : 1.0;
          _.each(topicCategory.entries, function (topic) {
            var topicValues = _.isObject(topic) ? (_.isArray(topic) ? topic : (_.isArray(topic.values) ? topic.values : [topic])) : [topic];
            _.each(topicValues, function (topicValue) {
              if (prob >= Math.random()) {
                item = replaceTopicReferenceWithValue(model, topicReference, topicValue);
                bindModel(results, item, topics, isFinal);
              }
            });
          });
        }
        else if (topicCategory.type === 'custom') {
          item = replaceTopicReferenceWithCustomSlot(model, topicReference);
          bindModel(results, item, topics, isFinal);
        }
        else if (topicCategory.type === 'number') {
          var min = topicCategory.min || 0;
          var max = topicCategory.max || 9;
          var step = topicCategory.step || 1;
          count = topicCategory.count || 0;
          var format = topicCategory.format || 'decimal';
          if (count > 0) {
            var randomNumbers = [];
            for (i = 0; i < count; i++) {
              num = Math.floor((Math.random() * (max - min))) + min;
              if (randomNumbers.indexOf(num) < 0) {
                randomNumbers.push(num);
                if (format === 'spelled') {
                  formattedNumber = spoken.toSpoken(num, 'dd:o');
                }
                else {
                  formattedNumber = num;
                }
                item = replaceTopicReferenceWithValue(model, topicReference, formattedNumber);
                bindModel(results, item, topics, isFinal);
              }
            }
          }
          else {
            for (num = min; num <= max; num += step) {
              if (format === 'spelled') {
                formattedNumber = spoken.toSpoken(num, 'dd:o');
              }
              else {
                formattedNumber = num;
              }
              item = replaceTopicReferenceWithValue(model, topicReference, formattedNumber);
              bindModel(results, item, topics, isFinal);
            }
          }
        }
      }
    }
    else {
      if (topicReference[0] === '@') {
        bindModel(results, model.replace('{' + topicReference + '}', '(' + topicReference + ')'), topics, isFinal);
      }
      else {
        bindModel(results, model.replace('{' + topicReference + '}', topicReference), topics, isFinal);
      }
    }
  }
  else {
    results.push(model);
  }
  //console.log(results);
};