<commit_before>require 'dictionary_api/article'
require 'dictionary_api/translation_article'

module DictionaryAPI

  class DictionaryAPI::Parser

    def parse(response)
      response['def'].inject([]) { |result, response_el| result << self.build_article(response_el) }
    end

  protected

    def build_article(some_hash)
      article = Article.new
      article.text = some_hash['text']
      article.position = some_hash['pos']
      article.transcription = some_hash['ts']
      article.translation = some_hash['tr'].inject([]) { |result, el| result << self.build_translation_article(el) }
      article
    end

    def build_translation_article(some_hash)
      translation_article = TranslationArticle.new
      translation_article.text = some_hash['text']
      translation_article.position = some_hash['pos']
      translation_article.synonym = self.build_string (some_hash['syn']) unless some_hash['syn'].class == NilClass
      translation_article.meaning = self.build_string(some_hash['mean']) unless some_hash['mean'].class == NilClass
      translation_article.example = self.build_string(some_hash['ex']) unless some_hash['ex'].class == NilClass
      translation_article.aspect = some_hash['asp']
      translation_article
    end
    
    def build_string(some_array)
      some_array.inject([]) do |result, element|      
        result << element.to_a.inject([]) do |result, el|
                   result << (el[1].class == Array ? self.build_string(el[1]) : el[1])
                 end
        result.flatten!
      end
    end
  end
end<commit_msg>Update parser.rb<commit_after>require 'dictionary_api/article'
require 'dictionary_api/translation_article'

module DictionaryAPI

	class DictionaryAPI::Parser

		def parse(response)
			response['def'].inject([]) { |result, response_el| result << self.build_article(response_el) }
		end

	protected

		def build_article(articles_hash)
			article = Article.new
			article.text = articles_hash['text']
			article.position = articles_hash['pos']
			article.transcription = articles_hash['ts']
			article.translation = articles_hash['tr'].inject([]) { |result, el| result << self.build_translation_article(el) }
			article
		end

		def build_translation_article(articles_hash)
			translation_article = TranslationArticle.new
			translation_article.text = articles_hash['text']
			translation_article.position = articles_hash['pos']
			translation_article.synonym = self.build_array(articles_hash['syn']) unless articles_hash['syn'].nil?
			translation_article.meaning = self.build_array(articles_hash['mean']) unless articles_hash['mean'].nil?
			translation_article.example = self.build_array(articles_hash['ex']) unless articles_hash['ex'].nil?
			translation_article.aspect = articles_hash['asp']
			translation_article
		end
		
		def build_array(articles_array)
			articles_array.inject([]) do |result, element|      
				result << element.to_a.inject([]) do |result, el|
									  result << (el[1].class == Array ? self.build_array(el[1]) : el[1])
								  end
				result.flatten!
			end
		end
	end
end
{'fraction_non_alphanumeric': 0.12016888600194868, 'fraction_numerical': 0.001948684637869438, 'mean_word_length': 4.141903171953255, 'pattern_counts': {'":': 0, '<': 19, '<?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': '2617794', 'n_tokens_mistral': 979, 'n_tokens_neox': 879, 'n_words': 166}