Class: Reckon::App
- Inherits:
-
Object
- Object
- Reckon::App
- Defined in:
- lib/reckon/app.rb
Constant Summary collapse
- VERSION =
"Reckon 0.3.10"
Instance Attribute Summary collapse
-
#accounts ⇒ Object
Returns the value of attribute accounts.
-
#csv_parser ⇒ Object
Returns the value of attribute csv_parser.
-
#options ⇒ Object
Returns the value of attribute options.
-
#seen ⇒ Object
Returns the value of attribute seen.
-
#tokens ⇒ Object
Returns the value of attribute tokens.
Class Method Summary collapse
Instance Method Summary collapse
- #already_seen?(row) ⇒ Boolean
- #each_row_backwards ⇒ Object
- #extract_account_tokens(subtree, account = nil) ⇒ Object
- #finish ⇒ Object
-
#initialize(options = {}) ⇒ App
constructor
A new instance of App.
- #interactive_output(str) ⇒ Object
- #learn! ⇒ Object
- #learn_about_account(account, data) ⇒ Object
- #learn_from(ledger) ⇒ Object
- #ledger_format(row, line1, line2) ⇒ Object
- #output(ledger_line) ⇒ Object
- #output_table ⇒ Object
- #tokenize(str) ⇒ Object
- #walk_backwards ⇒ Object
-
#weighted_account_match(row) ⇒ Object
Weigh accounts by how well they match the row.
Constructor Details
#initialize(options = {}) ⇒ App
Returns a new instance of App.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/reckon/app.rb', line 10 def initialize( = {}) self. = self.tokens = {} self.accounts = {} self.seen = {} self.[:currency] ||= '$' [:string] = File.read([:file]) unless [:string] @csv_parser = CSVParser.new( ) learn! end |
Instance Attribute Details
#accounts ⇒ Object
Returns the value of attribute accounts.
8 9 10 |
# File 'lib/reckon/app.rb', line 8 def accounts @accounts end |
#csv_parser ⇒ Object
Returns the value of attribute csv_parser.
8 9 10 |
# File 'lib/reckon/app.rb', line 8 def csv_parser @csv_parser end |
#options ⇒ Object
Returns the value of attribute options.
8 9 10 |
# File 'lib/reckon/app.rb', line 8 def end |
#seen ⇒ Object
Returns the value of attribute seen.
8 9 10 |
# File 'lib/reckon/app.rb', line 8 def seen @seen end |
#tokens ⇒ Object
Returns the value of attribute tokens.
8 9 10 |
# File 'lib/reckon/app.rb', line 8 def tokens @tokens end |
Class Method Details
.parse_opts(args = ARGV) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/reckon/app.rb', line 220 def self.parse_opts(args = ARGV) = { :output_file => STDOUT } parser = OptionParser.new do |opts| opts. = "Usage: Reckon.rb [options]" opts.separator "" opts.on("-f", "--file FILE", "The CSV file to parse") do |file| [:file] = file end opts.on("-a", "--account name", "The Ledger Account this file is for") do |a| [:bank_account] = a end opts.on("-v", "--[no-]verbose", "Run verbosely") do |v| [:verbose] = v end opts.on("-i", "--inverse", "Use the negative of each amount") do |v| [:inverse] = v end opts.on("-p", "--print-table", "Print out the parsed CSV in table form") do |p| [:print_table] = p end opts.on("-o", "--output-file FILE", "The ledger file to append to") do |o| [:output_file] = File.open(o, 'a') end opts.on("-l", "--learn-from FILE", "An existing ledger file to learn accounts from") do |l| [:existing_ledger_file] = l end opts.on("", "--ignore-columns 1,2,5", "Columns to ignore in the CSV file - the first column is column 1") do |ignore| [:ignore_columns] = ignore.split(",").map { |i| i.to_i } end opts.on("", "--contains-header [N]", "The first row of the CSV is a header and should be skipped. Optionally add the number of rows to skip.") do |contains_header| [:contains_header] = 1 [:contains_header] = contains_header.to_i if contains_header end opts.on("", "--csv-separator ','", "Separator for parsing the CSV - default is comma.") do |csv_separator| [:csv_separator] = csv_separator end opts.on("", "--comma-separates-cents", "Use comma instead of period to deliminate dollars from cents when parsing ($100,50 instead of $100.50)") do |c| [:comma_separates_cents] = c end opts.on("", "--encoding 'UTF-8'", "Specify an encoding for the CSV file; not usually needed") do |e| [:encoding] = e end opts.on("-c", "--currency '$'", "Currency symbol to use, defaults to $ (£, EUR)") do |e| [:currency] = e end opts.on("", "--date-format '%d/%m/%Y'", "Force the date format (see Ruby DateTime strftime)") do |d| [:date_format] = d end opts.on("-u", "--unattended", "Don't ask questions and guess all the accounts automatically. Used with --learn-from or --account-tokens options.") do |n| [:unattended] = n end opts.on("-t", "--account-tokens FILE", "YAML file with manually-assigned tokens for each account (see README)") do |a| [:account_tokens_file] = a end opts.on("", "--default-into-account name", "Default into account") do |a| [:default_into_account] = a end opts.on("", "--default-outof-account name", "Default 'out of' account") do |a| [:default_outof_account] = a end opts.on("", "--suffixed", "If --currency should be used as a suffix. Defaults to false.") do |e| [:suffixed] = e end opts.on_tail("-h", "--help", "Show this message") do puts opts exit end opts.on_tail("--version", "Show version") do puts VERSION exit end opts.parse!(args) end unless [:file] [:file] = ask("What CSV file should I parse? ") unless [:file].length > 0 puts "\nYou must provide a CSV file to parse.\n" puts parser exit end end unless [:bank_account] fail "Please specify --account for the unattended mode" if [:unattended] [:bank_account] = ask("What is the account name of this bank account in Ledger? ") do |q| q.readline = true q.validate = /^.{2,}$/ q.default = "Assets:Bank:Checking" end end end |
Instance Method Details
#already_seen?(row) ⇒ Boolean
37 38 39 |
# File 'lib/reckon/app.rb', line 37 def already_seen?(row) seen[row[:pretty_date]] && seen[row[:pretty_date]][row[:pretty_money]] end |
#each_row_backwards ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/reckon/app.rb', line 205 def each_row_backwards rows = [] (0...@csv_parser.columns.first.length).to_a.each do |index| rows << { :date => @csv_parser.date_for(index), :pretty_date => @csv_parser.pretty_date_for(index), :pretty_money => @csv_parser.pretty_money_for(index), :pretty_money_negated => @csv_parser.pretty_money_for(index, :negate), :money => @csv_parser.money_for(index), :description => @csv_parser.description_for(index) } end rows.sort { |a, b| a[:date] <=> b[:date] }.each do |row| yield row end end |
#extract_account_tokens(subtree, account = nil) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/reckon/app.rb', line 41 def extract_account_tokens(subtree, account = nil) if subtree.is_a?(Array) { account => subtree } else at = subtree.map { |k, v| extract_account_tokens(v, [account, k].compact.join(':')) } at.inject({}) { |k, v| k = k.merge(v)} end end |
#finish ⇒ Object
140 141 142 143 144 |
# File 'lib/reckon/app.rb', line 140 def finish [:output_file].close unless [:output_file] == STDOUT interactive_output "Exiting." exit end |
#interactive_output(str) ⇒ Object
21 22 23 24 |
# File 'lib/reckon/app.rb', line 21 def interactive_output(str) return if [:unattended] puts str end |
#learn! ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/reckon/app.rb', line 50 def learn! if [:account_tokens_file] fail "#{options[:account_tokens_file]} doesn't exist!" unless File.exists?([:account_tokens_file]) extract_account_tokens(YAML.load_file([:account_tokens_file])).each do |account, tokens| tokens.each { |t| learn_about_account(account, t) } end end return unless [:existing_ledger_file] fail "#{options[:existing_ledger_file]} doesn't exist!" unless File.exists?([:existing_ledger_file]) ledger_data = File.read([:existing_ledger_file]) learn_from(ledger_data) end |
#learn_about_account(account, data) ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/reckon/app.rb', line 63 def learn_about_account(account, data) accounts[account] ||= 0 tokenize(data).each do |token| tokens[token] ||= {} tokens[token][account] ||= 0 tokens[token][account] += 1 accounts[account] += 1 end end |
#learn_from(ledger) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/reckon/app.rb', line 26 def learn_from(ledger) LedgerParser.new(ledger).entries.each do |entry| entry[:accounts].each do |account| learn_about_account( account[:name], [entry[:desc], account[:amount]].join(" ") ) unless account[:name] == [:bank_account] seen[entry[:date]] ||= {} seen[entry[:date]][@csv_parser.pretty_money(account[:amount])] = true end end end |
#ledger_format(row, line1, line2) ⇒ Object
188 189 190 191 192 193 |
# File 'lib/reckon/app.rb', line 188 def ledger_format(row, line1, line2) out = "#{row[:pretty_date]}\t#{row[:description]}\n" out += "\t#{line1.first}\t\t\t\t\t#{line1.last}\n" out += "\t#{line2.first}\t\t\t\t\t#{line2.last}\n\n" out end |
#output(ledger_line) ⇒ Object
146 147 148 149 |
# File 'lib/reckon/app.rb', line 146 def output(ledger_line) [:output_file].puts ledger_line [:output_file].flush end |
#output_table ⇒ Object
195 196 197 198 199 200 201 202 203 |
# File 'lib/reckon/app.rb', line 195 def output_table output = Terminal::Table.new do |t| t.headings = 'Date', 'Amount', 'Description' each_row_backwards do |row| t << [ row[:pretty_date], row[:pretty_money], row[:description] ] end end interactive_output output end |
#tokenize(str) ⇒ Object
73 74 75 |
# File 'lib/reckon/app.rb', line 73 def tokenize(str) str.downcase.split(/[\s\-]/) end |
#walk_backwards ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/reckon/app.rb', line 77 def walk_backwards seen_anything_new = false each_row_backwards do |row| interactive_output Terminal::Table.new(:rows => [ [ row[:pretty_date], row[:pretty_money], row[:description] ] ]) if already_seen?(row) interactive_output "NOTE: This row is very similar to a previous one!" if !seen_anything_new interactive_output "Skipping..." next end else seen_anything_new = true end possible_answers = weighted_account_match( row ).map! { |a| a[:account] } ledger = if row[:money] > 0 if [:unattended] out_of_account = possible_answers.first || [:default_outof_account] || 'Income:Unknown' else out_of_account = ask("Which account provided this income? ([account]/[q]uit/[s]kip) ") { |q| q.completion = possible_answers q.readline = true q.default = possible_answers.first } end finish if out_of_account == "quit" || out_of_account == "q" if out_of_account == "skip" || out_of_account == "s" interactive_output "Skipping" next end ledger_format( row, [[:bank_account], row[:pretty_money]], [out_of_account, row[:pretty_money_negated]] ) else if [:unattended] into_account = possible_answers.first || [:default_into_account] || 'Expenses:Unknown' else into_account = ask("To which account did this money go? ([account]/[q]uit/[s]kip) ") { |q| q.completion = possible_answers q.readline = true q.default = possible_answers.first } end finish if into_account == "quit" || into_account == 'q' if into_account == "skip" || into_account == 's' interactive_output "Skipping" next end ledger_format( row, [into_account, row[:pretty_money_negated]], [[:bank_account], row[:pretty_money]] ) end learn_from(ledger) unless [:account_tokens_file] output(ledger) end end |
#weighted_account_match(row) ⇒ Object
Weigh accounts by how well they match the row
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/reckon/app.rb', line 152 def weighted_account_match( row ) query_tokens = tokenize(row[:description]) search_vector = [] account_vectors = {} query_tokens.each do |token| idf = Math.log((accounts.keys.length + 1) / ((tokens[token] || {}).keys.length.to_f + 1)) tf = 1.0 / query_tokens.length.to_f search_vector << tf*idf accounts.each do |account, total_terms| tf = (tokens[token] && tokens[token][account]) ? tokens[token][account] / total_terms.to_f : 0 account_vectors[account] ||= [] account_vectors[account] << tf*idf end end # Should I normalize the vectors? Probably unnecessary due to tf-idf and short documents. account_vectors = account_vectors.to_a.map do |account, account_vector| { :cosine => (0...account_vector.length).to_a.inject(0) { |m, i| m + search_vector[i] * account_vector[i] }, :account => account } end account_vectors.sort! {|a, b| b[:cosine] <=> a[:cosine] } # Return empty set if no accounts matched so that we can fallback to the defaults in the unattended mode if [:unattended] if account_vectors.first && account_vectors.first[:account] account_vectors = [] if account_vectors.first[:cosine] == 0 end end return account_vectors end |