<commit_before>include ActionView::Helpers::SanitizeHelper
require 'hunspell-ffi'

namespace :admin  do
  desc "create a custom dictionary file with text from the articles"

  task :createdict => :environment do
    str = ''
    Article.all.each do |a|
      str << a.tags                  + ' ' +
             a.title                 + ' ' +
             strip_tags( a.content ) + ' ' +
             a.preview               + ' ' +
             a.category              + ' '
    end

    # replace control characters like \n and \t with a space
    str = str.gsub!(/[[:cntrl:]]+/, ' ')

    # remove puncutation 
    str = str.gsub!(/[[:punct:]]+/, '')

    # remove numbers
    str = str.gsub!(/[[:digit:]]+/, '')

    # remove single characters
    str = str.gsub!(/\s\S\s/, ' ')

    # downcase
    str = str.downcase

    ## save in hunspell format - OVERWRITES existing file
    custom = File.new( "#{Rails.root.to_s}/lib/assets/dict/custom/custom.dic", 'w')
    words = str.split.uniq!

    # file should begin with the word count to optimize hash bucket size etc.
    custom.puts words.size 
    words.each do |word|
      custom.puts word
    end
    custom.close()

    # ensure the file can be read by hunspell
    dict = Hunspell.new( "#{Rails.root.to_s}/lib/assets/dict/custom", 'custom')
  end

  # TODO: create a rake task to update the dictionary file, 
  #       so that it doesn't have to be recreated each time
  
end
<commit_msg>minor tweak to the update_dict rake task<commit_after>include ActionView::Helpers::SanitizeHelper
require 'hunspell-ffi'

namespace :admin  do
  desc "create a custom dictionary file with text from the articles"

  task :createdict => :environment do
    str = ''
    Article.all.each do |a|
      str << a.tags                  + ' ' +
             a.title                 + ' ' +
             strip_tags( a.content ) + ' ' +
             a.preview               + ' ' +
             a.category              + ' '
    end

    # replace control characters like \n and \t with a space
    str = str.gsub!(/[[:cntrl:]]+/, ' ')

    # remove puncutation 
    str = str.gsub!(/[[:punct:]]+/, '')

    # remove numbers
    str = str.gsub!(/[[:digit:]]+/, '')

    # remove single characters
    str = str.gsub!(/\s\S\s/, ' ')

    # downcase
    str = str.downcase

    ## save in hunspell format - OVERWRITES existing file
    dict = File.new( "#{Rails.root.to_s}/lib/assets/dict/custom/custom.dic", 'w')
    words = str.split.uniq!

    # file should begin with the word count to optimize hash bucket size etc.
    dict.puts words.size 
    words.each do |word|
      dict.puts word
    end
    dict.close()

    # ensure the file can be read by hunspell
    Hunspell.new( "#{Rails.root.to_s}/lib/assets/dict/custom", 'custom')
  end

  # TODO: create a rake task to update the dictionary file, 
  #       so that it doesn't have to be recreated each time
  
end

{'fraction_non_alphanumeric': 0.1281786941580756, 'fraction_numerical': 0.0, 'mean_word_length': 2.0103412616339194, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '2613866', 'n_tokens_mistral': 944, 'n_tokens_neox': 918, 'n_words': 267}