<commit_before>require 'rubygems'
require 'sinatra'
require 'json'

WORDS = File.open("#{File.dirname(__FILE__)}/../words/sowpods.txt",'r') { |f| f.read.split("\r\n") }

def find_words_for(search_letters)
  WORDS.select do |word| 
    letters = search_letters.clone
    word.chars.all? do |char|
      if pos = letters.index(char)
        letters.delete_at(pos)
        true
      else
        false
      end
    end
  end.flatten.compact.sort {|a,b| a.length <=> b.length }.select {|w| w.length <= 8 }
end

get '/' do
  "Words!"
end

post "/find" do
  unless letters = params[:letters]
    halt 500, "Letters Missing"
  end
  find_words_for(letters.chars.map(&:upcase)).to_json
end
<commit_msg>small refactor<commit_after>require 'rubygems'
require 'sinatra'
require 'json'

WORDS = File.open("#{File.dirname(__FILE__)}/../words/sowpods.txt",'r') { |f| f.read.split("\r\n") }

# Finds words
#
# @param [Array] search_letters An array of letters to search
# @param [Fixnum] max_word_length the maximum word length to return
#
# Brute Force FTL
def find_words(words, search_letters, max_word_length = 8)
  words.select do |word| 
    if word.length > max_word_length
      false
    else
      letters = search_letters.clone
      word.chars.all? do |char|
        if pos = letters.index(char)
          letters.delete_at(pos)
          true
        else
          false
        end
      end
    end
  end.flatten.compact.sort {|a,b| a.length <=> b.length }
end

get '/' do
  "Words!"
end

post "/find" do
  unless letters = params[:letters]
    halt 500, "Letters Missing"
  end
  find_words(WORDS, letters.chars.map(&:upcase)).to_json
end

{'fraction_non_alphanumeric': 0.1364190012180268, 'fraction_numerical': 0.0048721071863581, 'mean_word_length': 2.9782082324455206, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '870172', 'n_tokens_mistral': 608, 'n_tokens_neox': 582, 'n_words': 140}