From f5e9f3699fffde8cd0bc13dd993f2f925dac6f2c Mon Sep 17 00:00:00 2001 From: maddiebaka Date: Thu, 12 Oct 2023 22:24:31 -0400 Subject: [PATCH] Add dictionary ingest and view Dictionary json ingest to database, view populated with database contents --- app/controllers/dictionary_controller.rb | 1 + app/models/definition.rb | 3 ++ app/models/word.rb | 3 ++ app/views/dictionary/index.html.erb | 20 +++++++++++- db/migrate/20231013015428_create_words.rb | 9 ++++++ .../20231013015524_create_definitions.rb | 11 +++++++ db/schema.rb | 18 ++++++++++- lib/tasks/database.rake | 31 ++++++++++++++++--- 8 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 app/models/definition.rb create mode 100644 app/models/word.rb create mode 100644 db/migrate/20231013015428_create_words.rb create mode 100644 db/migrate/20231013015524_create_definitions.rb diff --git a/app/controllers/dictionary_controller.rb b/app/controllers/dictionary_controller.rb index 1c164c5..4100ada 100644 --- a/app/controllers/dictionary_controller.rb +++ b/app/controllers/dictionary_controller.rb @@ -1,5 +1,6 @@ class DictionaryController < ApplicationController def index @parts_of_speech = PartOfSpeech.all + @words = Word.all end end diff --git a/app/models/definition.rb b/app/models/definition.rb new file mode 100644 index 0000000..26bd152 --- /dev/null +++ b/app/models/definition.rb @@ -0,0 +1,3 @@ +class Definition < ApplicationRecord + belongs_to :word +end diff --git a/app/models/word.rb b/app/models/word.rb new file mode 100644 index 0000000..fdd33d0 --- /dev/null +++ b/app/models/word.rb @@ -0,0 +1,3 @@ +class Word < ApplicationRecord + has_many :definitions +end diff --git a/app/views/dictionary/index.html.erb b/app/views/dictionary/index.html.erb index 0b6bae0..cb53560 100644 --- a/app/views/dictionary/index.html.erb +++ b/app/views/dictionary/index.html.erb @@ -10,8 +10,26 @@ <% @parts_of_speech.each do |item| %> - <%= item.pos %> + <%= item.pos %> <%= item.definition %> <% end %> + +

Words

+<% @words.each do |word| %> +

<%= word.word %>

+ + + + + + + <% word.definitions.each do |definition| %> + + + + + <% end %> +
Part of SpeechDefinition
<%= definition.pos %><%= definition.definition %>
+<% end %> diff --git a/db/migrate/20231013015428_create_words.rb b/db/migrate/20231013015428_create_words.rb new file mode 100644 index 0000000..925ec6e --- /dev/null +++ b/db/migrate/20231013015428_create_words.rb @@ -0,0 +1,9 @@ +class CreateWords < ActiveRecord::Migration[7.1] + def change + create_table :words do |t| + t.string :word + + t.timestamps + end + end +end diff --git a/db/migrate/20231013015524_create_definitions.rb b/db/migrate/20231013015524_create_definitions.rb new file mode 100644 index 0000000..cf375d8 --- /dev/null +++ b/db/migrate/20231013015524_create_definitions.rb @@ -0,0 +1,11 @@ +class CreateDefinitions < ActiveRecord::Migration[7.1] + def change + create_table :definitions do |t| + t.string :pos + t.string :definition + t.references :word, null: false, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index c56b2b4..8a968fe 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,16 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2023_10_12_235050) do +ActiveRecord::Schema[7.1].define(version: 2023_10_13_015524) do + create_table "definitions", force: :cascade do |t| + t.string "pos" + t.string "definition" + t.integer "word_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["word_id"], name: "index_definitions_on_word_id" + end + create_table "part_of_speeches", force: :cascade do |t| t.string "pos" t.string "definition" @@ -18,4 +27,11 @@ ActiveRecord::Schema[7.1].define(version: 2023_10_12_235050) do t.datetime "updated_at", null: false end + create_table "words", force: :cascade do |t| + t.string "word" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_foreign_key "definitions", "words" end diff --git a/lib/tasks/database.rake b/lib/tasks/database.rake index 8c658a5..893c113 100644 --- a/lib/tasks/database.rake +++ b/lib/tasks/database.rake @@ -1,14 +1,16 @@ namespace :dataset do desc "TODO" task ingest: :environment do - ingest_data + ingest_pos + ingest_dictionary + puts "Ingest complete." end end -def ingest_data +def ingest_pos if PartOfSpeech.count > 0 - puts "Data already exists in table! Aborting." + puts "Parts of speech data already exists in table! Aborting." return end @@ -17,5 +19,26 @@ def ingest_data parts_of_speech.each do |pos| PartOfSpeech.create(pos: pos['pos'], definition: pos['definition']) end - puts "Ingest complete." + puts "Parts of speech ingest complete." +end + +def ingest_dictionary + if Word.count > 0 + puts "Dictionary data already exists in table! Aborting." + return + end + + dictionary = JSON.parse(File.read('db/dataset_en/toki-dictionary.json')) + + dictionary.each do |entry| + if entry['word'] == "a" + puts entry + end + word = Word.create(word: entry['word']) + + entry['definitions'].each do |definition| + word.definitions.create(pos: definition['pos'], definition: definition['definition']) + end + end + puts "Dictionary ingest complete." end