Skip to content
131 changes: 131 additions & 0 deletions SamsKnightlyTranslator/controller.rb
Original file line number Diff line number Diff line change
@@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you have this get_input method but then on line 29 you repeat this code. Maybe use this method there instead?

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
35 changes: 35 additions & 0 deletions SamsKnightlyTranslator/model.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions SamsKnightlyTranslator/translations.json
Original file line number Diff line number Diff line change
@@ -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"}
100 changes: 100 additions & 0 deletions SamsKnightlyTranslator/view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#to-do:
#add views methods for:
# add menu
# update menu



module TranslatorView
module Display
class << self
def title
title = <<TITLE

WHAT HO!
THOU SHALL BECOME KNIGHTLY
WITH MINE AMAZING KNIGHTLY TRANSLATOR

,;~;,
/\_
( /
((), ;,;
| \\ ,;;'(
__ _( )'~;;' \
/' '\'()/~' \ /'\.)
,;( )|| |
,;' \ /-(.;, )
) / ) /
// ||
(_\ (_\

***** PRESS ANY KEY TO CONTINUE *****

TITLE
puts title
end

def menu

menu = <<MENU

| l~~~~~~~l |
/\ l^^^^^^^^^^^^^^^l /\
/__\ _l O O O O O l_/__\
l l l l l
l l l l l
[^^^] l [^^^] [^^^] l [^^^]
[ o ]-------------[ o ]-------------[ o ]-------------[ o ]
[ ] [ ] ___ [ ] [ ]
[ ] [ ] / \ [ ] [ ]
[ ] [ ] | | [ ] o [ ]
[ ] [ ] | | [ ]~\_|_ [ ]
[___]_____________[___]___|_____|___[___]_/\_/\_______[___]__

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are dope




********** HARK! ************
- Enter text to knightify, OR
- (V)iew translation schema
- (A)dd a new translation
- (U)pdate a translation
- (D)elete a translation
- (S)ave schema to file
- (Q)uit program
********** *************
MENU
puts menu
end

def print_schema source
puts "ORIGINAL // TRANSLATED"
source.each do |original, translated|
puts"#{original} => #{translated}"
end
end

def pause
puts "Press enter to continue"
end

def request_english_word
puts "Please enter the modern english word for the entry"
end

def request_translation
puts "Please enter the knightly translation"
end

def confirm?
puts "Save changes? Y"
end
end
end
end

#p TranslatorView::Display.methods.sort