dictionaries 0.3.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of dictionaries might be problematic. Click here for more details.

Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +354 -0
  3. data/bin/dictionaries +7 -0
  4. data/bin/unique_words_in_this_file +7 -0
  5. data/dictionaries.gemspec +84 -0
  6. data/doc/README.gen +292 -0
  7. data/doc/todo/todo.md +8 -0
  8. data/lib/dictionaries/ask_english_word.rb +141 -0
  9. data/lib/dictionaries/ask_italian_word.rb +84 -0
  10. data/lib/dictionaries/base/base.rb +78 -0
  11. data/lib/dictionaries/class/class.rb +903 -0
  12. data/lib/dictionaries/commandline/parse_commandline.rb +85 -0
  13. data/lib/dictionaries/constants/constants.rb +134 -0
  14. data/lib/dictionaries/gui/gtk3/dictionary/dictionary.rb +457 -0
  15. data/lib/dictionaries/gui/tk/README.md +2 -0
  16. data/lib/dictionaries/gui/tk/dictionary.rb +85 -0
  17. data/lib/dictionaries/helper_module/helper_module.rb +60 -0
  18. data/lib/dictionaries/project/project.rb +36 -0
  19. data/lib/dictionaries/require_project/require_project.rb +14 -0
  20. data/lib/dictionaries/sinatra/app.rb +123 -0
  21. data/lib/dictionaries/sinatra/english_to_german.rb +84 -0
  22. data/lib/dictionaries/spell_checker/README.md +5 -0
  23. data/lib/dictionaries/spell_checker/spell_checker.rb +133 -0
  24. data/lib/dictionaries/statistics/statistics.rb +59 -0
  25. data/lib/dictionaries/toplevel_methods/e.rb +16 -0
  26. data/lib/dictionaries/toplevel_methods/english_to_german.rb +31 -0
  27. data/lib/dictionaries/toplevel_methods/has_key.rb +32 -0
  28. data/lib/dictionaries/toplevel_methods/is_on_roebe.rb +16 -0
  29. data/lib/dictionaries/toplevel_methods/main_file.rb +88 -0
  30. data/lib/dictionaries/toplevel_methods/misc.rb +231 -0
  31. data/lib/dictionaries/toplevel_methods/module_methods.rb +9 -0
  32. data/lib/dictionaries/toplevel_methods/show_help.rb +31 -0
  33. data/lib/dictionaries/version/version.rb +19 -0
  34. data/lib/dictionaries/yaml/chinese.yml +25 -0
  35. data/lib/dictionaries/yaml/danish.yml +4 -0
  36. data/lib/dictionaries/yaml/deutsche_fremdw/303/266rter.yml +1 -0
  37. data/lib/dictionaries/yaml/dutch.yml +3 -0
  38. data/lib/dictionaries/yaml/english.yml +3157 -0
  39. data/lib/dictionaries/yaml/farsi.yml +8 -0
  40. data/lib/dictionaries/yaml/finnish.yml +2 -0
  41. data/lib/dictionaries/yaml/italian.yml +532 -0
  42. data/lib/dictionaries/yaml/japanese.yml +15 -0
  43. data/lib/dictionaries/yaml/norwegian.yml +26 -0
  44. data/lib/dictionaries/yaml/polish.yml +2 -0
  45. data/lib/dictionaries/yaml/portugese.yml +41 -0
  46. data/lib/dictionaries/yaml/russian.yml +10 -0
  47. data/lib/dictionaries/yaml/spanish.yml +147 -0
  48. data/lib/dictionaries/yaml/swedish.yml +104 -0
  49. data/lib/dictionaries.rb +1 -0
  50. data/test/translation_example.html +2758 -0
  51. metadata +211 -0
@@ -0,0 +1,903 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # This is the base class for the various dictionaries.
6
+ #
7
+ # That way, we can ask specific words from a specific dictionary,
8
+ # such as english words or italian words and so forth.
9
+ # =========================================================================== #
10
+ # require 'dictionaries/class/class.rb'
11
+ # =========================================================================== #
12
+ require 'dictionaries/base/base.rb'
13
+
14
+ module Dictionaries
15
+
16
+ class AskWordFromDictionary < Base # === Dictionaries::AskWordFromDictionary
17
+
18
+ require 'yaml'
19
+
20
+ # ========================================================================= #
21
+ # Require module-methods of this project next.
22
+ # ========================================================================= #
23
+ require 'dictionaries/toplevel_methods/main_file.rb'
24
+
25
+ # ========================================================================= #
26
+ # Next, load up dictionaries-related ruby code.
27
+ # ========================================================================= #
28
+ require 'dictionaries/constants/constants.rb'
29
+
30
+ begin
31
+ require 'generate_shell_completion'
32
+ rescue LoadError; end
33
+
34
+ begin
35
+ require 'xorg_buffer/module'
36
+ include XorgBuffer
37
+ rescue LoadError; end
38
+
39
+ begin
40
+ require 'save_file/module'
41
+ rescue LoadError; end
42
+
43
+ begin
44
+ require 'open/in_browser/in_browser.rb'
45
+ rescue LoadError; end
46
+
47
+ # ========================================================================= #
48
+ # === NAMESPACE
49
+ # ========================================================================= #
50
+ NAMESPACE = inspect
51
+
52
+ # ========================================================================= #
53
+ # === initialize
54
+ # ========================================================================= #
55
+ def initialize(
56
+ optional_search_for_this_word = nil,
57
+ use_this_file_for_the_dataset = :default_file,
58
+ run_already = true
59
+ )
60
+ register_sigint
61
+ reset
62
+ case optional_search_for_this_word
63
+ when :do_not_run_yet
64
+ run_already = :do_not_run_yet
65
+ optional_search_for_this_word = nil
66
+ end
67
+ # ======================================================================= #
68
+ # === Next, set the main file to use
69
+ # ======================================================================= #
70
+ if use_this_file_for_the_dataset
71
+ set_use_this_file(use_this_file_for_the_dataset)
72
+ load_main_dataset
73
+ end
74
+ set_search_for_this_word(optional_search_for_this_word)
75
+ if run_already.is_a? Hash
76
+ if run_already.has_key? :delay
77
+ set_delay(run_already.delete(:delay))
78
+ end
79
+ run_already = true
80
+ end
81
+ case run_already
82
+ when :dont_run_yet, :do_not_run_yet
83
+ run_already = false
84
+ end
85
+ run if run_already
86
+ end
87
+
88
+ # ========================================================================= #
89
+ # === green
90
+ # ========================================================================= #
91
+ def green
92
+ Colours::GREEN
93
+ end
94
+
95
+ # ========================================================================= #
96
+ # === brown
97
+ # ========================================================================= #
98
+ def brown(i = '')
99
+ "#{::Colours::BROWN}#{i}#{revert}"
100
+ end
101
+
102
+ # ========================================================================= #
103
+ # === sfancy
104
+ # ========================================================================= #
105
+ def sfancy(i)
106
+ ::Colours.sfancy(i)
107
+ end
108
+
109
+ # ========================================================================= #
110
+ # === simp
111
+ # ========================================================================= #
112
+ def simp(i)
113
+ ::Colours.simp(i)
114
+ end; alias simportant simp # === simportant
115
+
116
+ # ========================================================================= #
117
+ # === sfile
118
+ # ========================================================================= #
119
+ def sfile(i)
120
+ ::Colours.sfile(i)
121
+ end
122
+
123
+ # ========================================================================= #
124
+ # === sdir
125
+ # ========================================================================= #
126
+ def sdir(i)
127
+ ::Colours.sdir(i)
128
+ end
129
+
130
+ # ========================================================================= #
131
+ # === reset
132
+ # ========================================================================= #
133
+ def reset
134
+ # ======================================================================= #
135
+ # === @original_words
136
+ # ======================================================================= #
137
+ @original_words = {} # A Hash.
138
+ # ======================================================================= #
139
+ # === @words
140
+ # ======================================================================= #
141
+ @words = nil
142
+ # ======================================================================= #
143
+ # === @be_verbose
144
+ # ======================================================================= #
145
+ @be_verbose = true
146
+ # ======================================================================= #
147
+ # === @store_line_number_where
148
+ # ======================================================================= #
149
+ @store_line_number_where = STORE_LINE_NUMBER_HERE
150
+ # ======================================================================= #
151
+ # === @ask_the_question
152
+ # ======================================================================= #
153
+ @ask_the_question = true # Whether to ask the question or not.
154
+ set_use_this_file # Set to nil initially.
155
+ set_delay
156
+ end
157
+
158
+ # ========================================================================= #
159
+ # === set_delay (delay tag)
160
+ # ========================================================================= #
161
+ def set_delay(i = DEFAULT_DELAY)
162
+ @delay = i
163
+ end
164
+
165
+ # ========================================================================= #
166
+ # === find_all_matches_for
167
+ # ========================================================================= #
168
+ def find_all_matches_for(i) # Must return an Array.
169
+ selection = @words.select {|key,value| key.start_with? i }.keys
170
+ return selection
171
+ end
172
+
173
+ # ========================================================================= #
174
+ # === available_keys?
175
+ # ========================================================================= #
176
+ def available_keys?
177
+ @words.keys if @words
178
+ end; alias array available_keys? # === array
179
+
180
+ # ========================================================================= #
181
+ # === find_this_word_in_yaml_file
182
+ #
183
+ # This method will give us the correct line number.
184
+ # ========================================================================= #
185
+ def find_this_word_in_yaml_file(this_word)
186
+ _ = File.readlines(
187
+ @use_this_file, encoding: USE_THIS_ENCODING
188
+ )
189
+ _ = _.index() {|x| x =~ /^#{this_word}/ }
190
+ line_number = _.to_i + 1
191
+ save_line_number(line_number)
192
+ save_last_question_asked(this_word)
193
+ end
194
+
195
+ # ========================================================================= #
196
+ # === show_every_word
197
+ #
198
+ # This will simply show every word.
199
+ # ========================================================================= #
200
+ def show_every_word
201
+ @original_words.each_pair {|key, value|
202
+ e "#{key.to_s.ljust(20)} -> #{value}"
203
+ }
204
+ end
205
+
206
+ # ========================================================================= #
207
+ # === return_random_word
208
+ # ========================================================================= #
209
+ def return_random_word
210
+ available_keys?.sample
211
+ end
212
+
213
+ # ========================================================================= #
214
+ # === generate_tab_completion (tab tag)
215
+ #
216
+ # The first argument should be the name of the dictionary in question,
217
+ # such as 'english' or 'italian'.
218
+ #
219
+ # To generate the tab completion, do this:
220
+ #
221
+ # askeng TAB
222
+ #
223
+ # ========================================================================= #
224
+ def generate_tab_completion(
225
+ this_language = 'english'
226
+ )
227
+ require 'generate_shell_completion'
228
+ dataset = @original_words.keys # cat $SCI/YAML/DICTIONARIES/english.yml
229
+ # Next, determine where to store the completion stuff.
230
+ generate_where =
231
+ ENV['MY_OS'].to_s+'/LINUX/SHELL/SCRIPTS/'+this_language+'_completion.sh'
232
+ _ = GenerateShellCompletion::GenerateShellCompletion.new(:do_not_run_yet)
233
+ _.add_data(dataset)
234
+ _.set_file(generate_where)
235
+ _.set_name_of_function(this_language.to_sym)
236
+ _.add_aliases %w( askeng ask_eng )
237
+ _.generate
238
+ end
239
+
240
+ # ========================================================================= #
241
+ # === reveal_the_answer
242
+ #
243
+ # Show the answer here.
244
+ # ========================================================================= #
245
+ def reveal_the_answer
246
+ @result = @original_words[@search_for_this_word].to_s
247
+ _ = @result
248
+ begin
249
+ _[0,1] = _[0,1].upcase
250
+ rescue Exception => error
251
+ opne 'An exception happened at line '+simp(__LINE__.to_s)+' when '\
252
+ 'we tried to use .upcase.'
253
+ opne 'The file was '+sfile(__FILE__)
254
+ opne 'The difficult word in question was `'+simp(_)+'`.'
255
+ opne 'The encoding in use was '+simp(_.encoding.to_s)+'.'
256
+ pp error
257
+ begin
258
+ _ = _.encode(USE_THIS_ENCODING)
259
+ _[0,1] = _[0,1].upcase
260
+ rescue Exception => error
261
+ pp error
262
+ end
263
+ end
264
+ _ = sanitize_answer(_)
265
+ if _.include? '[]' # Chop off [] token, if it is part of the String.
266
+ _ = _.force_encoding(USE_THIS_ENCODING) if _.encoding.to_s.include? 'UTF'
267
+ _ = _.delete('[').delete(']').strip
268
+ end
269
+ # ======================================================================= #
270
+ # Finally show the answer:
271
+ # ======================================================================= #
272
+ e "#{N}#{simportant(_)}#{rev}.#{N}#{N}"
273
+ end; alias reveal_answer reveal_the_answer # === reveal_answer
274
+
275
+ # ========================================================================= #
276
+ # === register_sigint
277
+ # ========================================================================= #
278
+ def register_sigint
279
+ Signal.trap('SIGINT') { exit }
280
+ end
281
+
282
+ # ========================================================================= #
283
+ # === is_included?
284
+ # ========================================================================= #
285
+ def is_included?(i)
286
+ @original_words.has_key?(i)
287
+ end
288
+
289
+ # ========================================================================= #
290
+ # === feedback_help_options
291
+ #
292
+ # If you wish to trigger this, do:
293
+ #
294
+ # askeng --help
295
+ #
296
+ # ========================================================================= #
297
+ def feedback_help_options # help tag
298
+ e
299
+ e LPAD+sfancy('TOTAL')+' # Feedback how many keys are registered, '+
300
+ 'also aliased to '+sfancy('STATS')+'.'
301
+ e LPAD+sfancy('OPEN')+' # Open '+sfile(File.basename(main_file?))+
302
+ ' and '+sfile('ask_english_word.rb')+' in the main editor.'
303
+ e LPAD+sfancy('SOLVED')+' # Set the last word as solved.'
304
+ e LPAD+sfancy('URL')+' # Show the URL for the dictionary.'
305
+ e LPAD+sfancy('REPEAT')+' # Repeat the last question asked.'
306
+ e LPAD+sfancy('DELAY?')+' # Feedback the delay we use.'
307
+ e LPAD+sfancy('STATS')+' # To show some statistics, especially '\
308
+ 'the amount of registered words.'
309
+ e LPAD+sfancy('FILE?')+' # Show the main .yml file in question.'
310
+ e
311
+ exit
312
+ end
313
+
314
+ # ========================================================================= #
315
+ # === search_for?
316
+ # ========================================================================= #
317
+ def search_for?
318
+ @search_for_this_word
319
+ end
320
+
321
+ # ========================================================================= #
322
+ # === words?
323
+ # ========================================================================= #
324
+ def words?
325
+ @words
326
+ end; alias dataset? words? # === dataset?
327
+
328
+ # ========================================================================= #
329
+ # === be_verbose?
330
+ # ========================================================================= #
331
+ def be_verbose?
332
+ @be_verbose
333
+ end
334
+
335
+ # ========================================================================= #
336
+ # === result?
337
+ # ========================================================================= #
338
+ def result?
339
+ @result
340
+ end; alias result result? # === result
341
+
342
+ # ========================================================================= #
343
+ # === words?
344
+ # ========================================================================= #
345
+ def words?
346
+ @words
347
+ end
348
+
349
+ # ========================================================================= #
350
+ # === sanitize_dataset
351
+ # ========================================================================= #
352
+ def sanitize_dataset # We have to ignore all entries that contain []. We do this only for @words.
353
+ words?.reject! {|key, value|
354
+ value.include? '[]'
355
+ }
356
+ end
357
+
358
+ # ========================================================================= #
359
+ # === on_roebe?
360
+ # ========================================================================= #
361
+ def on_roebe?
362
+ ENV['IS_ROEBE'].to_s == '1'
363
+ end
364
+
365
+ # ========================================================================= #
366
+ # === do_ask_the_question
367
+ # ========================================================================= #
368
+ def do_ask_the_question # This method will ask the english question.
369
+ if search_for?.empty?
370
+ # set_search_for_this_word 'RANDOM'
371
+ # The above ^^^ was changed in August 2023.
372
+ e "#{rev}Please provide an argument to this class, the word that is"
373
+ e "#{rev}to be translated."
374
+ return
375
+ end
376
+ _ = search_for?
377
+ case _
378
+ when 'rand'
379
+ _ = (@search_for_this_word = return_random_word)
380
+ end
381
+ unless is_included? _
382
+ set_xorg_buffer _
383
+ opne "Sorry, the word `#{sfancy(_)}"\
384
+ "#{rev}` is not included."
385
+ opne 'We will thus try to open our english dictionary in our editor'
386
+ opne 'and lookup the word in our browser.'
387
+ open_main_yaml_file_in_main_editor # Added ~ 22.02.2013
388
+ consider_opening_browser_page(_)
389
+ else # Ok, we now know that it is included. Thus, continue.
390
+ if be_verbose?
391
+ e "#{rev}What does the word `#{sfancy(_)}#{rev}` mean? #{show_delay_string}"
392
+ find_this_word_in_yaml_file(_)
393
+ new_buffer = build_url_www_string(_)
394
+ set_xorg_buffer(new_buffer)
395
+ sleep delay?
396
+ reveal_the_answer
397
+ end
398
+ end
399
+ end
400
+
401
+ # ========================================================================= #
402
+ # === rev
403
+ # ========================================================================= #
404
+ def rev
405
+ ::Colours.rev
406
+ end; alias revert rev # === revert
407
+
408
+ # ========================================================================= #
409
+ # === set_xorg_buffer
410
+ # ========================================================================= #
411
+ def set_xorg_buffer(i)
412
+ XorgBuffer[i] if Object.const_defined? :XorgBuffer
413
+ end
414
+
415
+ # ========================================================================= #
416
+ # === store_last_question_asked_where?
417
+ #
418
+ # This method will try to automatically find out where we will store
419
+ # the last question asked.
420
+ # ========================================================================= #
421
+ def store_last_question_asked_where?
422
+ name = Dictionaries.return_name_from_compound(main_file?)
423
+ path = DEPOT_INFORMATION_DIR+'last_'+name+'_question_asked'
424
+ return path
425
+ end
426
+
427
+ # ========================================================================= #
428
+ # === build_url_www_string
429
+ # ========================================================================= #
430
+ def build_url_www_string(i)
431
+ _ = ''.dup
432
+ # ======================================================================= #
433
+ # We need to differ between the english dictionary and the italian
434
+ # dictionary.
435
+ # ======================================================================= #
436
+ language = Dictionaries.return_name_from_compound(main_file?)
437
+ if language == 'english'
438
+ _ << '"' unless _.end_with? '"'
439
+ end
440
+ case language # case tag
441
+ when 'italian'
442
+ _ << URL_FOR_ITALIAN_DICTIONARY
443
+ when 'english' # This is the default.
444
+ _ << URL_FOR_DICT_LEO+'ende/?lp=ende&lang=de&searchLoc=0&cmpType=relaxed&sectHdr=on&spellToler=&search='
445
+ end
446
+ _ << i.to_s
447
+ if language == 'english'
448
+ _ << '"' unless _.end_with? '"' # Append '"' here.
449
+ end
450
+ _
451
+ end
452
+
453
+ # ========================================================================= #
454
+ # === show_help
455
+ #
456
+ # Show the help options.
457
+ # ========================================================================= #
458
+ def show_help
459
+ end
460
+
461
+ # ========================================================================= #
462
+ # === load_dataset
463
+ #
464
+ # Here we will define the important instance variable @original_words.
465
+ # ========================================================================= #
466
+ def load_dataset(
467
+ of_this_file = @use_this_file
468
+ ) # called via reset()
469
+ of_this_file = of_this_file.to_s # We must work with a String.
470
+ if File.exist? of_this_file
471
+ dataset = YAML.load_file(of_this_file)
472
+ @original_words = dataset
473
+ # ^^^ This is the file english.yml
474
+ else
475
+ e 'Did not find a file at '+of_this_file+'.'
476
+ e 'Thus, the @original_words will be empty.'
477
+ e 'If you wish to change this, modify the constant ENGLISH_WORDS'
478
+ e 'to point at a yaml-dataset (we require a hash, thus key: value pairs)'
479
+ @original_words = {}
480
+ end
481
+ @words = @original_words.dup
482
+ sanitize_dataset # Get rid of lines that have [] as entry.
483
+ end; alias load_main_dataset load_dataset # === load_main_dataset
484
+
485
+ # ========================================================================= #
486
+ # === set_use_this_file
487
+ #
488
+ # This sets the main dictionary-file to use, the file path to it.
489
+ # ========================================================================= #
490
+ def set_use_this_file(i = :default)
491
+ case i
492
+ when :english
493
+ i = ENGLISH_WORDS
494
+ when :default_file, :default
495
+ i = main_file?
496
+ end
497
+ @use_this_file = i
498
+ end
499
+
500
+ # ========================================================================= #
501
+ # === load_the_english_file
502
+ # ========================================================================= #
503
+ def load_the_english_file
504
+ set_use_this_file(:english)
505
+ load_main_dataset
506
+ end
507
+
508
+ # ========================================================================= #
509
+ # === use_this_file?
510
+ # ========================================================================= #
511
+ def use_this_file?
512
+ @use_this_file
513
+ end
514
+
515
+ # ========================================================================= #
516
+ # === opnn
517
+ # ========================================================================= #
518
+ def opnn
519
+ Opn.opn(namespace: NAMESPACE)
520
+ end
521
+
522
+ # ========================================================================= #
523
+ # === feedback_statistics
524
+ #
525
+ # Feedback the amount of questions registered. In the future, perhaps
526
+ # we may want to have more statistic-options, so this may be expanded
527
+ # at a later time.
528
+ #
529
+ # To trigger this, do:
530
+ # askeng --stats?
531
+ # ========================================================================= #
532
+ def feedback_statistics
533
+ feedback_total_amount_of_keys(:do_not_exit)
534
+ end
535
+
536
+ # ========================================================================= #
537
+ # === feedback_url
538
+ # ========================================================================= #
539
+ def feedback_url
540
+ opne " -> #{URL_FOR_DICT_LEO}"
541
+ exit
542
+ end
543
+
544
+ # ========================================================================= #
545
+ # === open_yaml_file_in_main_editor
546
+ # ========================================================================= #
547
+ def open_yaml_file_in_main_editor(
548
+ this_file = main_file?
549
+ )
550
+ if on_roebe? # Rewrite for my own system at home.
551
+ this_file = File.basename(this_file)
552
+ this_file = MY_DICTIONARIES+this_file
553
+ end
554
+ _= this_file
555
+ notify_the_user_that_we_will_next_open_this_file(_)
556
+ system "bluefish #{_}"
557
+ end; alias open_main_yaml_file_in_main_editor open_yaml_file_in_main_editor # === open_main_yaml_file_in_main_editor
558
+
559
+ # ========================================================================= #
560
+ # === notify_the_user_that_we_will_next_open_this_file
561
+ # ========================================================================= #
562
+ def notify_the_user_that_we_will_next_open_this_file(this_file)
563
+ opne 'Opening file `'+sfile(this_file)+'` next.'
564
+ end
565
+
566
+ # ========================================================================= #
567
+ # === feedback_total_amount_of_keys
568
+ # ========================================================================= #
569
+ def feedback_total_amount_of_keys(shall_we_exit = true) # KEYS
570
+ case shall_we_exit
571
+ when :do_not_exit
572
+ shall_we_exit = false
573
+ end
574
+ _ = @original_words.size - @words.size
575
+ opne swarn(@original_words.size.to_s)+' english words in total.'
576
+ rounded = sprintf('%0.2f', 100.0 - (@words.size * 100.0 / @original_words.size) )
577
+ opne swarn(@words.size.to_s)+' unanswered english words, '\
578
+ 'thus '+sfancy(_.to_s)+' are already answered. ('+
579
+ simp(rounded.to_s+'%')+')'
580
+ opne 'The yaml file that we we will use is:'
581
+ opne ' '+sfile(main_file?)
582
+ exit if shall_we_exit
583
+ end
584
+
585
+ # ========================================================================= #
586
+ # === open_this_file_here
587
+ # ========================================================================= #
588
+ def open_this_file_here(be_verbose = false)
589
+ _= Dictionaries.main_file?
590
+ if be_verbose
591
+ opnn; notify_the_user_that_we_will_next_open_this_file(_)
592
+ end
593
+ system "bluefish #{_}"
594
+ end
595
+
596
+ # ========================================================================= #
597
+ # === sanitize_answer
598
+ #
599
+ # This may also re-encode the given String object.
600
+ # ========================================================================= #
601
+ def sanitize_answer(i)
602
+ begin
603
+ unless i.encoding.to_s.include? USE_THIS_ENCODING
604
+ i = i.force_encoding(USE_THIS_ENCODING)
605
+ end
606
+ i.gsub!(/ \[\]/,'') if i.include? '[]'
607
+ rescue Exception => error
608
+ opnn; pp error # Propagate the error here to the user.
609
+ end
610
+ return i
611
+ end
612
+
613
+ # ========================================================================= #
614
+ # === save_line_number
615
+ # ========================================================================= #
616
+ def save_line_number(what) # save tag
617
+ begin
618
+ save_file(what, @store_line_number_where)
619
+ rescue => error
620
+ pp error
621
+ pp error.class
622
+ end
623
+ end
624
+
625
+ # ========================================================================= #
626
+ # === set_last_word_solved
627
+ # ========================================================================= #
628
+ def set_last_word_solved # Call this only when you want to solve an english word.
629
+ begin
630
+ require 'roebe/classes/append_to_line.rb'
631
+ _ = Roebe::AppendToLine.new(:english, false) # bl $RUBY_TOOLS/append_to_line.rb
632
+ _.set_line File.read(STORE_LINE_NUMBER_HERE)
633
+ _.run
634
+ rescue LoadError; end
635
+ exit
636
+ end
637
+
638
+ # ========================================================================= #
639
+ # === save_last_question_asked
640
+ # ========================================================================= #
641
+ def save_last_question_asked(what) # Save the last question asked.
642
+ into = store_last_question_asked_where?
643
+ begin
644
+ save_file(what, into)
645
+ rescue Errno::ENOENT
646
+ opne "Can not store into `#{sfile(into)}`."
647
+ end
648
+ end
649
+
650
+ # ========================================================================= #
651
+ # === save_file
652
+ # ========================================================================= #
653
+ def save_file(what, into)
654
+ SaveFile.write_what_into(what, into)
655
+ end
656
+
657
+ # ========================================================================= #
658
+ # === delay?
659
+ # ========================================================================= #
660
+ def delay?
661
+ @delay
662
+ end
663
+
664
+ # ========================================================================= #
665
+ # === feedback_delay
666
+ # ========================================================================= #
667
+ def feedback_delay
668
+ opne "The delay we use is set to: #{sfancy(@delay.to_s)}"
669
+ exit
670
+ end
671
+
672
+ # ========================================================================= #
673
+ # === translate
674
+ # ========================================================================= #
675
+ def translate(i)
676
+ return @original_words[i].to_s
677
+ end
678
+
679
+ # ========================================================================= #
680
+ # === show_delay_string
681
+ # ========================================================================= #
682
+ def show_delay_string(
683
+ delay = delay?
684
+ )
685
+ result = "(Delay is at #{simp(@delay.to_s)}#{rev} seconds)"
686
+ result = '' if delay.to_i == 0
687
+ return result
688
+ end
689
+
690
+ # ========================================================================= #
691
+ # === set_search_for_this_word (menu tag)
692
+ #
693
+ # This method will also have the menu-functionality of the class.
694
+ # ========================================================================= #
695
+ def set_search_for_this_word(
696
+ i = :rand
697
+ )
698
+ i = i.join(' ').strip if i.is_a? Array # This will also handle empty ARGV passed to us.
699
+ i = :rand if i.nil?
700
+ case i
701
+ # ======================================================================= #
702
+ # === :rand
703
+ # ======================================================================= #
704
+ when :rand, 'rand' # We want a random word here.
705
+ i = return_random_word if @words
706
+ end
707
+ # ======================================================================= #
708
+ # We want a String past this point.
709
+ # ======================================================================= #
710
+ i = i.to_s.dup
711
+ # ======================================================================= #
712
+ # We can not downcase the input since we also try to have it match
713
+ # towards regular english letters. It is a dictionary after all.
714
+ # ======================================================================= #
715
+ # === Chop off last char if it is a question mark.
716
+ # ======================================================================= #
717
+ i.chop! if i.end_with? '?'
718
+ case i # case tag
719
+ # ======================================================================= #
720
+ # === askeng --help
721
+ # ======================================================================= #
722
+ when /^-?-?help$/i,
723
+ 'show_help'
724
+ feedback_help_options # This will also exit.
725
+ # ======================================================================= #
726
+ # === askeng --delay?
727
+ # ======================================================================= #
728
+ when /^-?-?delay\??$/i
729
+ feedback_delay
730
+ # ======================================================================= #
731
+ # === askeng --openall
732
+ # ======================================================================= #
733
+ when 'OPEN',
734
+ 'OPE',
735
+ 'OP',
736
+ 'O',
737
+ 'EDIT',
738
+ 'OPEN_ALL',
739
+ /open/,
740
+ 'OPENALL',
741
+ '--openall'
742
+ open_this_file_here(true) # true for "be verbose".
743
+ open_yaml_file_in_main_editor
744
+ exit
745
+ # ======================================================================= #
746
+ # === askeng --n-entries
747
+ # ======================================================================= #
748
+ when 'TOTAL',
749
+ 'KEYS',
750
+ 'KEYSTATS',
751
+ '--n-entries',
752
+ '--nentries',
753
+ 'keys?',
754
+ 'nkeys',
755
+ /-?-?nwords\??/
756
+ feedback_total_amount_of_keys
757
+ # ======================================================================= #
758
+ # === askeng --stats
759
+ # ======================================================================= #
760
+ when '--stats','--statistics','STATS','STATISTICS','STATS?','--stats?'
761
+ feedback_statistics; exit
762
+ # ======================================================================= #
763
+ # === askeng --every_word
764
+ # ======================================================================= #
765
+ when 'SHOW',
766
+ '--show-every-word',
767
+ '--every_word'
768
+ show_every_word
769
+ exit
770
+ # ======================================================================= #
771
+ # === askeng --generate-tab
772
+ # ======================================================================= #
773
+ when 'GENERATE',
774
+ 'TAB',
775
+ 'GENERATE_COMPLETION',
776
+ 'TABCOMPLETION',
777
+ 'COMPLETION',
778
+ '--generate-tab'
779
+ generate_tab_completion
780
+ exit
781
+ # ======================================================================= #
782
+ # === askeng --open-in-browser
783
+ # ======================================================================= #
784
+ when 'OPEN_BROWSER','--open-in-browser'
785
+ open_in_browser
786
+ # ======================================================================= #
787
+ # === askeng --solved
788
+ # ======================================================================= #
789
+ when 'SOLVED',/-?-?solved/
790
+ set_last_word_solved
791
+ # ======================================================================= #
792
+ # === askeng --yaml-file?
793
+ # ======================================================================= #
794
+ when 'FILE?',
795
+ 'YAML_FILE?','--yaml-file?'
796
+ feedback_yaml_file
797
+ exit
798
+ # ======================================================================= #
799
+ # === askeng --open-dictionary
800
+ # ======================================================================= #
801
+ when 'OPEN_DICTIONARY','OPEN_DICT','--open-dictionary'
802
+ open_main_yaml_file_in_main_editor
803
+ exit
804
+ # ======================================================================= #
805
+ # === askeng --repeat
806
+ # ======================================================================= #
807
+ when 'REPEAT','--repeat'
808
+ _ = STORE_LAST_QUESTION_ASKED_HERE
809
+ if File.exist? _
810
+ i = File.read(_)
811
+ else
812
+ opnn; e 'The file `'+sfile(_)+'` was not found, thus we can'
813
+ opnn; e 'not read in anything.'
814
+ exit
815
+ end
816
+ # ======================================================================= #
817
+ # === askeng --url
818
+ # ======================================================================= #
819
+ when '--url','URL'
820
+ feedback_url
821
+ # ======================================================================= #
822
+ # === askeng --random
823
+ # ======================================================================= #
824
+ when /^-?-?random$/i,'RAND','rand' # Pass through here.
825
+ i = return_random_word if available_keys?
826
+ # else tag
827
+ else # Handle assignment cases next.
828
+ if i.include?('=') and i.downcase.include? 'delay'
829
+ new_delay = i.split('=').last.strip
830
+ e 'It seems as if you may want to assign a new delay.'
831
+ e 'We will thus set the last line of this file here'
832
+ e 'with the new delay of:'
833
+ e ' '+sfancy(new_delay)
834
+ what = File.readlines(Dictionaries::AskEnglishWord.main_file?)
835
+ what[-1,1] = new_delay # Set the new entry here.
836
+ SaveFile.write_what_into(what.join, Dictionaries::AskEnglishWord.main_file?)
837
+ exit
838
+ end
839
+ end
840
+ i = i.to_s.dup
841
+ i = i.downcase if SHALL_WE_DOWNCASE
842
+ if i.size == 1 # We assume that the user wants to get a category.
843
+ i = find_all_matches_for(i).sample
844
+ end
845
+ @search_for_this_word = i # We will search for this word here.
846
+ end
847
+
848
+ # ========================================================================= #
849
+ # === feedback_yaml_file
850
+ # ========================================================================= #
851
+ def feedback_yaml_file
852
+ efile main_file?
853
+ end
854
+
855
+ # ========================================================================= #
856
+ # === main_file?
857
+ # ========================================================================= #
858
+ def main_file?
859
+ Dictionaries.main_file?
860
+ end; alias currently_selected_file? main_file? # === currently_selected_file?
861
+
862
+ # ========================================================================= #
863
+ # === consider_opening_browser_page
864
+ # ========================================================================= #
865
+ def consider_opening_browser_page(i = '')
866
+ url = build_url_www_string(i)
867
+ opne 'Consider visiting '+brown(url)+' if you need a translation.'
868
+ Open.in_browser(url) if Object.const_defined?(:Open)
869
+ end
870
+
871
+ # ========================================================================= #
872
+ # === run (run tag)
873
+ # ========================================================================= #
874
+ def run
875
+ load_dataset
876
+ do_ask_the_question if @ask_the_question
877
+ end
878
+
879
+ end
880
+
881
+ # =========================================================================== #
882
+ # === Dictionaries.translate
883
+ # =========================================================================== #
884
+ def self.translate(this_word = nil)
885
+ _ = Dictionaries::AskWordFromDictionary.new(this_word, :default_file, :dont_run_yet)
886
+ _.translate(this_word)
887
+ end
888
+
889
+ # =========================================================================== #
890
+ # === Dictionaries.new
891
+ # =========================================================================== #
892
+ def self.new(i = nil)
893
+ Dictionaries::AskWordFromDictionary.new(i)
894
+ end
895
+
896
+ end
897
+
898
+ if __FILE__ == $PROGRAM_NAME
899
+ pp Dictionaries::AskWordFromDictionary.new
900
+ pp Dictionaries::AskWordFromDictionary.class
901
+ pp Dictionaries::DICTIONARIES_DIR
902
+ pp Dictionaries::ENGLISH_WORDS
903
+ end