class CW::Sentence

Attributes

index[RW]

Public Instance Methods

all() click to toggle source
# File lib/cw/sentence.rb, line 9
def all               ; @sentences             ; end
change() click to toggle source
# File lib/cw/sentence.rb, line 21
def change
  forward if next?
  rewind if previous?
end
change?() click to toggle source
# File lib/cw/sentence.rb, line 16
def change?           ; next? || previous?     ; end
change_or_repeat?() click to toggle source
# File lib/cw/sentence.rb, line 17
def change_or_repeat? ; change? || repeat?     ; end
check_end_of_book(progress_file) click to toggle source
# 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) click to toggle source
# File lib/cw/sentence.rb, line 87
def check_sentence_navigation chr
  @next     = true if(chr == ']')
  @previous = true if(chr == '[')
  @repeat   = true if(chr == '-')
end
create_progress_maybe(progress_file) click to toggle source
# 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() click to toggle source
# File lib/cw/sentence.rb, line 18
def current           ; @sentences[@index]     ; end
cw_chars(chr) click to toggle source
# File lib/cw/sentence.rb, line 64
def cw_chars chr
  chr.tr('^a-z0-9\,\=\!\/\?\.', '')
end
exclude_non_cw_chars(word) click to toggle source
# File lib/cw/sentence.rb, line 68
def exclude_non_cw_chars word
  cw_chars(word)
end
find_all() click to toggle source
# 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() click to toggle source

todo def forward ; @index += 1 ; end

# File lib/cw/sentence.rb, line 13
def forward           ; @index                 ; end
next() click to toggle source
# File lib/cw/sentence.rb, line 10
def next              ; @next = true           ; end
next?() click to toggle source
# File lib/cw/sentence.rb, line 11
def next?             ; @next                  ; end
next_sentence() click to toggle source
# File lib/cw/sentence.rb, line 19
def next_sentence     ; @sentences[@index + 1] ; end
previous?() click to toggle source
# File lib/cw/sentence.rb, line 14
def previous?         ; @previous              ; end
read_book(book) click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# File lib/cw/sentence.rb, line 15
def repeat?           ; @repeat                ; end
reset_flags() click to toggle source
# File lib/cw/sentence.rb, line 93
def reset_flags
  @next = @previous = @repeat = nil
end
reset_progress(progress_file) click to toggle source
# File lib/cw/sentence.rb, line 36
def reset_progress progress_file
  @index = 0
  write_progress progress_file
end
rewind() click to toggle source
# File lib/cw/sentence.rb, line 26
def rewind
  @index = @index <= 1 ? 0 : @index - 1
end
text() click to toggle source
# File lib/cw/sentence.rb, line 8
def text              ; @text ||= String.new   ; end
to_array() click to toggle source
# File lib/cw/sentence.rb, line 97
def to_array
  array = @sentences[@index].split(' ')
  array.collect {|x| x + ' '}
end
write_progress(progress_file) click to toggle source
# File lib/cw/sentence.rb, line 56
def write_progress progress_file
  File.open(progress_file, 'w') {|f| f.puts @index.to_s}
end