class Dictionaries::AskWordFromDictionary
Constants
- NAMESPACE
#¶ ↑
NAMESPACE
¶ ↑#¶ ↑
Public Class Methods
new( optional_search_for_this_word = nil, use_this_file_for_the_dataset = :default_file, run_already = true )
click to toggle source
#¶ ↑
initialize¶ ↑
#¶ ↑
# File lib/dictionaries/class/ask_word_from_dictionary.rb, line 56 def initialize( optional_search_for_this_word = nil, use_this_file_for_the_dataset = :default_file, run_already = true ) register_sigint reset case optional_search_for_this_word when :do_not_run_yet run_already = :do_not_run_yet optional_search_for_this_word = nil end # ======================================================================= # # === Next, set the main file to use # ======================================================================= # if use_this_file_for_the_dataset set_use_this_file(use_this_file_for_the_dataset) load_main_dataset end set_search_for_this_word(optional_search_for_this_word) if run_already.is_a? Hash if run_already.has_key? :delay set_delay(run_already.delete(:delay)) end run_already = true end case run_already when :dont_run_yet, :do_not_run_yet run_already = false end run if run_already end
Public Instance Methods
available_keys?()
click to toggle source
#¶ ↑
available_keys?¶ ↑
#¶ ↑
# File lib/dictionaries/class/ask_word_from_dictionary.rb, line 135 def available_keys? @words.keys if @words end
Also aliased as: array
be_verbose?()
click to toggle source
brown(i = '')
click to toggle source
build_url_www_string(i)
click to toggle source
#¶ ↑
build_url_www_string
¶ ↑
#¶ ↑
# File lib/dictionaries/class/ask_word_from_dictionary.rb, line 389 def build_url_www_string(i) _ = ''.dup # ======================================================================= # # We need to differ between the english dictionary and the italian # dictionary. # ======================================================================= # language = Dictionaries.return_name_from_compound(main_file?) if language == 'english' _ << '"' unless _.end_with? '"' end case language # case tag when 'italian' _ << URL_FOR_ITALIAN_DICTIONARY when 'english' # This is the default. _ << URL_FOR_DICT_LEO+'ende/?lp=ende&lang=de&searchLoc=0&cmpType=relaxed§Hdr=on&spellToler=&search=' end _ << i.to_s if language == 'english' _ << '"' unless _.end_with? '"' # Append '"' here. end _ end
consider_opening_browser_page(i = '')
click to toggle source
delay?()
click to toggle source
do_ask_the_question()
click to toggle source
#¶ ↑
do_ask_the_question
¶ ↑
#¶ ↑
# File lib/dictionaries/class/ask_word_from_dictionary.rb, line 324 def do_ask_the_question # This method will ask the english question. if search_for?.empty? set_search_for_this_word 'RANDOM' end _ = search_for? if _ == 'rand' _ = (@search_for_this_word = return_random_word) end unless is_included? _ set_xorg_buffer _ opnn; e 'Sorry, the word `'+sfancy(_)+'` is not included.' opnn; e 'We will thus try to open our english dictionary in our editor' opnn; e 'and lookup the word in our browser.' open_main_yaml_file_in_main_editor # Added ~ 22.02.2013 consider_opening_browser_page(_) else # Ok, we now know that it is included. Thus, continue. if be_verbose? e "#{rev}What does the word `#{sfancy(_)}` mean? #{show_delay_string}" find_this_word_in_yaml_file(_) new_buffer = build_url_www_string(_) set_xorg_buffer(new_buffer) sleep delay? reveal_the_answer end end end
feedback_delay()
click to toggle source
feedback_help_options()
click to toggle source
#¶ ↑
feedback_help_options
¶ ↑
If you wish to trigger this, do:
askeng --help
#¶ ↑
# File lib/dictionaries/class/ask_word_from_dictionary.rb, line 253 def feedback_help_options # help tag e e LPAD+sfancy('TOTAL')+' # Feedback how many keys are registered, '+ 'also aliased to '+sfancy('STATS')+'.' e LPAD+sfancy('OPEN')+' # Open '+sfile(File.basename(main_file?))+ ' and '+sfile('ask_english_word.rb')+' in the main editor.' e LPAD+sfancy('SOLVED')+' # Set the last word as solved.' e LPAD+sfancy('URL')+' # Show the URL for the dictionary.' e LPAD+sfancy('REPEAT')+' # Repeat the last question asked.' e LPAD+sfancy('DELAY?')+' # Feedback the delay we use.' e LPAD+sfancy('STATS')+' # To show some statistics, especially '\ 'the amount of registered words.' e LPAD+sfancy('FILE?')+' # Show the main .yml file in question.' e exit end
feedback_statistics()
click to toggle source
#¶ ↑
feedback_statistics
¶ ↑
Feedback the amount of questions registered. In the future, perhaps we may want to have more statistic-options, so this may be expanded at a later time.
To trigger this, do:
askeng --stats?
#¶ ↑
# File lib/dictionaries/class/ask_word_from_dictionary.rb, line 491 def feedback_statistics feedback_total_amount_of_keys(:do_not_exit) end
feedback_total_amount_of_keys(shall_we_exit = true)
click to toggle source
#¶ ↑
feedback_total_amount_of_keys
¶ ↑
#¶ ↑
# File lib/dictionaries/class/ask_word_from_dictionary.rb, line 528 def feedback_total_amount_of_keys(shall_we_exit = true) # KEYS case shall_we_exit when :do_not_exit shall_we_exit = false end _ = @original_words.size - @words.size opnn; e swarn(@original_words.size.to_s)+' english words in total.' rounded = sprintf('%0.2f', 100.0 - (@words.size * 100.0 / @original_words.size) ) opnn; e swarn(@words.size.to_s)+' unanswered english words, '\ 'thus '+sfancy(_.to_s)+' are already answered. ('+ simp(rounded.to_s+'%')+')' opnn; e 'The yaml file that we we will use is:' opnn; e ' '+sfile(main_file?) exit if shall_we_exit end
feedback_url()
click to toggle source
feedback_yaml_file()
click to toggle source
find_all_matches_for(i)
click to toggle source
find_this_word_in_yaml_file(this_word)
click to toggle source
#¶ ↑
find_this_word_in_yaml_file
¶ ↑
This method will give us the correct line number.
#¶ ↑
# File lib/dictionaries/class/ask_word_from_dictionary.rb, line 144 def find_this_word_in_yaml_file(this_word) _ = File.readlines( @use_this_file, :encoding => USE_THIS_ENCODING ) _ = _.index() {|x| x =~ /^#{this_word}/ } line_number = _.to_i + 1 save_line_number(line_number) save_last_question_asked(this_word) end
generate_tab_completion( this_language = 'english' )
click to toggle source
#¶ ↑
generate_tab_completion
(tab tag)¶ ↑
The first argument should be the name of the dictionary in question, such as ‘english’ or ‘italian’.
To generate the tab completion, do this:
askeng TAB
#¶ ↑
# File lib/dictionaries/class/ask_word_from_dictionary.rb, line 183 def generate_tab_completion( this_language = 'english' ) require 'generate_shell_completion' dataset = @original_words.keys # cat $SCI/YAML/DICTIONARIES/english.yml # Next, determine where to store the completion stuff. generate_where = ENV['MY_OS'].to_s+'/LINUX/SHELL/SCRIPTS/'+this_language+'_completion.sh' _ = GenerateShellCompletion::GenerateShellCompletion.new(:do_not_run_yet) _.add_data(dataset) _.set_file(generate_where) _.set_name_of_function(this_language.to_sym) _.add_aliases %w( askeng ask_eng ) _.generate end
green()
click to toggle source
is_included?(i)
click to toggle source
load_dataset( of_this_file = @use_this_file )
click to toggle source
#¶ ↑
load_dataset
¶ ↑
Here we will define the important instance variable @original_words.
#¶ ↑
# File lib/dictionaries/class/ask_word_from_dictionary.rb, line 425 def load_dataset( of_this_file = @use_this_file ) # called via reset() of_this_file = of_this_file.to_s # We must work with a String. if File.exist? of_this_file dataset = YAML.load_file(of_this_file) @original_words = dataset # ^^^ This is the file english.yml else e 'Did not find a file at '+of_this_file+'.' e 'Thus, the @original_words will be empty.' e 'If you wish to change this, modify the constant ENGLISH_WORDS' e 'to point at a yaml-dataset (we require a hash, thus key: value pairs)' @original_words = {} end @words = @original_words.dup sanitize_dataset # Get rid of lines that have [] as entry. end
Also aliased as: load_main_dataset
load_the_english_file()
click to toggle source
main_file?()
click to toggle source
#¶ ↑
main_file?¶ ↑
#¶ ↑
# File lib/dictionaries/class/ask_word_from_dictionary.rb, line 657 def main_file? Dictionaries.main_file? end
Also aliased as: currently_selected_file?
notify_the_user_that_we_will_next_open_this_file(this_file)
click to toggle source
on_roebe?()
click to toggle source
open_main_yaml_file_in_main_editor( this_file = main_file? )
Alias for: open_yaml_file_in_main_editor
open_this_file_here(be_verbose = false)
click to toggle source
open_yaml_file_in_main_editor( this_file = main_file? )
click to toggle source
#¶ ↑
open_yaml_file_in_main_editor
¶ ↑
#¶ ↑
# File lib/dictionaries/class/ask_word_from_dictionary.rb, line 506 def open_yaml_file_in_main_editor( this_file = main_file? ) if on_roebe? # Rewrite for my own system at home. this_file = File.basename(this_file) this_file = MY_DICTIONARIES+this_file end _= this_file notify_the_user_that_we_will_next_open_this_file(_) system "bluefish #{_}" end
Also aliased as: open_main_yaml_file_in_main_editor
opnn()
click to toggle source
register_sigint()
click to toggle source
reset()
click to toggle source
#¶ ↑
reset¶ ↑
#¶ ↑
# File lib/dictionaries/class/ask_word_from_dictionary.rb, line 92 def reset # ======================================================================= # # === @original_words # ======================================================================= # @original_words = {} # A Hash. # ======================================================================= # # === @words # ======================================================================= # @words = nil # ======================================================================= # # === @be_verbose # ======================================================================= # @be_verbose = true # ======================================================================= # # === @store_line_number_where # ======================================================================= # @store_line_number_where = STORE_LINE_NUMBER_HERE # ======================================================================= # # === @ask_the_question # ======================================================================= # @ask_the_question = true # Whether to ask the question or not. set_use_this_file # Set to nil initially. set_delay end
result?()
click to toggle source
#¶ ↑
result?¶ ↑
#¶ ↑
# File lib/dictionaries/class/ask_word_from_dictionary.rb, line 294 def result? @result end
Also aliased as: result
return_random_word()
click to toggle source
rev()
click to toggle source
reveal_the_answer()
click to toggle source
#¶ ↑
reveal_the_answer
¶ ↑
Show the answer here.
#¶ ↑
# File lib/dictionaries/class/ask_word_from_dictionary.rb, line 204 def reveal_the_answer @result = @original_words[@search_for_this_word].to_s _ = @result begin _[0,1] = _[0,1].upcase rescue Exception => error opnn; e 'An exception happened at line '+simp(__LINE__.to_s)+' when '\ 'we tried to use .upcase.' opnn; e 'The file was '+sfile(__FILE__) opnn; e 'The difficult word in question was `'+simp(_)+'`.' opnn; e 'The encoding in use was '+simp(_.encoding.to_s)+'.' pp error begin _ = _.encode(USE_THIS_ENCODING) _[0,1] = _[0,1].upcase rescue Exception => error pp error end end _ = sanitize_answer(_) if _.include? '[]' # Chop off [] token, if it is part of the String. _ = _.force_encoding(USE_THIS_ENCODING) if _.encoding.to_s.include? 'UTF' _ = _.delete('[').delete(']').strip end e N+simportant(_)+'.'+N+N end
Also aliased as: reveal_answer
run()
click to toggle source
sanitize_answer(i)
click to toggle source
#¶ ↑
sanitize_answer
¶ ↑
This may also re-encode the given String object.
#¶ ↑
# File lib/dictionaries/class/ask_word_from_dictionary.rb, line 560 def sanitize_answer(i) begin unless i.encoding.to_s.include? USE_THIS_ENCODING i = i.force_encoding(USE_THIS_ENCODING) end i.gsub!(/ \[\]/,'') if i.include? '[]' rescue Exception => error opnn; pp error # Propagate the error here to the user. end return i end
sanitize_dataset()
click to toggle source
save_file(what, into)
click to toggle source
save_last_question_asked(what)
click to toggle source
#¶ ↑
save_last_question_asked
¶ ↑
#¶ ↑
# File lib/dictionaries/class/ask_word_from_dictionary.rb, line 600 def save_last_question_asked(what) # Save the last question asked. into = store_last_question_asked_where? begin save_file(what, into) rescue Errno::ENOENT opnn; e "Can not store into `#{sfile(into)}`." end end
save_line_number(what)
click to toggle source
sdir(i)
click to toggle source
search_for?()
click to toggle source
set_delay(i = DEFAULT_DELAY)
click to toggle source
set_last_word_solved()
click to toggle source
#¶ ↑
set_last_word_solved
¶ ↑
#¶ ↑
# File lib/dictionaries/class/ask_word_from_dictionary.rb, line 587 def set_last_word_solved # Call this only when you want to solve an english word. begin require 'roebe/classes/append_to_line.rb' _ = Roebe::AppendToLine.new(:english, false) # bl $RUBY_TOOLS/append_to_line.rb _.set_line File.read(STORE_LINE_NUMBER_HERE) _.run rescue LoadError; end exit end
set_search_for_this_word(i = :rand)
click to toggle source
#¶ ↑
set_search_for_this_word
(menu tag)¶ ↑
This method will also have the menu-functionality of the class.
#¶ ↑
# File lib/dictionaries/class/set_search_for_this_word.rb, line 16 def set_search_for_this_word(i = :rand) i = i.join(' ').strip if i.is_a? Array # This will also handle empty ARGV passed to us. i = :rand if i.nil? case i when :rand, 'rand' # We want a random word here. i = return_random_word if @words end # ======================================================================= # # We want a String past this point. # ======================================================================= # i = i.to_s.dup # ======================================================================= # # We can not downcase the input since we also try to have it match # towards regular english letters. It is a dictionary after all. # ======================================================================= # # === Chop off last char if it is a question mark. # ======================================================================= # i.chop! if i.end_with? '?' case i # case tag # ======================================================================= # # === askeng --delay? # ======================================================================= # when /^-?-?delay\??$/i feedback_delay # ======================================================================= # # === askeng --openall # ======================================================================= # when 'OPEN', 'OPE', 'OP', 'O', 'EDIT', 'OPEN_ALL', /open/, 'OPENALL', '--openall' open_this_file_here(true) # true for "be verbose". open_yaml_file_in_main_editor exit # ======================================================================= # # === askeng --n-entries # ======================================================================= # when 'TOTAL', 'KEYS', 'KEYSTATS', '--n-entries', '--nentries', 'keys?', 'nkeys', /-?-?nwords\??/ feedback_total_amount_of_keys # ======================================================================= # # === askeng --stats # ======================================================================= # when '--stats','--statistics','STATS','STATISTICS','STATS?','--stats?' feedback_statistics; exit # ======================================================================= # # === askeng --help # ======================================================================= # when 'HELP','--help','show_help','--HELP' feedback_help_options # This will also exit. # ======================================================================= # # === askeng --every_word # ======================================================================= # when 'SHOW', '--show-every-word', '--every_word' show_every_word exit # ======================================================================= # # === askeng --generate-tab # ======================================================================= # when 'GENERATE', 'TAB', 'GENERATE_COMPLETION', 'TABCOMPLETION', 'COMPLETION', '--generate-tab' generate_tab_completion exit # ======================================================================= # # === askeng --open-in-browser # ======================================================================= # when 'OPEN_BROWSER','--open-in-browser' open_in_browser # ======================================================================= # # === askeng --solved # ======================================================================= # when 'SOLVED',/-?-?solved/ set_last_word_solved # ======================================================================= # # === askeng --yaml-file? # ======================================================================= # when 'FILE?', 'YAML_FILE?','--yaml-file?' feedback_yaml_file exit # ======================================================================= # # === askeng --open-dictionary # ======================================================================= # when 'OPEN_DICTIONARY','OPEN_DICT','--open-dictionary' open_main_yaml_file_in_main_editor exit # ======================================================================= # # === askeng --repeat # ======================================================================= # when 'REPEAT','--repeat' _ = STORE_LAST_QUESTION_ASKED_HERE if File.exist? _ i = File.read(_) else opnn; e 'The file `'+sfile(_)+'` was not found, thus we can' opnn; e 'not read in anything.' exit end # ======================================================================= # # === askeng --url # ======================================================================= # when '--url','URL' feedback_url # ======================================================================= # # === askeng --random # ======================================================================= # when /^-?-?random$/i,'RAND','rand' # Pass through here. i = return_random_word if available_keys? # else tag else # Handle assignment cases next. if i.include?('=') and i.downcase.include? 'delay' new_delay = i.split('=').last.strip e 'It seems as if you may want to assign a new delay.' e 'We will thus set the last line of this file here' e 'with the new delay of:' e ' '+sfancy(new_delay) what = File.readlines(Dictionaries::AskEnglishWord.main_file?) what[-1,1] = new_delay # Set the new entry here. SaveFile.write_what_into(what.join, Dictionaries::AskEnglishWord.main_file?) exit end end i = i.to_s.dup i = i.downcase if SHALL_WE_DOWNCASE if i.size == 1 # We assume that the user wants to get a category. i = find_all_matches_for(i).sample end @search_for_this_word = i # We will search for this word here. end
set_use_this_file(i = :default)
click to toggle source
#¶ ↑
set_use_this_file
¶ ↑
This sets the main dictionary-file to use, the file path to it.
#¶ ↑
# File lib/dictionaries/class/ask_word_from_dictionary.rb, line 449 def set_use_this_file(i = :default) case i when :english i = ENGLISH_WORDS when :default_file, :default i = main_file? end @use_this_file = i end
set_xorg_buffer(i)
click to toggle source
sfancy(i)
click to toggle source
sfile(i)
click to toggle source
show_delay_string()
click to toggle source
show_every_word()
click to toggle source
show_help()
click to toggle source
simp(i)
click to toggle source
#¶ ↑
simp¶ ↑
#¶ ↑
# File lib/dictionaries/class/colours.rb, line 37 def simp(i) ::Colours.simp(i) end
Also aliased as: simportant
store_last_question_asked_where?()
click to toggle source
#¶ ↑
store_last_question_asked_where?¶ ↑
This method will try to automatically find out where we will store the last question asked.
#¶ ↑
# File lib/dictionaries/class/ask_word_from_dictionary.rb, line 371 def store_last_question_asked_where? name = Dictionaries.return_name_from_compound(main_file?) path = DEPOT_INFORMATION_DIR+'last_'+name+'_question_asked' return path end
translate(i)
click to toggle source
use_this_file?()
click to toggle source