cw 0.2.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/.cw_config +18 -0
  3. data/.gitignore +10 -1
  4. data/LICENSE +2 -1
  5. data/README.md +212 -96
  6. data/Rakefile +15 -2
  7. data/VERSION +1 -1
  8. data/Vagrantfile +45 -0
  9. data/cw.gemspec +6 -1
  10. data/data/text/book_to_read.txt +4 -0
  11. data/data/text/english.txt +10000 -0
  12. data/example.rb +172 -106
  13. data/lib/cw.rb +10 -126
  14. data/lib/cw/alphabet.rb +53 -46
  15. data/lib/cw/audio_player.rb +83 -72
  16. data/lib/cw/book.rb +160 -185
  17. data/lib/cw/book_details.rb +38 -49
  18. data/lib/cw/cl.rb +101 -95
  19. data/lib/cw/common_words.rb +76 -0
  20. data/lib/cw/config.rb +50 -0
  21. data/lib/cw/current_word.rb +23 -24
  22. data/lib/cw/cw_dsl.rb +264 -131
  23. data/lib/cw/cw_encoding.rb +63 -69
  24. data/lib/cw/cw_stream.rb +86 -82
  25. data/lib/cw/cw_threads.rb +132 -22
  26. data/lib/cw/element.rb +60 -54
  27. data/lib/cw/file_details.rb +26 -11
  28. data/lib/cw/key_input.rb +53 -35
  29. data/lib/cw/numbers.rb +26 -19
  30. data/lib/cw/os.rb +13 -0
  31. data/lib/cw/play.rb +92 -0
  32. data/lib/cw/print.rb +102 -100
  33. data/lib/cw/process.rb +3 -0
  34. data/lib/cw/progress.rb +20 -17
  35. data/lib/cw/randomize.rb +56 -52
  36. data/lib/cw/repeat_word.rb +59 -66
  37. data/lib/cw/reveal.rb +32 -31
  38. data/lib/cw/rss.rb +52 -48
  39. data/lib/cw/sentence.rb +83 -76
  40. data/lib/cw/speak.rb +8 -4
  41. data/lib/cw/spoken.rb +8 -4
  42. data/lib/cw/str.rb +62 -30
  43. data/lib/cw/test_letters.rb +20 -28
  44. data/lib/cw/test_words.rb +25 -31
  45. data/lib/cw/tester.rb +219 -226
  46. data/lib/cw/text_helpers.rb +19 -15
  47. data/lib/cw/timing.rb +63 -67
  48. data/lib/cw/tone_generator.rb +176 -153
  49. data/lib/cw/tone_helpers.rb +15 -23
  50. data/lib/cw/voice.rb +12 -8
  51. data/lib/cw/words.rb +136 -106
  52. data/run_script_tests.rb +165 -0
  53. data/test/my_words.txt +1 -0
  54. data/test/test_common_words.rb +71 -0
  55. data/test/test_config.rb +98 -0
  56. data/test/test_current_word.rb +62 -0
  57. data/test/test_cw.rb +87 -120
  58. data/test/test_cw_threads.rb +123 -0
  59. data/test/test_filtering.rb +439 -0
  60. data/test/test_params.rb +28 -0
  61. data/test/test_play.rb +51 -0
  62. data/test/test_stream.rb +83 -83
  63. data/test/test_tester.rb +9 -27
  64. data/test/test_timing.rb +212 -0
  65. metadata +94 -12
  66. data/lib/cw/config_file.rb +0 -69
  67. data/lib/cw/monitor_keys.rb +0 -37
  68. data/lib/cw/params.rb +0 -104
@@ -1,37 +1,36 @@
1
1
  # encoding: utf-8
2
2
 
3
- class CurrentWord
3
+ module CWG
4
4
 
5
- include TextHelpers
5
+ class CurrentWord
6
6
 
7
- def initialize
8
- @current_word = ''
9
- end
7
+ include TextHelpers
10
8
 
11
- def current_word
12
- @current_word ||= String.new
13
- end
9
+ def initialize
10
+ @current_word = ''
11
+ end
14
12
 
15
- def push_letter letr
16
- @current_word += letr
17
- end
13
+ def push_letter letr
14
+ @current_word += letr
15
+ end
18
16
 
19
- def to_s
20
- @current_word
21
- end
17
+ def to_s
18
+ @current_word
19
+ end
22
20
 
23
- def clear
24
- @current_word.clear
25
- @current_word = ''
26
- end
21
+ def clear
22
+ @current_word.clear
23
+ end
27
24
 
28
- def strip
29
- @current_word.strip!
30
- end
25
+ def strip
26
+ @current_word.strip!
27
+ end
28
+
29
+ def process_letter letr
30
+ letr.downcase!
31
+ push_letter letr
32
+ end
31
33
 
32
- def process_letter letr
33
- letr.downcase!
34
- push_letter letr
35
34
  end
36
35
 
37
36
  end
@@ -2,175 +2,308 @@
2
2
 
3
3
  # class Cw_dsl provides CW's commands
4
4
 
5
- require_relative 'params'
5
+ module CWG
6
+
7
+ class CwDsl
8
+
9
+ include CWG::Cfg
10
+ include CWG::FileDetails
11
+ [:wpm, :effective_wpm, :frequency, :audio_filename,:audio_dir,
12
+ :book_name, :book_dir, :play_command, :run_default, :command_line,
13
+ :author, :title, :quality, :ebook2cw_path, :list_colour, :list_colour,
14
+ :success_colour, :fail_colour, :name, :tone, :volume, :print_words
15
+ ].each do |method|
16
+ define_method method do |arg = nil|
17
+ arg ? Cfg.config.params[method.to_s] = arg :
18
+ Cfg.config[method.to_s]
19
+ end
20
+ end
6
21
 
7
- class CwDsl
22
+ def initialize
23
+ @words, @cl, @str =
24
+ Words.new, Cl.new, Str.new
25
+ Cfg.reset
26
+ load_common_words# unless @words.exist?
27
+ end
8
28
 
9
- include Params::ParamsSetup
29
+ def words= words
30
+ @words.add words
31
+ end
10
32
 
11
- attr_accessor :cl
33
+ def word_size(size = nil)
34
+ if size
35
+ Cfg.config.params["size"] = size
36
+ @words.word_size size
37
+ end
38
+ Cfg.config["size"]
39
+ end
12
40
 
13
- HERE = File.dirname(__FILE__) + '/'
14
- TEXT = HERE + '../../data/text/'
15
- COMMON_WORDS = TEXT + 'common_words.txt'
16
- MOST_COMMON_WORDS = TEXT + 'most_common_words.txt'
17
- ABBREVIATIONS = TEXT + 'abbreviations.txt'
18
- Q_CODES = TEXT + 'q_codes.txt'
41
+ def word_spacing(spacing)
42
+ Cfg.config.params["word_spacing"] = spacing.to_i
43
+ end
19
44
 
20
- def initialize
21
- @words, @cl, @str =
22
- Words.new, Cl.new, Str.new
23
- Params.init_config
24
- config_defaults
25
- config_files
26
- end
45
+ # Test user against letters rather than words.
46
+ #
27
47
 
28
- def config_defaults
29
- Params.config {
30
- name 'unnamed'
31
- wpm 25
32
- frequency 500
33
- dictionary COMMON_WORDS
34
- }
35
- end
48
+ def test_letters
49
+ Cfg.config.params["no_run"] = true
50
+ test_letters = TestLetters.new
51
+ test_letters.run @words
52
+ end
36
53
 
37
- def config_files
38
- Params.config {
39
- audio_dir 'audio'
40
- audio_filename 'audio_output'
41
- word_filename 'words.txt'
42
- }
43
- end
54
+ #todo
55
+ # def tx
56
+ # @words.add ["abc"]
57
+ # Cfg.config.params["no_run"] = true
58
+ # tx = Tx.new
59
+ # tx.listen @words
60
+ # end
61
+
62
+ # Test user against complete words rather than letters.
63
+ #
64
+
65
+ def test_words
66
+ Cfg.config.params["no_run"] = true
67
+ tw = TestWords.new
68
+ tw.run @words
69
+ end
44
70
 
45
- def words
46
- @words.all
47
- end
71
+ # Repeat word repeats the current word if the word is entered incorrectly
72
+ # (or not entered at all).
48
73
 
49
- def words= words
50
- @words.add words
51
- end
74
+ def repeat_word
75
+ Cfg.config.params["no_run"] = true
76
+ repeat_word = RepeatWord.new
77
+ repeat_word.run @words
78
+ end
52
79
 
53
- def word_size(size = nil)
54
- if size
55
- Params.size = size
56
- @words.word_size size
80
+ # Reveal words only at end of test.
81
+ # Useful for learning to copy `in the head'
82
+
83
+ def reveal
84
+ Cfg.config.params["no_run"] = true
85
+ reveal = Reveal.new
86
+ reveal.run @words
57
87
  end
58
- Params.size
59
- end
60
88
 
61
- def word_count(wordcount)
62
- Params.word_count = wordcount
63
- @words.count wordcount
64
- end
89
+ def sending_practice
90
+ Params.sending_practice = true
91
+ print_letters (:print_early)
92
+ end
65
93
 
66
- def beginning_with(* letters)
67
- @words.beginning_with letters
68
- Params.begin = letters
69
- end
94
+ # Play book using provided arguments.
95
+ # @param [Hash] args the options to play book with.
96
+ # @option args [Integer] :sentences Number of sentences to play
97
+ # @option args [Integer] :duration Number of minutes to play
98
+ # @option args [Boolean] :letter Mark by letter if true else mark by word
70
99
 
71
- def ending_with(* letters)
72
- @words.ending_with letters
73
- Params.end = letters
74
- end
100
+ def read_book args = {}
101
+ Cfg.config.params["no_run"] = true
102
+ details = BookDetails.new
103
+ details.arguments(args)
104
+ book = Book.new details
75
105
 
76
- def including(* letters)
77
- Params.including = letters
78
- @words.including letters
79
- end
106
+ book.run @words
107
+ end
80
108
 
81
- def containing(* letters)
82
- @words.containing letters
83
- end
109
+ # Convert book to mp3.
84
110
 
85
- def no_longer_than(max)
86
- Params.max = max
87
- @words.no_longer_than max
88
- end
111
+ def convert_book args = {}
112
+ details = BookDetails.new
113
+ details.arguments(args)
114
+ book = Book.new details
115
+ Cfg.config.params["no_run"] = true
116
+ book.convert
117
+ end
89
118
 
90
- def no_shorter_than(min)
91
- Params.min = min
92
- @words.no_shorter_than min
93
- end
119
+ # Reads RSS feed (requires an internet connection). Feed can be one of:
120
+ # - bbc:
121
+ # - reuters:
122
+ # - guardian:
123
+ # - quotation:
124
+ # @param [Symbol] source The source of the feed.
125
+ # @param [Integer] article_count Number of articles to play.
126
+
127
+ def read_rss(source, article_count = 3)
128
+ rss, = Rss.new
129
+
130
+ # don't go online if CW_ENV == test
131
+ if(ENV["CW_ENV"] == "test")
132
+ @words.assign ['test', 'rss', 'stub']
133
+ return
134
+ end
135
+ rss.read_rss(source, article_count)
136
+ loop do
137
+ article = rss.next_article
138
+ return unless article
139
+ @words.assign article
140
+ run
141
+ end
142
+ end
94
143
 
95
- def reverse
96
- @words.reverse
97
- end
144
+ def shuffle
145
+ @words.shuffle unless(ENV["CW_ENV"] == "test")
146
+ end
98
147
 
99
- def letters_numbers
100
- @words.letters_numbers
101
- end
148
+ def word_count(wordcount)
149
+ Cfg.config.params["word_count"] = wordcount
150
+ @words.count wordcount
151
+ end
102
152
 
103
- def random_letters_numbers(options = {})
104
- options.merge!(letters_numbers: true)
105
- @words.random_letters_numbers options
106
- end
153
+ def beginning_with(* letters)
154
+ Cfg.config.params["begin"] = letters
155
+ @words.beginning_with
156
+ end
107
157
 
108
- def random_letters(options = {})
109
- @words.random_letters(options)
110
- end
158
+ def ending_with(* letters)
159
+ Cfg.config.params["end"] = letters
160
+ @words.ending_with
161
+ end
111
162
 
112
- def random_numbers(options = {})
113
- @words.random_numbers(options)
114
- end
163
+ def including(* letters)
164
+ Cfg.config.params["including"] = letters
165
+ @words.including
166
+ end
115
167
 
116
- def alphabet(options = {reverse: nil})
117
- @words.alphabet(options)
118
- end
168
+ def containing(* letters)
169
+ Cfg.config.params["containing"] = letters
170
+ @words.containing
171
+ end
119
172
 
120
- def numbers(options = {reverse: nil})
121
- @words.numbers(options)
122
- end
173
+ def repeat mult
174
+ @words.repeat mult
175
+ end
123
176
 
124
- def numbers_spoken()
125
- end
177
+ def no_longer_than(max)
178
+ @words.no_longer_than max
179
+ end
126
180
 
127
- # def add_noise
128
- # Params.noise = true
129
- # end
181
+ def no_shorter_than(min)
182
+ @words.no_shorter_than min
183
+ end
130
184
 
131
- def reload
132
- load_words(Params.dictionary)
133
- end
185
+ def random_letters_numbers(options = {})
186
+ options.merge!(letters_numbers: true)
187
+ @words.random_letters_numbers options
188
+ end
134
189
 
135
- def load_common_words
136
- load_words
137
- end
190
+ def random_letters(options = {})
191
+ @words.random_letters(options)
192
+ end
138
193
 
139
- def load_most_common_words
140
- load_words MOST_COMMON_WORDS
141
- end
194
+ def random_numbers(options = {})
195
+ @words.random_numbers(options)
196
+ end
142
197
 
143
- def load_abbreviations
144
- load_words ABBREVIATIONS
145
- end
198
+ def alphabet(options = {reverse: nil})
199
+ @words.alphabet(options)
200
+ end
146
201
 
147
- def load_q_codes
148
- load_words Q_CODES
149
- end
202
+ def numbers(options = {reverse: nil})
203
+ @words.numbers(options)
204
+ end
150
205
 
151
- #todo refactor
206
+ def numbers_spoken()
207
+ #todo
208
+ end
152
209
 
153
- def alpha ; 'a'.upto('z').collect{|ch| ch} ; end
210
+ def cw_element_match arg
211
+ encs = CWG::CwEncoding.new
212
+ encs.match_elements arg
213
+ end
154
214
 
155
- def vowels ; ['a','e','i','o','u'] ; end
156
- def load_vowels ; @words.assign vowels ; end
157
- def load_alphabet ; @words.assign alpha ; end
158
- def load_consonants ; @words.assign alpha - vowels ; end
215
+ def letter_range args
216
+ @words.assign alpha
217
+ Cfg.config.params["including"] = args
218
+ @words.including
219
+ end
159
220
 
160
- def load_numbers
161
- @words.assign '1 2 3 4 5 6 7 8 9 0 '
162
- end
221
+ def load_alphabet(* args)
222
+ @words.assign alpha if args.empty?
223
+ return if args.empty?
224
+ if args[0].class == Range
225
+ @words.assign letter_range(args)
226
+ return
227
+ end
228
+ @words.assign cw_element_match(args)
229
+ end
163
230
 
164
- def load_words(filename = COMMON_WORDS)
165
- Params.dictionary = filename
166
- @words.load filename
167
- end
231
+ def load_text(filename)
232
+ Cfg.config.params["dictionary"] = filename
233
+ @words.load_text filename
234
+ end
168
235
 
169
- def set_tone_type(type)
170
- case type
171
- when :squarewave, :sawtooth, :sinewave
172
- Params.tone = type
236
+ def load_words(args)
237
+ @words.load args
238
+ end
239
+
240
+ def run_default
241
+ Cfg.config["run_default"] || :test_letters
242
+ end
243
+
244
+ def print_letters
245
+ Cfg.config.params["print_letters"] = true
246
+ end
247
+
248
+ def run
249
+ return if Cfg.config["no_run"]
250
+ self.send run_default
251
+ end
252
+
253
+ def no_run
254
+ Cfg.config.params["no_run"] = true
173
255
  end
174
- end
175
256
 
257
+ def noise
258
+ Cfg.config.params["noise"] = true
259
+ end
260
+
261
+ def no_noise
262
+ Cfg.config.params["noise"] = nil
263
+ end
264
+
265
+ def use_ebook2cw
266
+ Cfg.config.params["use_ebook2cw"] = true
267
+ end
268
+
269
+ def use_ruby_tone
270
+ Cfg.config.params["use_ebook2cw"] = nil
271
+ end
272
+
273
+ def words ; @words.all ; end
274
+ def load_common_words ; @words.load 1000 ; end
275
+ def load_most_common_words ; @words.load 500 ; end
276
+ def load_abbreviations ; load_text ABBREVIATIONS ; end
277
+ def reverse ; @words.reverse ; end
278
+ def double_words ; @words.double_words ; end
279
+ def letters_numbers ; @words.letters_numbers ; end
280
+ def load_codes ; load_text Q_CODES ; end
281
+ def alpha ; 'a'.upto('z').collect{|ch| ch} ; end
282
+ def vowels ; ['a','e','i','o','u'] ; end
283
+ def dot_letters ; ['e','i','s','h'] ; end
284
+ def dash_letters ; ['t','m','o'] ; end
285
+ def load_vowels ; @words.assign vowels ; end
286
+ def load_consonants ; @words.assign alpha - vowels ; end
287
+ def numbers ; '0'.upto('9').collect{|ch| ch} ; end
288
+ def load_numbers ; @words.assign numbers ; end
289
+ def load_dots ; load_letters(dot_letters) ; end
290
+ def load_dashes ; load_letters(dash_letters) ; end
291
+ def to_s ; @str.to_s ; end
292
+ def list ; Print.new.list self.to_s; puts ; end
293
+
294
+ alias_method :ewpm, :effective_wpm
295
+ alias_method :comment, :name
296
+ alias_method :word_length, :word_size
297
+ alias_method :load_letters, :load_alphabet
298
+ alias_method :word_shuffle, :shuffle
299
+ alias_method :repeat_letter, :repeat_word
300
+ alias_method :having_size_of, :word_size
301
+ alias_method :number_of_words, :word_count
302
+ alias_method :words_including, :including
303
+ alias_method :words_ending_with, :ending_with
304
+ alias_method :random_alphanumeric, :random_letters_numbers
305
+ alias_method :words_beginning_with, :beginning_with
306
+ alias_method :words_no_longer_than, :no_longer_than
307
+ alias_method :words_no_shorter_than, :no_shorter_than
308
+ end
176
309
  end