<commit_before>#!/usr/bin/env ruby

require 'register_client_manager'
require 'fuzzy_match'
require 'tty-prompt'

OPT_NONE_OF_THE_ABOVE = "None of the above"

client_mgr = RegistersClient::RegisterClientManager.new({
  page_size: 5000
})

initial_input = ARGV.join(' ')

abort "Usage: #{$0} gov-dept-search-substring" if ARGV.empty?

prompt = TTY::Prompt.new(output: STDERR)

def sorted_matches(matchset, search_term)
  FuzzyMatch
    .new(matchset)
    .find_all_with_score(search_term)
    .map { |dept, _pair_dist, _lev_dist| dept }
end

def opts_or_none(opts)
  [OPT_NONE_OF_THE_ABOVE].concat(opts)
end

client = client_mgr.get_register('government-organisation', 'beta')
dept_names = client.get_records.map { |r| r.item.value['name'] }

selected_name = nil
input = initial_input
loop do
  break unless selected_name.nil?

  choice = prompt.enum_select(
    'Did you mean?',
    opts_or_none(sorted_matches(dept_names, input))
  )
  if choice != OPT_NONE_OF_THE_ABOVE
    selected_name = choice
    break
  end

  choice = prompt.ask("Couldn't find a department with a name close enough to '#{input}'. Try another name (or leave empty to quit)")

  if choice.nil?
    prompt.say('Stopping')
    exit(1)
  end

  input = choice
end

puts selected_name<commit_msg>Allow user to override find-gov-department-name.rb<commit_after>#!/usr/bin/env ruby

require 'register_client_manager'
require 'fuzzy_match'
require 'tty-prompt'

OPT_NONE_OF_THE_ABOVE = "None of the above"
OVERRIDE_MSG = '(ONLY PICK THIS IF YOU CANNOT FIND IT BELOW)'

client_mgr = RegistersClient::RegisterClientManager.new({
  page_size: 5000
})

initial_input = ARGV.join(' ')

abort "Usage: #{$0} gov-dept-search-substring" if ARGV.empty?

prompt = TTY::Prompt.new(output: STDERR)

def sorted_matches(matchset, search_term)
  FuzzyMatch
    .new(matchset)
    .find_all_with_score(search_term)
    .map { |dept, _pair_dist, _lev_dist| dept }
end

def opts_or_none(initial, opts)
  [
    OPT_NONE_OF_THE_ABOVE,
    "#{initial} #{OVERRIDE_MSG}",
  ].concat(opts)
end

client = client_mgr.get_register('government-organisation', 'beta')
dept_names = client.get_records.map { |r| r.item.value['name'] }

selected_name = nil
input = initial_input
loop do
  break unless selected_name.nil?

  choice = prompt.enum_select(
    'Did you mean?',
    opts_or_none(input, sorted_matches(dept_names, input))
  )
  if choice != OPT_NONE_OF_THE_ABOVE
    selected_name = choice
    break
  end

  choice = prompt.ask("Couldn't find a department with a name close enough to '#{input}'. Try another name (or leave empty to quit)")

  if choice.nil?
    prompt.say('Stopping')
    exit(1)
  end

  input = choice
end

puts selected_name.gsub(OVERRIDE_MSG, '').strip
{'fraction_non_alphanumeric': 0.1154836336888562, 'fraction_numerical': 0.004413387274733358, 'mean_word_length': 4.562372188139059, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7570940', 'n_tokens_mistral': 1025, 'n_tokens_neox': 985, 'n_words': 228}