<commit_before>import _                          from 'lodash';
import baseSerializer             from './base';
import { scrub, buildChoiceText } from '../../serializer_utils';
import genusTypes                 from '../../../../constants/genus_types';
import guid                       from '../../../../utils/guid';

function serializeChoices(originalChoices, newChoiceAttributes, inlineRegionId) {
  const choices = _.map(originalChoices, (choice) => {
    const updateValues = newChoiceAttributes[choice.id];

    return {
      id: choice.id,
      text: buildChoiceText(
        _.get(updateValues, 'text') || choice.text,
        _.get(updateValues, 'wordType') || choice.wordType || 'other',
      ),
      delete: _.get(updateValues, 'delete'),
    };
  });

  if (newChoiceAttributes.new) {
    choices.push({
      id: guid(),
      text: '',
      order: choices.length,
    });
  }

  return { [inlineRegionId]: { choices } };
}

function serializeQuestion(originalQuestion, newQuestionAttributes, questionString) {
  const newQuestion = {
    inlineRegions: null,
  };
  const inlineRegionId = originalQuestion.inlineRegionId || guid();

  if (newQuestionAttributes.choices) {
    newQuestion.inlineRegions = serializeChoices(
      originalQuestion.choices,
      newQuestionAttributes.choices,
      inlineRegionId
    );
  } else if (!originalQuestion.inlineRegionId) {
    newQuestion.inlineRegions = { [inlineRegionId]: { choices: [] } };
  }

  if (questionString) {
    const newQuestionString = _.cloneDeep(questionString);
    newQuestionString.text = _.map(newQuestionString.text.split(' '), (word) => {
      if (word === '[_]') {
        return `<inlineChoiceInteraction id="${inlineRegionId}"></inlineChoiceInteraction>`;
      }
      return buildChoiceText(word, 'other');<commit_msg>accept [ _ ] for blank as well (#274)<commit_after>import _                          from 'lodash';
import baseSerializer             from './base';
import { scrub, buildChoiceText } from '../../serializer_utils';
import genusTypes                 from '../../../../constants/genus_types';
import guid                       from '../../../../utils/guid';

function serializeChoices(originalChoices, newChoiceAttributes, inlineRegionId) {
  const choices = _.map(originalChoices, (choice) => {
    const updateValues = newChoiceAttributes[choice.id];

    return {
      id: choice.id,
      text: buildChoiceText(
        _.get(updateValues, 'text') || choice.text,
        _.get(updateValues, 'wordType') || choice.wordType || 'other',
      ),
      delete: _.get(updateValues, 'delete'),
    };
  });

  if (newChoiceAttributes.new) {
    choices.push({
      id: guid(),
      text: '',
      order: choices.length,
    });
  }

  return { [inlineRegionId]: { choices } };
}

function serializeQuestion(originalQuestion, newQuestionAttributes, questionString) {
  const newQuestion = {
    inlineRegions: null,
  };
  const inlineRegionId = originalQuestion.inlineRegionId || guid();

  if (newQuestionAttributes.choices) {
    newQuestion.inlineRegions = serializeChoices(
      originalQuestion.choices,
      newQuestionAttributes.choices,
      inlineRegionId
    );
  } else if (!originalQuestion.inlineRegionId) {
    newQuestion.inlineRegions = { [inlineRegionId]: { choices: [] } };
  }

  if (questionString) {
    const newQuestionString = _.cloneDeep(questionString);
    newQuestionString.text = _.map(newQuestionString.text.split(' '), (word) => {
      if (word === '[_]' || word === '[ _ ]') {
        return `<inlineChoiceInteraction id="${inlineRegionId}"></inlineChoiceInteraction>`;
      }
      return buildChoiceText(word, 'other');
{'fraction_non_alphanumeric': 0.14324618736383443, 'fraction_numerical': 0.0008169934640522876, 'mean_word_length': 2.9367631296891745, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7662276', 'n_tokens_mistral': 1031, 'n_tokens_neox': 1011, 'n_words': 201}