Class: CW::Sentence
- Inherits:
-
Object
- Object
- CW::Sentence
- Defined in:
- lib/cw/sentence.rb
Instance Attribute Summary collapse
-
#index ⇒ Object
todo.
Instance Method Summary collapse
- #all ⇒ Object
- #change ⇒ Object
- #change? ⇒ Boolean
- #change_or_repeat? ⇒ Boolean
- #check_end_of_book(progress_file) ⇒ Object
- #check_sentence_navigation(chr) ⇒ Object
- #create_progress_maybe(progress_file) ⇒ Object
- #current ⇒ Object
- #cw_chars(chr) ⇒ Object
- #exclude_non_cw_chars(word) ⇒ Object
- #find_all ⇒ Object
-
#forward ⇒ Object
todo def forward ; @index += 1 ; end.
- #next ⇒ Object
- #next? ⇒ Boolean
- #next_sentence ⇒ Object
- #previous? ⇒ Boolean
- #read_book(book) ⇒ Object
- #read_progress(progress_file) ⇒ Object
- #repeat? ⇒ Boolean
- #reset_flags ⇒ Object
- #reset_progress(progress_file) ⇒ Object
- #rewind ⇒ Object
- #text ⇒ Object
- #to_array ⇒ Object
- #write_progress(progress_file) ⇒ Object
Instance Attribute Details
#index ⇒ Object
todo
6 7 8 |
# File 'lib/cw/sentence.rb', line 6 def index @index end |
Instance Method Details
#all ⇒ Object
9 |
# File 'lib/cw/sentence.rb', line 9 def all ; @sentences ; end |
#change ⇒ Object
21 22 23 24 |
# File 'lib/cw/sentence.rb', line 21 def change forward if next? rewind if previous? end |
#change? ⇒ Boolean
16 |
# File 'lib/cw/sentence.rb', line 16 def change? ; next? || previous? ; end |
#change_or_repeat? ⇒ Boolean
17 |
# File 'lib/cw/sentence.rb', line 17 def change_or_repeat? ; change? || repeat? ; end |
#check_end_of_book(progress_file) ⇒ Object
41 42 43 44 45 |
# File 'lib/cw/sentence.rb', line 41 def check_end_of_book progress_file if @index >= @sentences.size reset_progress progress_file end end |
#check_sentence_navigation(chr) ⇒ Object
87 88 89 90 91 |
# File 'lib/cw/sentence.rb', line 87 def chr @next = true if(chr == ']') @previous = true if(chr == '[') @repeat = true if(chr == '-') end |
#create_progress_maybe(progress_file) ⇒ Object
30 31 32 33 34 |
# File 'lib/cw/sentence.rb', line 30 def create_progress_maybe progress_file unless File.exists? progress_file reset_progress progress_file end end |
#current ⇒ Object
18 |
# File 'lib/cw/sentence.rb', line 18 def current ; @sentences[@index] ; end |
#cw_chars(chr) ⇒ Object
64 65 66 |
# File 'lib/cw/sentence.rb', line 64 def cw_chars chr chr.tr('^a-z0-9\,\=\!\/\?\.', '') end |
#exclude_non_cw_chars(word) ⇒ Object
68 69 70 |
# File 'lib/cw/sentence.rb', line 68 def exclude_non_cw_chars word cw_chars(word) end |
#find_all ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/cw/sentence.rb', line 72 def find_all @sentences = [] @text.gsub!(/\s+/, ' ').downcase! loop do sentence_end = @text.index('. ') unless sentence_end break end line = @text[0..sentence_end] line = line.split.collect{|word| exclude_non_cw_chars word}.join(' ') @sentences << line @text.replace @text[sentence_end + 2..-1] end end |
#forward ⇒ Object
todo def forward ; @index += 1 ; end
13 |
# File 'lib/cw/sentence.rb', line 13 def forward ; @index ; end |
#next ⇒ Object
10 |
# File 'lib/cw/sentence.rb', line 10 def next ; @next = true ; end |
#next? ⇒ Boolean
11 |
# File 'lib/cw/sentence.rb', line 11 def next? ; @next ; end |
#next_sentence ⇒ Object
19 |
# File 'lib/cw/sentence.rb', line 19 def next_sentence ; @sentences[@index + 1] ; end |
#previous? ⇒ Boolean
14 |
# File 'lib/cw/sentence.rb', line 14 def previous? ; @previous ; end |
#read_book(book) ⇒ Object
60 61 62 |
# File 'lib/cw/sentence.rb', line 60 def read_book book File.open(book, 'r') { |f| text.replace f.readlines(' ').join} end |
#read_progress(progress_file) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/cw/sentence.rb', line 47 def read_progress progress_file create_progress_maybe progress_file File.open(progress_file, 'r') {|f| @index = f.readline.to_i} unless(@index && @index.class == 1.class) reset_progress progress_file end check_end_of_book progress_file end |
#repeat? ⇒ Boolean
15 |
# File 'lib/cw/sentence.rb', line 15 def repeat? ; @repeat ; end |
#reset_flags ⇒ Object
93 94 95 |
# File 'lib/cw/sentence.rb', line 93 def reset_flags @next = @previous = @repeat = nil end |
#reset_progress(progress_file) ⇒ Object
36 37 38 39 |
# File 'lib/cw/sentence.rb', line 36 def reset_progress progress_file @index = 0 write_progress progress_file end |
#rewind ⇒ Object
26 27 28 |
# File 'lib/cw/sentence.rb', line 26 def rewind @index = @index <= 1 ? 0 : @index - 1 end |
#text ⇒ Object
8 |
# File 'lib/cw/sentence.rb', line 8 def text ; @text ||= String.new ; end |
#to_array ⇒ Object
97 98 99 100 |
# File 'lib/cw/sentence.rb', line 97 def to_array array = @sentences[@index].split(' ') array.collect {|x| x + ' '} end |
#write_progress(progress_file) ⇒ Object
56 57 58 |
# File 'lib/cw/sentence.rb', line 56 def write_progress progress_file File.open(progress_file, 'w') {|f| f.puts @index.to_s} end |