namespace :dataset do desc "TODO" task ingest: :environment do english = create_language('English') ingest_pos(english, 'dataset_en') ingest_dictionary(english, 'dataset_en') french = create_language('French') ingest_pos(french, 'dataset_fr') ingest_dictionary(french, 'dataset_fr') puts "Ingest complete." end end def create_language(name) if Language.where(name: name).count > 0 puts "Language #{name} already exists! Skipping step." return end return Language.create(name: name) end def ingest_pos(language, dataset) if PartOfSpeech.where(language_id: language.id).count > 0 puts "Parts of speech data already exists in table! Aborting." return end parts_of_speech = JSON.parse(File.read("db/#{dataset}/toki-partsofspeech.json")) parts_of_speech.each do |pos| PartOfSpeech.create(pos: pos['pos'], definition: pos['definition'], language_id: language.id) end puts "Parts of speech ingest complete." end def ingest_dictionary(language, dataset) if Definition.where(language_id: language.id).count > 0 puts "Dictionary data for this language already exists in table! Aborting." return end dictionary = JSON.parse(File.read("db/#{dataset}/toki-dictionary.json")) dictionary.each do |entry| if entry['word'] == "a" puts entry end #word = Word.create(word: entry['word']) word = Word.where(word: entry['word']).first if word.nil? word = Word.create(word: entry['word']) end entry['definitions'].each do |definition| word.definitions.create(pos: definition['pos'], definition: definition['definition'], language_id: language.id) end end puts "Dictionary ingest complete." end