Class: CW::Sentence

Inherits:
Object
  • Object
show all
Defined in:
lib/cw/sentence.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#indexObject

todo



6
7
8
# File 'lib/cw/sentence.rb', line 6

def index
  @index
end

Instance Method Details

#allObject



9
# File 'lib/cw/sentence.rb', line 9

def all               ; @sentences             ; end

#changeObject



21
22
23
24
# File 'lib/cw/sentence.rb', line 21

def change
  forward if next?
  rewind if previous?
end

#change?Boolean

Returns:

  • (Boolean)


16
# File 'lib/cw/sentence.rb', line 16

def change?           ; next? || previous?     ; end

#change_or_repeat?Boolean

Returns:

  • (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 check_sentence_navigation 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

#currentObject



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_allObject



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

#forwardObject

todo def forward ; @index += 1 ; end



13
# File 'lib/cw/sentence.rb', line 13

def forward           ; @index                 ; end

#nextObject



10
# File 'lib/cw/sentence.rb', line 10

def next              ; @next = true           ; end

#next?Boolean

Returns:

  • (Boolean)


11
# File 'lib/cw/sentence.rb', line 11

def next?             ; @next                  ; end

#next_sentenceObject



19
# File 'lib/cw/sentence.rb', line 19

def next_sentence     ; @sentences[@index + 1] ; end

#previous?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


15
# File 'lib/cw/sentence.rb', line 15

def repeat?           ; @repeat                ; end

#reset_flagsObject



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

#rewindObject



26
27
28
# File 'lib/cw/sentence.rb', line 26

def rewind
  @index = @index <= 1 ? 0 : @index - 1
end

#textObject



8
# File 'lib/cw/sentence.rb', line 8

def text              ; @text ||= String.new   ; end

#to_arrayObject



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