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,33 +1,25 @@
1
1
  # Module ToneHelpers provides helper methods for ToneGenerator
2
2
 
3
- module ToneHelpers
3
+ module CWG
4
4
 
5
- TWO_PI = 2 * Math::PI
6
- HERE = File.dirname(__FILE__) + '/../../'
7
- DOT_FILENAME = HERE + "audio/dot.wav"
8
- DASH_FILENAME = HERE + "audio/dash.wav"
9
- SPACE_FILENAME = HERE + "audio/space.wav"
10
- E_SPACE_FILENAME = HERE + "audio/e_space.wav"
5
+ module ToneHelpers
11
6
 
12
- # :reek: exclude from UtilityFunction
13
- def play_filename
14
- HERE + "audio/#{Params.audio_filename}"
15
- end
7
+ TWO_PI = 2 * Math::PI
16
8
 
17
- def convert_words wrds
18
- wrds.to_array.collect{ |wrd| wrd.delete("\n")}
19
- end
9
+ def convert_words wrds
10
+ wrds.to_array.collect{ |wrd| wrd.delete("\n")}
11
+ end
20
12
 
21
- def generate_space number_of_samples
22
- [].fill(0.0, 0, number_of_samples)
23
- end
13
+ def generate_space number_of_samples
14
+ [].fill(0.0, 0, number_of_samples)
15
+ end
24
16
 
25
- def space_sample? ele
26
- ele == :space || ele == :e_space
27
- end
17
+ def space_sample? ele
18
+ ele == :space || ele == :e_space
19
+ end
28
20
 
29
- def last_element? idx, chr
30
- idx == chr.size - 1
21
+ def last_element? idx, chr
22
+ idx == chr.size - 1
23
+ end
31
24
  end
32
-
33
25
  end
@@ -1,16 +1,20 @@
1
1
  # encoding: utf-8
2
2
 
3
- #class Speak speaks with a voice
3
+ module CWG
4
4
 
5
- class Voice
5
+ #class Speak speaks with a voice
6
6
 
7
- def initialize(options = {})
8
- @options = options
9
- end
7
+ class Voice
8
+
9
+ def initialize(options = {})
10
+ @options = options
11
+ end
12
+
13
+ def say words, rate = 300, voice = 'bruce'
14
+ system("say #{words} -ospoken.wave -r#{rate} -v#{voice}")
15
+ system("afplay spoken.wave")
16
+ end
10
17
 
11
- def say words, rate = 300, voice = 'bruce'
12
- system("say #{words} -ospoken.wave -r#{rate} -v#{voice}")
13
- system("afplay spoken.wave")
14
18
  end
15
19
 
16
20
  end
@@ -1,149 +1,179 @@
1
1
  # encoding: utf-8
2
2
 
3
- #class Words deals with words
3
+ module CWG
4
4
 
5
- class Words
5
+ #class Words deals with words
6
6
 
7
- include TextHelpers
7
+ class Words
8
8
 
9
- def load(filename)
10
- File.open(filename, 'r') do |file|
11
- @words = file.read.split
9
+ include TextHelpers
10
+
11
+ def load args
12
+ @words = CommonWords.new.read(args)
12
13
  end
13
- end
14
14
 
15
- def double_words
16
- temp = []
17
- @words.each do |wrd|
18
- 2.times { temp.push wrd }
15
+ def load_text(filename)
16
+ File.open(filename, 'r') do |file|
17
+ @words = file.read.split
18
+ end
19
19
  end
20
- @words = temp
21
- end
22
20
 
23
- def shuffle
24
- @words.shuffle!
25
- end
21
+ def double_words
22
+ @words.collect! {|wrd| [wrd] * 2}
23
+ @words.flatten!
24
+ end
26
25
 
27
- def to_array
28
- @words
29
- end
26
+ def repeat multiplier
27
+ @words *= multiplier + 1
28
+ end
30
29
 
31
- def all
32
- @words
33
- end
30
+ def shuffle
31
+ @words.shuffle!
32
+ end
34
33
 
35
- def to_s
36
- @words.join(' ')
37
- end
34
+ def to_array
35
+ @words
36
+ end
38
37
 
39
- def add words
40
- @words = words
41
- end
38
+ def all
39
+ @words
40
+ end
42
41
 
43
- def exist?
44
- @words
45
- end
42
+ def to_s
43
+ @words.join(' ')
44
+ end
46
45
 
47
- def word_size(size)
48
- @words = @words.select{|wrd| wrd.size == size}
49
- end
46
+ def add words
47
+ @words = words
48
+ end
50
49
 
51
- def beginning_with_letter(letr)
52
- @words.select{|wrd| wrd.start_with?(letr)}
53
- end
50
+ def exist?
51
+ @words
52
+ end
54
53
 
55
- def beginning_with(* letters)
56
- @words = letters.flatten.collect{|letr| beginning_with_letter(letr)}.flatten
57
- end
54
+ def word_size(size)
55
+ @words = @words.select{|wrd| wrd.size == size}
56
+ end
58
57
 
59
- def ending_with_letter(letr)
60
- @words.select{|wrd| wrd.end_with?(letr)}
61
- end
58
+ def beginning_with_letter(letr)
59
+ @words.select{|wrd| wrd.start_with?(letr)}
60
+ end
62
61
 
63
- def ending_with(* letters)
64
- @words = letters.flatten.collect{|letr| ending_with_letter(letr)}.flatten
65
- end
62
+ def ending_with_letter(letr)
63
+ @words.select{|wrd| wrd.end_with?(letr)}
64
+ end
66
65
 
67
- def count word_count
68
- @words = @words.take(word_count)
69
- end
66
+ def beginning_with
67
+ letter_filter(:beginning_with, Cfg.config["begin"])
68
+ end
70
69
 
71
- def including_letter(letr)
72
- @words.select{|wrd| wrd.include?(letr)}
73
- end
70
+ def ending_with
71
+ letter_filter(:ending_with, Cfg.config["end"])
72
+ end
74
73
 
75
- def containing(* letters)
76
- lets = letters.flatten.join('').split("")
77
- found, temp, @words = false, @words, []
78
- temp.each do |wrd|
79
- wrd_lets = wrd.split("")
80
- wrd_lets.each do |wrd_let|
81
- if lets.include?(wrd_let)
82
- found = true
74
+ def including
75
+ letter_filter(:including, Cfg.config["including"])
76
+ end
77
+
78
+ def letter_filter(option,letters)
79
+ method_name = option.to_s + "_letter"
80
+ @words = letters.flatten.collect do |letr|
81
+ if letr.class == Range
82
+ letr.collect do |let|
83
+ self.send(method_name, let)
84
+ end
83
85
  else
84
- found = false
85
- break
86
+ self.send(method_name, letr)
87
+ end
88
+ end.flatten
89
+ end
90
+
91
+ def count word_count
92
+ @words = @words.take(word_count)
93
+ end
94
+
95
+ def including_letter(letr)
96
+ @words.select{|wrd| wrd.include?(letr)}
97
+ end
98
+
99
+ def containing
100
+ letters = Cfg.config["containing"]
101
+ letters.flatten.collect do |letr|
102
+ if letr.class == Range
103
+ letters = letr.collect { |let| let }
86
104
  end
87
105
  end
88
- if found
89
- @words.push wrd
106
+
107
+ lets = letters.flatten.join('').split("")
108
+ found, temp, @words = false, @words, []
109
+ temp.each do |wrd|
110
+ wrd_lets = wrd.split("")
111
+ wrd_lets.each do |wrd_let|
112
+ if lets.include?(wrd_let)
113
+ found = true
114
+ else
115
+ found = false
116
+ break
117
+ end
118
+ end
119
+ if found
120
+ @words.push wrd
121
+ end
90
122
  end
123
+ @words
91
124
  end
92
- @words
93
- end
94
125
 
95
- def including(* letters)
96
- @words = letters.flatten.collect{|letr| including_letter(letr)}.flatten
97
- end
126
+ def no_longer_than(max)
127
+ @words = @words.select{|wrd| wrd.size <= max}
128
+ end
98
129
 
99
- def no_longer_than(max)
100
- @words = @words.select{|wrd| wrd.size <= max}
101
- end
130
+ def no_shorter_than(min)
131
+ @words = @words.select{|wrd| wrd.size >= min}
132
+ end
102
133
 
103
- def no_shorter_than(min)
104
- @words = @words.select{|wrd| wrd.size >= min}
105
- end
134
+ def assign(words)
135
+ words.kind_of?(String) ?
136
+ @words = words.split(/\s+/) :
137
+ @words = words
138
+ end
106
139
 
107
- def assign(words)
108
- words.kind_of?(String) ?
109
- @words = words.split(/\s+/) :
110
- @words = words
111
- end
140
+ def collect_words
141
+ @words.each{ |word| ' ' + word }.
142
+ collect{|wrd| wrd}.join(' ')
143
+ end
112
144
 
113
- def collect_words
114
- @words.each{ |word| ' ' + word }.
115
- collect{|wrd| wrd}.join(' ')
116
- end
145
+ def reverse
146
+ @words.reverse!
147
+ end
117
148
 
118
- def reverse
119
- @words.reverse!
120
- end
149
+ def letters_numbers
150
+ letter_group.push( * number_group)
151
+ end
121
152
 
122
- def letters_numbers
123
- letter_group.push( * number_group)
124
- end
153
+ def random_letters(options = {})
154
+ @words = Randomize.new(options, letter_group).generate
155
+ end
125
156
 
126
- def random_letters(options = {})
127
- @words = Randomize.new(options, letter_group).generate
128
- end
157
+ def random_numbers(options = {})
158
+ @words = Randomize.new(options, number_group).generate
159
+ end
129
160
 
130
- def random_numbers(options = {})
131
- @words = Randomize.new(options, number_group).generate
132
- end
161
+ def random_letters_numbers(options = {})
162
+ @words = Randomize.new(options, letters_numbers.shuffle).generate
163
+ end
133
164
 
134
- def random_letters_numbers(options = {})
135
- @words = Randomize.new(options, letters_numbers.shuffle).generate
136
- end
165
+ def alphabet(options = {reverse: nil})
166
+ @words = [Alphabet.new(options).generate]
167
+ end
137
168
 
138
- def alphabet(options = {reverse: nil})
139
- @words = [Alphabet.new(options).generate]
140
- end
169
+ def numbers(options = {reverse: nil})
170
+ @words = [Numbers.new(options).generate]
171
+ end
141
172
 
142
- def numbers(options = {reverse: nil})
143
- @words = [Numbers.new(options).generate]
144
- end
173
+ def numbers_spoken()
145
174
 
146
- def numbers_spoken()
175
+ end
147
176
 
148
177
  end
178
+
149
179
  end
@@ -0,0 +1,165 @@
1
+ require 'sequel'
2
+
3
+ DEBUG = false
4
+
5
+ class RunScriptTests
6
+ NO_RUN = DEBUG ? false : false
7
+ TEST_ON_MEMORY_DATABASE = DEBUG ? true : false
8
+ NO_RSS_FEED = DEBUG ? true : false
9
+ PATH_TO_CW_SCRIPTS = '/Users/martyn/jekyll/documentation-theme-jekyll/cw_scripts'
10
+ ROOT = '/Users/martyn/cw/cw_clone'
11
+ PROGRESS_FILE = "data/text/progress.txt"
12
+ DB = Sequel.sqlite(File.join(ROOT, 'test.sqlite')) unless(TEST_ON_MEMORY_DATABASE)
13
+ DB = Sequel.sqlite if(TEST_ON_MEMORY_DATABASE)
14
+
15
+ def initialize
16
+ create_tables if(TEST_ON_MEMORY_DATABASE)
17
+ populate_cw_scripts if(TEST_ON_MEMORY_DATABASE)
18
+ end
19
+
20
+ def create_tables
21
+ DB.create_table :scripts do
22
+ primary_key :id
23
+ String :script, :null => false
24
+ String :path, :null => false
25
+ end
26
+
27
+ DB.create_table :test_runs do
28
+ primary_key :id
29
+ DateTime :date_time
30
+ Fixnum :total_secs
31
+ String :result
32
+ end
33
+
34
+ DB.create_table :tests do
35
+ primary_key :id
36
+ Fixnum :test_run_id, :null => false
37
+ Fixnum :script_id, :null => false
38
+ Fixnum :wpm, :null => false
39
+ Fixnum :ewpm, :null => false
40
+ String :duration
41
+ end
42
+ end
43
+
44
+ def populate_cw_scripts
45
+ scripts = (Dir.glob(PATH_TO_CW_SCRIPTS + '/*.rb'))
46
+ scripts.each do |script|
47
+ script_name = File.basename(script,'.rb')
48
+ # populate the table
49
+ DB[:scripts].insert(:script => "#{script_name}",
50
+ :path => PATH_TO_CW_SCRIPTS)
51
+ end
52
+ DB[:scripts].insert(:script => "example",
53
+ :path => ROOT)
54
+ end
55
+
56
+ def reset_sentence_count
57
+ File.open(PROGRESS_FILE,'w') do |f|
58
+ f.puts "0"
59
+ end
60
+ end
61
+
62
+ def get_current_test_runs(test_run_id)
63
+ DB[:test_runs].where(:id => test_run_id)
64
+ end
65
+
66
+ def update_current_test_run(test_run_id,total_secs)
67
+ current = get_current_test_runs(test_run_id)
68
+ current.update(:total_secs=> total_secs, :result=>"pass")
69
+ end
70
+
71
+ def exec_command line
72
+ system(line)
73
+ end
74
+
75
+ def set_test_environment
76
+ "CW_ENV=test "
77
+ end
78
+
79
+ def run_test filename, environment = nil
80
+ environment == :test ?
81
+ env = set_test_environment : env = ''
82
+ exec_command(env + "bundle exec ruby #{filename}")
83
+ end
84
+
85
+ def execute_test_env filename
86
+ if NO_RUN
87
+ sleep 0.05
88
+ else
89
+ run_test(filename, :test)
90
+ end
91
+ end
92
+
93
+ class Timer
94
+ def initialize
95
+ @finish_time = @total_start_time = Time.now
96
+ end
97
+
98
+ def start
99
+ @start_time = Time.now
100
+ end
101
+ def finish
102
+ @finish_time = Time.now
103
+ end
104
+ def duration
105
+ @finish_time - @start_time
106
+ end
107
+ def total_secs
108
+ @finish_time - @total_start_time
109
+ end
110
+ def running_time
111
+ Time.now - @total_start_time
112
+ end
113
+ end
114
+
115
+ def test_runs_insert_dateTime
116
+ DB[:test_runs].insert(:date_time => Time.now,
117
+ :total_secs => 0,
118
+ :result => "fail")
119
+ end
120
+
121
+ def reset_sentence_count_maybe script_name
122
+ if script_name.include?("book_reading")
123
+ reset_sentence_count
124
+ end
125
+ end
126
+
127
+ def print_last_five_results
128
+ DB[:test_runs].each do |test_run|
129
+ if(test_run[:id] > (@test_run_id - 5))
130
+ puts test_run[:date_time].to_s + ': ' +
131
+ "Total secs: " + test_run[:total_secs].to_s
132
+ end
133
+ end
134
+ end
135
+
136
+ def run
137
+ timer = Timer.new
138
+ @test_run_id = test_runs_insert_dateTime
139
+ DB[:scripts].each do |test|
140
+ filename = File.join test[:path], test[:script] + '.rb'
141
+ puts "[#{timer.running_time.to_i}] Running: #{test[:script]}"
142
+ reset_sentence_count_maybe test[:script]
143
+ timer.start
144
+ # next unless test[:script].include? ("example")
145
+ execute_test_env filename
146
+ timer.finish
147
+ reset_sentence_count_maybe test[:script]
148
+ DB[:tests].insert(:script_id => test[:id],
149
+ :test_run_id => @test_run_id,
150
+ :wpm => 60,
151
+ :ewpm => 60,
152
+ :duration => "#{timer.duration}")
153
+ DB[:tests].all
154
+ end
155
+ update_current_test_run(@test_run_id,timer.total_secs)
156
+ run_test(PATH_TO_CW_SCRIPTS + '/rss_feed.rb') unless(NO_RSS_FEED)
157
+ print_last_five_results
158
+ end
159
+ end
160
+
161
+ test_scripts = RunScriptTests.new
162
+ 1.times do |loop_count|
163
+ test_scripts.run
164
+ break unless(DEBUG)
165
+ end