<commit_before>require 'socket'
require 'rubygems'
require 'osc-ruby'
require 'securerandom'

class SonicPi
  RUN_COMMAND = "/run-code"
  STOP_COMMAND = "/stop-all-jobs"
  SERVER = 'localhost'
  PORT = 4557
  GUI_ID = 'SONIC_PI_CLI'

  def run(command)
    client.send(run_command(command))
  end

  def stop
    client.send(stop_command)
  end

  def test_connection!
    begin
      OSC::Server.new(PORT)
      abort("ERROR: Sonic Pi is not listening on #{PORT} - is it running?")

    rescue
      # everything is good
    end
  end

  private

  def client
    client ||= OSC::Client.new(SERVER, PORT)
  end

  def run_command(command)
    OSC::Message.new(RUN_COMMAND, GUI_ID, "#{command}")
  end

  def stop_command
    OSC::Message.new(STOP_COMMAND, GUI_ID)
  end
end
<commit_msg>Refactored code to reduce duplication and number of methods<commit_after>require 'socket'
require 'rubygems'
require 'osc-ruby'
require 'securerandom'

class SonicPi
  RUN_COMMAND = "/run-code"
  STOP_COMMAND = "/stop-all-jobs"
  SERVER = 'localhost'
  PORT = 4557
  GUI_ID = 'SONIC_PI_CLI'

  def run(command)
    send_command(RUN_COMMAND, command)
  end

  def stop
    send_command(STOP_COMMAND)
  end

  def test_connection!
    begin
      OSC::Server.new(PORT)
      abort("ERROR: Sonic Pi is not listening on #{PORT} - is it running?")
    rescue
      # everything is good
    end
  end

  private

  def client
    @client ||= OSC::Client.new(SERVER, PORT)
  end

  def send_command(call_type, command=nil)
    prepared_command = OSC::Message.new(call_type, GUI_ID, command)
    client.send(prepared_command)
  end

end

{'fraction_non_alphanumeric': 0.1021039603960396, 'fraction_numerical': 0.0049504950495049506, 'mean_word_length': 3.3702702702702703, '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': '6224665', 'n_tokens_mistral': 596, 'n_tokens_neox': 569, 'n_words': 143}