module Dictionaries
#¶ ↑
#¶ ↑
require ‘dictionaries/class/constants.rb’
#¶ ↑
#¶ ↑
require ‘dictionaries/class/set_search_for_this_word.rb’
#¶ ↑
#¶ ↑
require ‘dictionaries/commandline/parse_commandline.rb’
#¶ ↑
#¶ ↑
require ‘dictionaries/constants.rb’
#¶ ↑
#¶ ↑
require ‘dictionaries/helper_module/helper_module.rb’ include Dictionaries::HelperModule
#¶ ↑
#¶ ↑
This file will point to the yaml directory of the Dictionaries
project.
#¶ ↑
require ‘dictionaries/project/project.rb’
#¶ ↑
#¶ ↑
Dictionaries::Statistics
¶ ↑
#¶ ↑
require ‘dictionaries/statistics/statistics.rb’ Dictionaries::Statistics.report
#¶ ↑
#¶ ↑
require ‘dictionaries/toplevel_methods/e.rb’
#¶ ↑
#¶ ↑
require ‘dictionaries/toplevel_methods/has_key.rb’ Dictionaries.has_key?
#¶ ↑
#¶ ↑
require ‘dictionaries/toplevel_methods/is_on_roebe.rb’
#¶ ↑
#¶ ↑
require ‘dictionaries/toplevel_methods/main_file.rb’
#¶ ↑
#¶ ↑
require ‘dictionaries/toplevel_methods/misc.rb’
#¶ ↑
#¶ ↑
require ‘dictionaries/version/version.rb’
#¶ ↑
Constants
- DEFAULT_DELAY
#¶ ↑
DEFAULT_DELAY
¶ ↑Specify how long to wait before revealing the translated word.
#¶ ↑
- DEPOT_INFORMATION_DIR
#¶ ↑
DEPOT_INFORMATION_DIR
¶ ↑#¶ ↑
- DICTIONARIES_DIR
#¶ ↑
DICTIONARIES_DIR
¶ ↑This constant will point at a path such as this one here:
/Programs/Ruby/2.6.3/lib/ruby/site_ruby/2.6.0/dictionaries/yaml/
#¶ ↑
- ENGLISH_WORDS
#¶ ↑
ENGLISH_WORDS
¶ ↑This constant is no longer that important because we can now automatically infer the name of the .yml file.
#¶ ↑
- ENGLISH_YAML_FILE
- FILE_ENGLISH_DICTIONARY
- FILE_ENGLISH_WORDS
- FILE_ITALIAN_DICTIONARY
- ITALIAN_WORDS
#¶ ↑
ITALIAN_WORDS
¶ ↑This constant is no longer that important because we can now automatically infer the name of the .yml file.
#¶ ↑
- ITALIAN_YAML_FILE
- LAST_UPDATE
#¶ ↑
LAST_UPDATE
¶ ↑#¶ ↑
- LPAD
#¶ ↑
LPAD
¶ ↑#¶ ↑
- MAIN_ENCODING
- MY_DICTIONARIES
#¶ ↑
MY_DICTIONARIES
¶ ↑This constant is only valid for my own system.
#¶ ↑
- N
#¶ ↑
Dictionaries::N
¶ ↑#¶ ↑
- PROJECT_BASE_DIRECTORY
#¶ ↑
Dictionaries::PROJECT_BASE_DIRECTORY
¶ ↑#¶ ↑
- PROJECT_YAML_DIRECTORY
#¶ ↑
PROJECT_YAML_DIRECTORY
¶ ↑#¶ ↑
- RUBY_SRC
- SCIENCE_DIR
#¶ ↑
SCIENCE_DIR
¶ ↑#¶ ↑
- SHALL_WE_DOWNCASE
#¶ ↑
SHALL_WE_DOWNCASE
¶ ↑#¶ ↑
- STORE_LAST_ENGLISH_QUESTION_ASKED_HERE
#¶ ↑
STORE_LAST_ENGLISH_QUESTION_ASKED_HERE
¶ ↑#¶ ↑
- STORE_LAST_ITALIAN_QUESTION_ASKED_HERE
#¶ ↑
STORE_LAST_ITALIAN_QUESTION_ASKED_HERE
¶ ↑#¶ ↑
- STORE_LINE_NUMBER_HERE
#¶ ↑
STORE_LINE_NUMBER_HERE
¶ ↑#¶ ↑
- URL_FOR_DICT_LEO
#¶ ↑
Dictionaries::URL_FOR_DICT_LEO
¶ ↑#¶ ↑
- URL_FOR_ITALIAN_DICTIONARY
#¶ ↑
URL_FOR_ITALIAN_DICTIONARY
¶ ↑#¶ ↑
- USE_THIS_ENCODING
#¶ ↑
USE_THIS_ENCODING
¶ ↑This is the main encoding to use.
#¶ ↑
- VERSION
#¶ ↑
VERSION
¶ ↑#¶ ↑
Public Class Methods
#¶ ↑
Dictionaries[]¶ ↑
Currently this method will always reload the main file. In the future we may change this approach, but for now this has to suffice (May 2019).
The method will return either the translated string - or nil. Nil indicates that the main Hash does not include that key; in other words, that the word is not registered.
#¶ ↑
# File lib/dictionaries/toplevel_methods/misc.rb, line 68 def self.[](i = ARGV) i = i.join.strip if i.is_a? Array this_file = Dictionaries.main_file? if this_file.nil? Dictionaries.set_main_file(:default) # Must initialize it in this case. this_file = Dictionaries.main_file? end if this_file dataset = YAML.load_file(this_file) dataset[i] # Return the result here. else nil end end
#¶ ↑
Dictionaries.english_to_german
¶ ↑
This method will return nil if the key has not been found in the Dictionaries
dataset.
#¶ ↑
# File lib/dictionaries/toplevel_methods/english_to_german.rb, line 18 def self.english_to_german(i) i = i.join.strip if i.is_a? Array converted_word = nil if Dictionaries.has_key? i converted_word = Dictionaries[i] end converted_word end
#¶ ↑
Dictionaries.generate_pdf_file
¶ ↑
This method can be used to generate a .pdf file.
#¶ ↑
# File lib/dictionaries/toplevel_methods/misc.rb, line 19 def self.generate_pdf_file( use_this_font = 'Courier', # Helvetica-Bold' # #'Times-Roman' use_this_font_size = 12 ) require 'prawn' ::Prawn::Fonts::AFM.hide_m17n_warning = true right_arrow = ' -> '.dup.encode( 'Windows-1252', invalid: :replace, undef: :replace, replace: '' ) dataset = YAML.load_file(Dictionaries.file_english) into = File.absolute_path('english_to_german_dictionary.pdf') Prawn::Document.generate(into) { font use_this_font font_size use_this_font_size text( "English to German dictionary: "\ "#{dataset.keys.size.to_s} translated words\n\n" ) # ===================================================================== # # Iterate over our exam dataset next. # ===================================================================== # dataset.each_pair {|key, value| indent(8) { key = key.dup if key.frozen? value = value.dup if value.frozen? result = key.encode( 'Windows-1252', invalid: :replace, undef: :replace, replace: '' )+ right_arrow+ value.encode( 'Windows-1252', invalid: :replace, undef: :replace, replace: '' ) text(result, {size: 10}) } } } e 'Stored into `'+into+'`.' end
#¶ ↑
Dictionaries.gtk_widget
¶ ↑
This toplevel-method can be used to return the gtk-widget, which can then be embedded by other ruby-gtk applications, in particular admin_panel.rb of the gtk_paradise project.
#¶ ↑
# File lib/dictionaries/gui/gtk3/dictionary/dictionary.rb, line 449 def self.gtk_widget Dictionaries::GUI::Gtk::Dictionary.new end
#¶ ↑
Dictionaries.has_key?
¶ ↑
Query whether the main file of the Dictionaries
namespace, has the given input key at hand.
Returns:
true if the key is included false otherwise
Invocation examples:
Dictionaries.has_key? 'apprehensions' # => true Dictionaries.has_key? 'apprehensio'
#¶ ↑
# File lib/dictionaries/toplevel_methods/has_key.rb, line 28 def self.has_key?(this_key) Dictionaries::AskEnglishWord.dataset?.has_key?(this_key) end
#¶ ↑
Dictionaries.parse_commandline
¶ ↑
#¶ ↑
# File lib/dictionaries/commandline/parse_commandline.rb, line 26 def self.parse_commandline( i = ARGV ) if i.is_a? Array i.each {|entry| parse_commandline(entry) } else case i # case tag # ===================================================================== # # === dictionaries --stats # # This entry point allows the user to show some statistics about # this project. # ===================================================================== # when /^-?-?stats\??$/i, /^-?-?statistics\??$/i Dictionaries::Statistics.report # ===================================================================== # # === dictionaries --read=www.nytimes.com.html # ===================================================================== # when /^-?-?read=(.+)$/i _ = $1.to_s.dup require 'dictionaries/toplevel_methods/misc.rb' if File.exist? _ e 'Finding unique words ... this may take a while. Please be' e 'patient.' unique_words = ::Dictionaries.return_unique_words_from_this_file(_, :default, :remove_HTML_tags).sort pp unique_words e 'n entries: '+unique_words.size.to_s else e 'No file exists at '+sfile(_)+'.' end # ===================================================================== # # === dictionaries --gui # ===================================================================== # when /^-?-?gui$/i, /^-?-?gtk$/i, /^-?-?start(-|_)?gtk$/i start_gtk_component # ===================================================================== # # === dictionaries --sinatra # ===================================================================== # when /^-?-?sinatra$/i require 'dictionaries/sinatra/app.rb' Dictionaries.start_sinatra_interface # ===================================================================== # # === dictionaries --n-words? # ===================================================================== # when /^-?-?n(_|-)?words\??$/i e "#{Colours.sfancy(Dictionaries.n_entries?)} english-to-german "\ "translations are presently registered in this project." # ===================================================================== # # === dictionaries --help # ===================================================================== # when /help/ show_help end end end
#¶ ↑
Dictionaries.path_to_the_english_file?
¶ ↑
This method may return a String, as a file path, such as “/home/Programs/Ruby/2.7.2/lib/ruby/site_ruby/2.7.0/dictionaries/yaml/english.yml”.
#¶ ↑
# File lib/dictionaries/ask_english_word.rb, line 123 def self.path_to_the_english_file? Dictionaries.main_file? end
#¶ ↑
Dictionaries.return_array_of_translated_words_from_online_leo
¶ ↑
This method can be used to query the online dictionary from leo.
It will then return an Array of translations.
Note that this currently only works for the translation from english to german, not the other way around, even though that is not too hard to implement either.
#¶ ↑
# File lib/dictionaries/toplevel_methods/misc.rb, line 94 def self.return_array_of_translated_words_from_online_leo(this_word) if this_word.is_a? Array this_word = this_word.join(' ').strip end require 'open-uri' remote_url = "https://dict.leo.org/german-english/#{this_word}" remote_dataset = URI.open(remote_url).read # ======================================================================= # # See: https://rubular.com/r/OOXwAc6PVqjU5Q # ======================================================================= # use_this_regex = /<words><word>([a-zA-Z\s]+)\<\/word><\/words>/ scanned_results = remote_dataset.scan(use_this_regex).flatten # ======================================================================= # # This result may look like so: # [["cat"], ["die Katze"] # We have to sanitize it still. # ======================================================================= # scanned_results.reject! {|entry| entry.start_with?(this_word) } return scanned_results # Note that the full Array is returned. end
#¶ ↑
Dictionaries.return_name_from_compound
¶ ↑
This method will return ‘italian’ if the input is ‘ask_italian_word’.
#¶ ↑
# File lib/dictionaries/toplevel_methods/main_file.rb, line 32 def self.return_name_from_compound( i = Dictionaries.main_file? ) i = i.to_s i = File.basename(i).gsub(/.rb/, '').gsub(/.yml/, '') if i.include? '_' i = i.split('_')[1] # Grab the middle part here. end i end
#¶ ↑
Dictionaries.return_unique_words_from_this_file
¶ ↑
This method will read in the words from an existing local file.
Some files may be huge, though, and then this method becomes quite useless, so we really should discard a lot of data after a certain threshold, to keep memory usage low.
#¶ ↑
# File lib/dictionaries/toplevel_methods/misc.rb, line 125 def self.return_unique_words_from_this_file( this_file, optional_arguments = :english, remove_html_tags = false ) case remove_html_tags # ======================================================================= # # === :remove_HTML_tags # ======================================================================= # when :remove_HTML_tags remove_html_tags = true end dataset = nil if this_file.is_a? Array this_file = this_file.join(' ').strip end if this_file and File.file?(this_file) dataset = File.read(this_file) if remove_html_tags require 'cyberweb/toplevel_methods/misc.rb' dataset = ::Cyberweb.remove_html(dataset) end end result = [] # ← This variable will store the unique words found in the file. case optional_arguments # ======================================================================= # # === :english # ======================================================================= # when :english, :default # ===================================================================== # # Query unknown words in english. We will compare the words to the # available variants. # ===================================================================== # result = dataset.scan(/\w+/).map {|entry| entry.downcase } # ← Obtain all words here. # ===================================================================== # # Next, reject those that are registered: # ===================================================================== # result.reject! {|this_word| ::Dictionaries.has_key?(this_word) } end if dataset return result.uniq.sort end
#¶ ↑
Dictionaries.set_main_file
¶ ↑
Use this method to designate the main yaml file which should contain the language-specific translations.
If the input includes a ‘/’ token and also ends with ‘.rb’ then we assume that it may be in the form of this:
/home/x/programming/ruby/src/dictionaries/lib/dictionaries/ask_italian_word.rb
So we will instead use the second part of the last part.
Usage example:
Dictionaries.set_main_file(THIS_FILE)
#¶ ↑
# File lib/dictionaries/toplevel_methods/main_file.rb, line 61 def self.set_main_file(i = :default_file) case i when :default_file, :default i = FILE_ENGLISH_WORDS end if i.include? '/' if i.end_with? '.rb' i = File.basename(i).sub(/.rb^/,'') if i.include? '_' i = Dictionaries.return_name_from_compound(i) # Grab the middle part here. i = DICTIONARIES_DIR+i+'.yml' end end end @main_file = i end
#¶ ↑
Dictionaries.show_help
¶ ↑
To invoke this, try:
dictionaries --help
#¶ ↑
# File lib/dictionaries/toplevel_methods/show_help.rb, line 19 def self.show_help help_string = <<EOF These options are currently available: --n_words? # Show how many words are available --gui # Start the GTK GUI; aliases exist to this, such as --gtk --read=www.nytimes.com.html # Read from a specific file, to find unique words EOF e help_string end