Module: Dictionaries

Defined in:
lib/dictionaries/base/base.rb,
lib/dictionaries/class/class.rb,
lib/dictionaries/project/project.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/constants/constants.rb,
lib/dictionaries/statistics/statistics.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/helper_module/helper_module.rb,
lib/dictionaries/spell_checker/spell_checker.rb,
lib/dictionaries/toplevel_methods/is_on_roebe.rb,
lib/dictionaries/commandline/parse_commandline.rb,
lib/dictionaries/toplevel_methods/english_to_german.rb,
lib/dictionaries/gui/universal_widgets/dictionary/dictionary.rb

Overview

#

require ‘dictionaries/toplevel_methods/english_to_german.rb’

#

Defined Under Namespace

Modules: GUI, HelperModule, Statistics Classes: AskEnglishWord, AskItalianWord, AskWordFromDictionary, Base, Sinatra, SpellChecker

Constant Summary collapse

PROJECT_BASE_DIRECTORY =
#

Dictionaries::PROJECT_BASE_DIRECTORY

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

PROJECT_YAML_DIRECTORY

#
"#{PROJECT_BASE_DIRECTORY}yaml/"
VERSION =
#

VERSION

#
'0.3.86'
LAST_UPDATE =
#

LAST_UPDATE

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

'/home/x/programming/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

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.

#


201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/dictionaries/toplevel_methods/misc.rb', line 201

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

.create_javascript_file(into = File.absolute_path('dictionary.js'), dataset = english_key_values? ) ⇒ Object

#

Dictionaries.create_javascript_file

This method will create a .js file, into the current working directory.

Invocation example:

require 'dictionaries'; result = Dictionaries.create_javascript_file
#


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dictionaries/toplevel_methods/misc.rb', line 39

def self.create_javascript_file(
    into    = File.absolute_path('dictionary.js'),
    dataset = english_key_values? 
  )
  what = <<-EOF
function return_english_dictionary() {

var dictionary = {};

EOF
  what = what.dup
  dataset.each {|key, value|
    value = value.gsub(/"/,'\"') if value.include?('"')
    what << "  dictionary[\"#{key}\"] = \"#{value}\";\n"
  }
  what << "  return dictionary;\n"
  what << "}\n"
  SaveFile.write_what_into(what, into)
  return into
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_key_values?(this_file = Dictionaries.main_file?) ⇒ Boolean

#

Dictionaries.english_key_values?

#

Returns:

  • (Boolean)


22
23
24
25
26
27
# File 'lib/dictionaries/toplevel_methods/misc.rb', line 22

def self.english_key_values?(
    this_file = Dictionaries.main_file?
  )
  dataset = YAML.load_file(this_file)
  return dataset
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

#


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

def self.file_english
  FILE_ENGLISH_DICTIONARY
end

.generate_pdf_file(use_this_font = 'Courier', use_this_font_size = 12) ⇒ Object

#

Dictionaries.generate_pdf_file

This method can be used to generate a .pdf file.

#


65
66
67
68
69
70
71
72
73
74
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
# File 'lib/dictionaries/toplevel_methods/misc.rb', line 65

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

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

#


508
509
510
# File 'lib/dictionaries/gui/universal_widgets/dictionary/dictionary.rb', line 508

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

#


900
901
902
# File 'lib/dictionaries/class/class.rb', line 900

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

.parse_commandline(i = ARGV) ⇒ Object

#

Dictionaries.parse_commandline

#


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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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

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


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

def self.project_base_dir?
  PROJECT_BASE_DIRECTORY
end

.project_yaml_dir?Boolean

#

Dictionaries.project_yaml_dir?

#

Returns:

  • (Boolean)


32
33
34
# File 'lib/dictionaries/project/project.rb', line 32

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.

#


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/dictionaries/toplevel_methods/misc.rb', line 115

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, remove_html_tags = false) ⇒ Object

#

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.

#


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/dictionaries/toplevel_methods/misc.rb', line 146

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/toplevel_methods.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

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

/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)
#


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

.start_gtk_componentObject

#

Dictionaries.start_gtk_component

This module-method will start the GTK component.

#


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

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

.translate(this_word = nil) ⇒ Object

#

Dictionaries.translate

#


892
893
894
895
# File 'lib/dictionaries/class/class.rb', line 892

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