Class: CW::CommonWords

Inherits:
Object
  • Object
show all
Includes:
FileDetails
Defined in:
lib/cw/common_words.rb

Constant Summary

Constants included from FileDetails

FileDetails::ABBREVIATIONS, FileDetails::AUDIO_DIR, FileDetails::BOOKMARK_FILE, FileDetails::CALLS, FileDetails::CALLS_FILENAME, FileDetails::CODE, FileDetails::CODE_FILENAME, FileDetails::CONFIG_FILENAME, FileDetails::CONFIG_PATH, FileDetails::DASH_FILENAME, FileDetails::DATA, FileDetails::DEF_AUDIO_FILENAME, FileDetails::DICT_DIR, FileDetails::DICT_FILENAME, FileDetails::DOT_AUDIO_DIR, FileDetails::DOT_CW_DIR, FileDetails::DOT_FILENAME, FileDetails::E_SPACE_FILENAME, FileDetails::HERE, FileDetails::Q_CODES, FileDetails::ROOT, FileDetails::SPACE_FILENAME, FileDetails::TEXT, FileDetails::USER_CONFIG_PATH, FileDetails::WORK_DIR

Instance Method Summary collapse

Methods included from FileDetails

#audio_dir, #audio_filename, #dash_path, #default_audio_dir, #dot_audio_dir, #dot_cw_dir, #dot_path, #e_space_path, #init_filenames, #process_audio_dir, #process_dot_audio, #process_dot_cw, #progress_file, #space_path, #user_audio_dir

Constructor Details

#initializeCommonWords

Returns a new instance of CommonWords.



7
8
9
# File 'lib/cw/common_words.rb', line 7

def initialize
  @words = []
end

Instance Method Details

#allObject



34
35
36
37
38
# File 'lib/cw/common_words.rb', line 34

def all
  File.foreach(dictionary).collect do |line|
  line.chomp
  end
end

#custom_dict_dirObject



11
12
13
# File 'lib/cw/common_words.rb', line 11

def custom_dict_dir
  File.join(WORK_DIR, Cfg.config["dictionary_dir"])
end

#dict_filenameObject



22
23
24
25
26
27
# File 'lib/cw/common_words.rb', line 22

def dict_filename
  @dict_filename ||=
    Cfg.config["dictionary_name"] ?
      Cfg.config["dictionary_name"] :
      DICT_FILENAME
end

#dictionaryObject



29
30
31
32
# File 'lib/cw/common_words.rb', line 29

def dictionary
  @dictionary ||=
    File.join(dictionary_dir, dict_filename)
end

#dictionary_dirObject



15
16
17
18
19
20
# File 'lib/cw/common_words.rb', line 15

def dictionary_dir
  @dictionary_dir ||=
    Cfg.config["dictionary_dir"] ?
      custom_dict_dir :
      DICT_DIR
end

#low(last) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cw/common_words.rb', line 40

def low last
  results = []
  count = 0
  File.foreach(dictionary).collect do |line|
    if count <= last
      results << line.chomp
    end
    count += 1
    break if count > last
  end
  results
end

#mid(first, last) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cw/common_words.rb', line 53

def mid first, last
  results = []
  count = 0
  File.foreach(dictionary).collect do |line|
    if (count >= first) && (count <= last)
      results << line.chomp
    end
    count += 1
    break if count > last
  end
  results
end

#parse_quantity(quantity = :default) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cw/common_words.rb', line 66

def parse_quantity(quantity = :default)
  if quantity == :default
    return [0, 999]
  elsif quantity.class == 1.class
    [0, quantity - 1]
    (0...quantity).collect {|q| q}
  elsif quantity.class == Range
    ary = quantity.to_a
    return ary[0] - 1, ary[-1] -1
#        ary.pop
#        ary.unshift ary[0] - 1
#        ary
  end
end

#read(quantity = :default) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cw/common_words.rb', line 81

def read(quantity = :default)
  if quantity == :all
    @words = all
  else
    qty = parse_quantity(quantity)
    if qty[0] == 0
      @words = low qty[-1]
    else
      @words = mid qty[0], qty[1]
    end
  end
  @words
end

#to_aObject



95
96
97
# File 'lib/cw/common_words.rb', line 95

def to_a
  @words
end