Module: Dictionaries

Defined in:
lib/dictionaries/constants.rb,
lib/dictionaries/sinatra/app.rb,
lib/dictionaries/class/colours.rb,
lib/dictionaries/class/constants.rb,
lib/dictionaries/version/version.rb,
lib/dictionaries/ask_english_word.rb,
lib/dictionaries/ask_italian_word.rb,
lib/dictionaries/gui/tk/dictionary.rb,
lib/dictionaries/toplevel_methods/e.rb,
lib/dictionaries/gui/gtk2/dictionary.rb,
lib/dictionaries/toplevel_methods/misc.rb,
lib/dictionaries/toplevel_methods/has_key.rb,
lib/dictionaries/sinatra/english_to_german.rb,
lib/dictionaries/toplevel_methods/main_file.rb,
lib/dictionaries/toplevel_methods/show_help.rb,
lib/dictionaries/toplevel_methods/is_on_roebe.rb,
lib/dictionaries/commandline/parse_commandline.rb,
lib/dictionaries/class/ask_word_from_dictionary.rb,
lib/dictionaries/class/set_search_for_this_word.rb,
lib/dictionaries/gui/gtk3/dictionary/dictionary.rb,
lib/dictionaries/project/project_base_directory.rb,
lib/dictionaries/project/project_yaml_directory.rb,
lib/dictionaries/toplevel_methods/english_to_german.rb,
lib/dictionaries/gui/shared_code/dictionaries/dictionary_module.rb

Overview

#

Dictionaries::GUI::Gtk::DictionaryModule

#

require 'dictionaries/gui/shared_code/dictionaries/dictionary_module.rb' include ::Dictionaries::GUI::Gtk::DictionaryModule

#

Defined Under Namespace

Modules: GUI Classes: AskEnglishWord, AskItalianWord, AskWordFromDictionary, Sinatra

Constant Summary collapse

N =
#

Dictionaries::N

#
"\n"
URL_FOR_DICT_LEO =
#

Dictionaries::URL_FOR_DICT_LEO

#
'http://dict.leo.org/'
URL_FOR_ITALIAN_DICTIONARY =
#

URL_FOR_ITALIAN_DICTIONARY

#
'http://www.wordreference.com/iten/'
LPAD =
#

LPAD

#
'  '
RUBY_SRC =

else we just hardcode it anyway.

'/Users/x/DATA/PROGRAMMING_LANGUAGES/RUBY/src/'
MY_DICTIONARIES =
#

MY_DICTIONARIES

This constant is only valid for my own system.

#
RUBY_SRC+'dictionaries/lib/dictionaries/yaml/'
SCIENCE_DIR =
#

SCIENCE_DIR

#
ENV['SCIENCE'].to_s+'/'
DEPOT_INFORMATION_DIR =
#

DEPOT_INFORMATION_DIR

#
'/Depot/Information/'
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/
#
Dictionaries.dictionary_directory?
ENGLISH_WORDS =
#

ENGLISH_WORDS

This constant is no longer that important because we can now automatically infer the name of the .yml file.

#
DICTIONARIES_DIR+'english.yml'
ENGLISH_YAML_FILE =

ENGLISH_YAML_FILE

ENGLISH_WORDS
FILE_ENGLISH_WORDS =

FILE_ENGLISH_WORDS

ENGLISH_WORDS
FILE_ENGLISH_DICTIONARY =
ENGLISH_YAML_FILE
ITALIAN_WORDS =
#

ITALIAN_WORDS

This constant is no longer that important because we can now automatically infer the name of the .yml file.

#
DICTIONARIES_DIR+'italian.yml'
ITALIAN_YAML_FILE =
ITALIAN_WORDS
FILE_ITALIAN_DICTIONARY =
ITALIAN_YAML_FILE
STORE_LINE_NUMBER_HERE =
#

STORE_LINE_NUMBER_HERE

#
DEPOT_INFORMATION_DIR+'line_number_of_the_last_word'
STORE_LAST_ENGLISH_QUESTION_ASKED_HERE =
#

STORE_LAST_ENGLISH_QUESTION_ASKED_HERE

#
DEPOT_INFORMATION_DIR+'last_english_question_asked'
STORE_LAST_ITALIAN_QUESTION_ASKED_HERE =
#

STORE_LAST_ITALIAN_QUESTION_ASKED_HERE

#
DEPOT_INFORMATION_DIR+'last_italian_question_asked'
SHALL_WE_DOWNCASE =
#

SHALL_WE_DOWNCASE

#
true
USE_THIS_ENCODING =
#

USE_THIS_ENCODING

This is the main encoding to use.

#
'ISO-8859-1'
MAIN_ENCODING =
USE_THIS_ENCODING
DEFAULT_DELAY =
#

DEFAULT_DELAY

Specify how long to wait before revealing the translated word.

#
1.6
VERSION =
#

VERSION

#
'0.2.184'
LAST_UPDATE =
#

LAST_UPDATE

#
'03.05.2021'
PROJECT_BASE_DIRECTORY =
#

Dictionaries::PROJECT_BASE_DIRECTORY

#
File.absolute_path("#{__dir__}/..")+'/'
PROJECT_YAML_DIRECTORY =
#

PROJECT_YAML_DIRECTORY

#
"#{PROJECT_BASE_DIRECTORY}yaml/"

Class Method Summary collapse

Class Method Details

.[](i = ARGV) ⇒ Object

#

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.

#

24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dictionaries/toplevel_methods/misc.rb', line 24

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

.ask_english_wordObject

#

Dictionaries.ask_english_word

#

113
114
115
# File 'lib/dictionaries/ask_english_word.rb', line 113

def self.ask_english_word
  AskEnglishWord.new
end

.ask_italian_wordObject

#

Dictionaries.ask_italian_word

#

73
74
75
# File 'lib/dictionaries/ask_italian_word.rb', line 73

def self.ask_italian_word
  AskItalianWord.new
end

.e(i = '') ⇒ Object

#

Dictionaries.e

#

12
13
14
# File 'lib/dictionaries/toplevel_methods/e.rb', line 12

def self.e(i = '')
  puts i
end

.english_to_german(i) ⇒ Object

#

Dictionaries.english_to_german

This method will return nil if the key has not been found in the Dictionaries dataset.

#

18
19
20
21
22
23
24
25
# 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

.file_englishObject

#

Dictionaries.file_english

#

82
83
84
# File 'lib/dictionaries/constants.rb', line 82

def self.file_english
  FILE_ENGLISH_DICTIONARY
end

.gtk_widgetObject

#

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.

#

358
359
360
# File 'lib/dictionaries/gui/shared_code/dictionaries/dictionary_module.rb', line 358

def self.gtk_widget
  Dictionaries::GUI::Gtk::Dictionary.new
end

.has_key?(this_key) ⇒ Boolean

#

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'
#

Returns:

  • (Boolean)

28
29
30
# File 'lib/dictionaries/toplevel_methods/has_key.rb', line 28

def self.has_key?(this_key)
  Dictionaries::AskEnglishWord.dataset?.has_key?(this_key)
end

.is_on_roebe?Boolean

#

Dictionaries.is_on_roebe?

#

Returns:

  • (Boolean)

12
13
14
# File 'lib/dictionaries/toplevel_methods/is_on_roebe.rb', line 12

def self.is_on_roebe?
  (ENV['IS_ROEBE'].to_s == '1')
end

.main_file?Boolean

#

Dictionaries.main_file?

Query method over the @main_file module-level instance variable.

#

Returns:

  • (Boolean)

84
85
86
# File 'lib/dictionaries/toplevel_methods/main_file.rb', line 84

def self.main_file?
  @main_file
end

.n_entries?Boolean

#

Dictionaries.n_entries?

#

Returns:

  • (Boolean)

130
131
132
# File 'lib/dictionaries/ask_english_word.rb', line 130

def self.n_entries?
  ::Dictionaries::AskEnglishWord.n_entries?
end

.new(i = nil) ⇒ Object

#

Dictionaries.new

#

682
683
684
# File 'lib/dictionaries/class/ask_word_from_dictionary.rb', line 682

def self.new(i = nil)
  Dictionaries::AskWordFromDictionary.new(i)
end

.parse_commandline(i = ARGV) ⇒ Object

#

Dictionaries.parse_commandline

#

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dictionaries/commandline/parse_commandline.rb', line 25

def self.parse_commandline(
    i = ARGV
  )
  if i.is_a? Array
    i.each {|entry| parse_commandline(entry) }
  else
    case i # case tag
    # ===================================================================== #
    # === 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

.path_to_the_english_file?Boolean

#

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”.

#

Returns:

  • (Boolean)

123
124
125
# File 'lib/dictionaries/ask_english_word.rb', line 123

def self.path_to_the_english_file?
  Dictionaries.main_file?
end

.project_base_dir?Boolean

#

Dictionaries.project_base_dir?

#

Returns:

  • (Boolean)

18
19
20
# File 'lib/dictionaries/project/project_base_directory.rb', line 18

def self.project_base_dir?
  PROJECT_BASE_DIRECTORY
end

.project_yaml_dir?Boolean

#

Dictionaries.project_yaml_dir?

#

Returns:

  • (Boolean)

21
22
23
# File 'lib/dictionaries/project/project_yaml_directory.rb', line 21

def self.project_yaml_dir?
  PROJECT_YAML_DIRECTORY
end

.return_array_of_translated_words_from_online_leo(this_word) ⇒ Object

#

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.

#

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dictionaries/toplevel_methods/misc.rb', line 50

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

.return_name_from_compound(i = Dictionaries.main_file?) ⇒ Object

#

Dictionaries.return_name_from_compound

This method will return 'italian' if the input is 'ask_italian_word'.

#

32
33
34
35
36
37
38
39
40
41
# 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

.return_unique_words_from_this_file(this_file, optional_arguments = :english) ⇒ Object

#

Dictionaries.return_unique_words_from_this_file

#

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/dictionaries/toplevel_methods/misc.rb', line 75

def self.return_unique_words_from_this_file(
    this_file,
    optional_arguments = :english
  )
  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)
  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
end

.set_main_file(i = :default_file) ⇒ Object

#

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:

/Users/x/DATA/PROGRAMMING_LANGUAGES/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)
#

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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

.show_helpObject

#

Dictionaries.show_help

To invoke this, try:

dictionaries --help
#

19
20
21
22
23
24
25
26
27
28
# 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

EOF
  e help_string
end

.start_gtk_componentObject

#

Dictionaries.start_gtk_component

This module-method will start the GTK component.

#

17
18
19
20
# File 'lib/dictionaries/commandline/parse_commandline.rb', line 17

def self.start_gtk_component
  require 'dictionaries/gui/gtk3/dictionary/dictionary.rb'
  Dictionaries::GUI::Gtk::Dictionary.run
end

.start_sinatra_interfaceObject

#

Dictionaries.start_sinatra_interface

This method can be used to start the sinatra interface.

#

93
94
95
96
# File 'lib/dictionaries/sinatra/app.rb', line 93

def self.start_sinatra_interface
  e 'Trying to start the sinatra-interface of the Dictionaries project.'
  ::Dictionaries::Sinatra.run!
end

.translate(this_word = nil) ⇒ Object

#

Dictionaries.translate

#

674
675
676
677
# File 'lib/dictionaries/class/ask_word_from_dictionary.rb', line 674

def self.translate(this_word = nil)
  _ = Dictionaries::AskWordFromDictionary.new(this_word, :default_file, :dont_run_yet)
  _.translate(this_word)
end