<commit_before>
module Yandex

  class JsonParser < HTTParty::Parser
    def json
      ::JSON.parse(body)
    end
  end

  class Translator
    include HTTParty
    parser JsonParser
    base_uri 'https://translate.yandex.net/api/v1.5/tr.json'

    attr_reader :api_key

    class << self # backwards compatability
      attr_reader :api_key, :translator

      def api_key=(key)
        @api_key = key
        @translator = new(api_key)
      end

      def get_langs
        translator.get_langs
      end

      def translate(text, *lang)
        translator.translate(text, *lang)
      end

      def detect(text)
        translator.detect(text)
      end

      private :translator
    end

    def initialize(api_key)
      @api_key = api_key
    end

    def langs
      @langs ||= visit('/getLangs')['dirs']
    end
    alias_method :get_langs, :langs

    def detect(text)
      lang = visit('/detect', text: text)['lang']
      lang == '' ? nil : lang
    end

    def translate(text, *lang)
      if lang.last.is_a?(Hash)
        lang_options = lang.last
        lang = [lang_options[:to], lang_options[:from]].compact
      end

      options = { text: text, lang: lang.reverse.join('-') }

      result = visit('/translate', options)['text']

      result.size == 1 ? result.first : result
    end

    def visit(address, options = {})
      response = self.class.post address, body: options.merge(key: api_key)
      check_errors(response)
      response
    end<commit_msg>add optional format<commit_after>
module Yandex

  class JsonParser < HTTParty::Parser
    def json
      ::JSON.parse(body)
    end
  end

  class Translator
    include HTTParty
    parser JsonParser
    base_uri 'https://translate.yandex.net/api/v1.5/tr.json'

    attr_reader :api_key

    class << self # backwards compatability
      attr_reader :api_key, :translator

      def api_key=(key)
        @api_key = key
        @translator = new(api_key)
      end

      def get_langs
        translator.get_langs
      end

      def translate(text, *lang)
        translator.translate(text, *lang)
      end

      def detect(text)
        translator.detect(text)
      end

      private :translator
    end

    def initialize(api_key)
      @api_key = api_key
    end

    def langs
      @langs ||= visit('/getLangs')['dirs']
    end
    alias_method :get_langs, :langs

    def detect(text)
      lang = visit('/detect', text: text)['lang']
      lang == '' ? nil : lang
    end

    def translate(text, *lang, format: 'plain')
      if lang.last.is_a?(Hash)
        lang_options = lang.last
        lang = [lang_options[:to], lang_options[:from]].compact
      end

      options = { text: text, lang: lang.reverse.join('-'), format: format }

      result = visit('/translate', options)['text']

      result.size == 1 ? result.first : result
    end

    def visit(address, options = {})
      response = self.class.post address, body: options.merge(key: api_key)
      check_errors(response)
      response
    end
{'fraction_non_alphanumeric': 0.11070599933708983, 'fraction_numerical': 0.0019887305270135896, 'mean_word_length': 2.2911668484187566, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '3066907', 'n_tokens_mistral': 983, 'n_tokens_neox': 946, 'n_words': 227}