Skip to content

Commit

Permalink
fig bugs in csv parsing and don't throw an exception on blank token file
Browse files Browse the repository at this point in the history
  • Loading branch information
benprew committed Jun 4, 2024
1 parent 974b3eb commit 784294d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
11 changes: 7 additions & 4 deletions lib/reckon/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def learn_from_ledger(ledger)

# Add tokens from account_tokens_file to accounts
def extract_account_tokens(subtree, account = nil)
if subtree.nil?
if subtree.nil? || !subtree
puts "Warning: empty #{account} tree"
{}
elsif subtree.is_a?(Array)
Expand Down Expand Up @@ -168,15 +168,18 @@ def each_row_backwards
:money => @csv_parser.money_for(index),
:description => @csv_parser.description_for(index) }
end
rows.sort_by { |n| [n[:date], -n[:money], n[:description]] }.each { |row| yield row }
rows.sort_by { |n|
[n[:date], -n[:money], n[:description]]
}.each { |row| yield row }
end

def print_transaction(rows, fh = $stdout)
str = "\n"
header = %w[Date Amount Description Note]
maxes = header.map(&:length)

rows = rows.map { |r| [r[:pretty_date], r[:pretty_money], r[:description], r[:note]] }
rows = rows.map { |r|
[r[:pretty_date], r[:pretty_money], r[:description], r[:note]]
}

rows.each do |r|
r.length.times { |i| l = r[i] ? r[i].length : 0; maxes[i] = l if maxes[i] < l }
Expand Down
1 change: 1 addition & 0 deletions lib/reckon/cosine_similarity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# These weights and measures are used to suggest which account a transaction should be
# assigned to.
module Reckon
# Calculates cosine similarity for tf/idf
class CosineSimilarity
DocumentInfo = Struct.new(:tokens, :accounts)

Expand Down
17 changes: 9 additions & 8 deletions lib/reckon/csv_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ def row(index)
private

def filter_csv
if options[:ignore_columns]
new_columns = []
columns.each_with_index do |column, index|
new_columns << column unless options[:ignore_columns].include?(index + 1)
end
@columns = new_columns
return unless options[:ignore_columns]

new_columns = []
columns.each_with_index do |column, index|
new_columns << (options[:ignore_columns].include?(index + 1) ? [''] * column.length : column)
end
@columns = new_columns
end

def evaluate_columns(cols)
Expand Down Expand Up @@ -222,7 +222,8 @@ def parse(data, filename = nil)
# convert to a stringio object to handle multi-line fields
parser_opts = {
col_sep: separator,
skip_blanks: true
skip_blanks: true,
row_sep: :auto
}
begin
rows = CSV.parse(StringIO.new(data), **parser_opts)
Expand All @@ -235,7 +236,7 @@ def parse(data, filename = nil)
index = data.index("\n", index) + 1 # skip over newline character
count += 1
end
rows = CSV.parse(StringIO.new(data[index..-1]), **parser_opts)
rows = CSV.parse(StringIO.new(data[index..]), **parser_opts)
rows[0..-footer_lines_to_skip]
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/reckon/ledger_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def to_csv(ledger)
end

def format_row(row, line1, line2)
note = row[:note] ? "\t; row[:note]" : ""
note = row[:note] ? "\t; #{row[:note]}" : ""
out = "#{row[:pretty_date]}\t#{row[:description]}#{note}\n"
out += "\t#{line1.first}\t\t\t#{line1.last}\n"
out += "\t#{line2.first}\t\t\t#{line2.last}\n\n"
Expand Down

0 comments on commit 784294d

Please sign in to comment.