<commit_before># Internal dictionary: Definitions for certain words get automatically
# infused into the texts of other models.
class Definition < ActiveRecord::Base
  # Validations
  validates :key, presence: true, uniqueness: true, length: { maximum: 50 },
                  exclusion: { in: %w(dfn class JS tooltip) }
  validates :explanation, presence: true, length: { maximum: 500 }

  # Methods

  # Infuses from the list of all definitions into the given string
  def self.infuse string
    select(:id, :key).find_each do |definition|
      regex = /(\W)(#{definition.key})(\W)/i
      if string.match regex
        string.gsub! regex,
                     '\1<dfn class="JS-tooltip" data-id="' +
                       definition.id.to_s + '">\2</dfn>\3'
      end
    end
    string
  end
end<commit_msg>for #236 - slightly simplify regex<commit_after># Internal dictionary: Definitions for certain words get automatically
# infused into the texts of other models.
class Definition < ActiveRecord::Base
  # Validations
  validates :key, presence: true, uniqueness: true, length: { maximum: 50 },
                  exclusion: { in: %w(dfn class JS tooltip) }
  validates :explanation, presence: true, length: { maximum: 500 }

  # Methods

  # Infuses from the list of all definitions into the given string
  def self.infuse string
    select(:id, :key).find_each do |definition|
      regex = /\b(#{definition.key})\b/i
      if string.match regex
        string.gsub! regex,
                     "<dfn class='JS-tooltip' data-id='#{definition.id}'>"\
                     '\2</dfn>'
      end
    end
    string
  end
end
{'fraction_non_alphanumeric': 0.10974862047823421, 'fraction_numerical': 0.010423053341508276, 'mean_word_length': 2.6266666666666665, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '480882', 'n_tokens_mistral': 490, 'n_tokens_neox': 459, 'n_words': 158}