diff --git a/SamsKnightlyTranslator/controller.rb b/SamsKnightlyTranslator/controller.rb new file mode 100644 index 0000000..d2e3c47 --- /dev/null +++ b/SamsKnightlyTranslator/controller.rb @@ -0,0 +1,131 @@ +require_relative 'view' +require_relative 'model' +require 'json' +require 'pry' + +class TranslatorController + attr_accessor :translation_dict + + include TranslatorView + include TranslatorModel + + def initialize + @translation_dict = Schema::import_translation_schema + end + + def translate string + words = string.scan Schema::WORDS_REGEX + translated_words = words.collect { |w| @translation_dict[w] || w } + translated_words.join " " + end + + def play + Display::title + pause + user_input = nil + + until ['quit', 'q', 'exit', 'e'].include? user_input + Display::menu + user_input = gets.chomp.downcase + case user_input + when 'v' then Display::print_schema(translation_dict) + when 'a' then add_new_entry + when 'u' then update_entry + when 'd' then delete_entry + when 's' then save_schema + else + puts translate user_input + end + pause + end + puts "~ Fare thee well ~" + end + + def pause + Display::pause + get_input + end + + def get_input + gets.chomp.downcase + end + + def confirm? + ['y', 'yes'].include? get_input + end + + def add_new_entry + Display::request_english_word + original = get_input + + if @translation_dict[original] + puts "Translation already exists; use (U)pdate to modify." + return + end + + Display::request_translation + translated = get_input + + puts "Save new translation: #{original} => #{translated}?" + if confirm? + Schema::update_entry(@translation_dict, original, translated) + #@translation_dict[original] = translated + puts "Change saved." + else + puts "Change rejected." + end + end + + def save_schema + puts "Save current translation schema to file?" + if confirm? + Schema::export_translation_schema(@translation_dict) + puts "New schema saved." + else + puts "Schema changes cancelled." + end + end + + def delete_entry + Display::request_english_word + target = get_input + + case + when (@translation_dict.keys.include? target) + puts "Destroy #{target} => #{@translation_dict[target]}?" + if confirm? + Schema::destroy_entry(@translation_dict, target) + puts "Translation deleted." + else + puts "Canceling deletion." + end + else + puts "Translation not found in current schema. Returning to menu." + end + end + + def update_entry + Display::request_english_word + target = get_input + + case + when @translation_dict.keys.include?(target) + puts "Current translation: #{target} => #{@translation_dict[target]}" + puts "Enter new translation for this word." + new_translation = get_input + + puts "Save updated entry: #{target} => #{new_translation}?" + if confirm? + Schema::update_entry(@translation_dict, target, new_translation) + #@translation_dict[target] = new_translation + puts "Entry updated." + else + puts "Canceled update." + end + else puts "No entry found to update. Use (A)dd to enter a new entry." + end + end +end + +translator = TranslatorController.new +translator.play \ No newline at end of file diff --git a/SamsKnightlyTranslator/model.rb b/SamsKnightlyTranslator/model.rb new file mode 100644 index 0000000..c48da88 --- /dev/null +++ b/SamsKnightlyTranslator/model.rb @@ -0,0 +1,35 @@ +#needs to get data from dictionary file (let's call it translation_dict.json)' + +#define here methods for: +#retrieving entry data for viewing +#adding new entries +#updating entries +#deleting entries +#saving current translation dictionary to file + +require 'json' + +module TranslatorModel + module Schema + WORDS_REGEX = /\w+[a-z']*/ + class << self + def import_translation_schema source = 'translations.json' + JSON.parse(File.new(source).read) + end + + def export_translation_schema(translation_dict, target = 'translations.json') + File.open(target, "w") do |f| + f.write(JSON.dump(translation_dict)) + end + end + + def update_entry(target_dict, original, translated) + target_dict[original] = translated + end + + def destroy_entry(target_dict, entry_key) + target_dict.delete entry_key + end + end + end +end \ No newline at end of file diff --git a/SamsKnightlyTranslator/translations.json b/SamsKnightlyTranslator/translations.json new file mode 100644 index 0000000..e1db8db --- /dev/null +++ b/SamsKnightlyTranslator/translations.json @@ -0,0 +1 @@ +{"hello":"what ho!","wait":"tarry","you":"thou","can":"mayst","drunk":"besotted","sir":"sire","lazy":"distemperate","here":"hither","there":"thither","ugh":"fie","have":"han","do":"doeth","say":"sayeth","mine":"mineth","would":"wouldst","go":"goeth","get":"geteth","when":"whenst","horse":"steed","pass":"passeth","starve":"wasteth away","my":"mineth","are":"art","why":"wherefore","slob":"lout","should":"shouldst","boss":"liege","where":"whence","destroy":"slay","truly":"verily","bum":"rapscallion","away":"hence","trash":"rubbish","doctor":"apothecary","hey":"hark","dirty":"sullied","kill":"slay","happens":"passeth","shirt":"surcoat","take":"taketh","insult":"besmirch","decorate":"bedight","before":"ere","actually":"forsooth","peninsula":"ness","often":"oft","pan":"pannikin","tough":"stalwart","prevent":"thwart","reject":"forswear","old":"wizened","whimsical":"flighty","sleepy":"bedward","argue":"brabble","stuffed":"crapulous","matted":"elflocked","confuse":"jargogle","enjoy":"deliciate","silly":"ludibrious","dismal":"malagruous","quarrel":"brabble","binge":"brannigan","fool":"hoddypeak","alcoholism":"bibesy","confusion":"widdendream","careless":"yemeles","yourself":"thyself","twilight":"twitter-light","attractive":"illecebrous","schmoozer":"snecklifter","hungry":"famelicose","although":"al be that","also":"als","immediately":"anon","unless":"but if","chance":"aventure","better":"bet","know":"konne","case":"caas","counsel":"conseil","courage":"corage","every":"everich","judge":"deeme","doubt":"drede","eyes":"eyen","eye":"ye","befall":"falle","glad":"fayne","evil":"foul","from":"fro","free":"fre","did":"gan","luck":"hap","same":"ilke","let":"lat","rude":"lewed","little":"lyte","like":"lyke","dear":"leve","mastery":"maistry","much":"michel","may":"moot","more":"mo","not":"nat","nor":"ne","weren't":"nere","wouldn't":"nolde","isn't":"nys","perhaps":"peraventure","ignorant":"nyce","said":"quod","advice":"reede","certainly":"sikerly","certain":"siker","blessed":"sely","opinion":"sentence","solace":"solas","truthfulness":"soothfastness","stop":"stente","dream":"sweven","such":"swich","prosper":"theen","wary":"war","true":"verray","jump":"sterte","will":"wol","knew":"wiste","once":"whilom","supposed":"wende","knows":"woot","gave":"yaf","new":"yep","if":"yif","surely":"ywis"} \ No newline at end of file diff --git a/SamsKnightlyTranslator/view.rb b/SamsKnightlyTranslator/view.rb new file mode 100644 index 0000000..eb6f86a --- /dev/null +++ b/SamsKnightlyTranslator/view.rb @@ -0,0 +1,100 @@ +#to-do: +#add views methods for: +# add menu +# update menu + + + +module TranslatorView + module Display + class << self + def title +title = <