-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBankLog.m
215 lines (191 loc) · 6.11 KB
/
BankLog.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
139
140
141
142
143
144
145
146
147
148
149
150
151
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//
// BankLog.m
// FileExample
//
// Created by Jesus Renero Quintero on 24/12/10.
// Copyright 2013 Jesus Renero Quintero. All rights reserved.
//
#import "BankLog.h"
#import "Entry.h"
#import "Levenshtein.h"
#import "Prefs.h"
#import "Match.h"
@implementation BankLog
@synthesize logArray;
- (id)init {
self = [super init];
if (self != nil) {
logArray = [[NSMutableArray alloc]init];
}
return self;
}
/*
Esta funcion recorre toda la lista de entries del banklog y
busca con qué categorias hace matching cada una.
in prefs: las categorias en las que estoy clasificando las entradas del banco
junto con los tags asociados a cada una.
in conflictsSet: los conflictos que he ido aprendiendo y que me enseñan a
resolver un empate entre categorias para una entrada.
*/
- (void)matchEntries:(Prefs *)prefs :(NSMutableSet *)conflictsSet
{
Entry *entry;
int numEntries = [logArray count];
NSMutableSet *matchesSet;
LogIt(@"Matching entries with categories...");
// Recorro el numero de entradas, que ya tengo en memoria.
for(int i=0; i<numEntries; i++)
{
Match *winner;
entry = [logArray objectAtIndex:i];
// Busco a que categorias puede pertenecer esta entrada bancaria.
matchesSet = [prefs matchTag:[entry concepto]];
if (matchesSet.count == 0) {
LogIt (@"-- MISMATCH -- Entry doesn't match any category!!");
LogIt (@" '%@'", entry.concepto);
winner = nil;
}
else
{
LogIt (@"ENTRY: '%@'.", [entry concepto]);
// Recorro todas las categorias con las que ha habido "match"
int j=0;
for (Match *match in matchesSet) {
// Pinto cada categoria del Set.
LogIt (@" > MATCH (%d/%d): '%@'", j+1, matchesSet.count, match.categoryMatched);
// Pinto las etiquetas que han producido el match.
for (int k=0;k<[match.tagsMatched count];k++) {
LogIt (@" > %d - %@",k+1, [match.tagsMatched objectAtIndex:k]);
}
j++;
}
// Si hay mas de una categoria tenemos un conflicto.
if ( matchesSet.count > 1 ) {
winner = [Match solveConflict:matchesSet :conflictsSet];
}
// Si solo hay una, sacamos el elemento del set y ese es el winner.
else if ( matchesSet.count == 1 ) {
NSEnumerator *e = [matchesSet objectEnumerator];
winner = [e nextObject];
}
}
entry.matchingCategory = winner;
[logArray replaceObjectAtIndex:i withObject:entry];
}
LogIt(@"DONE!");
}
- (void)printFullEntries
{
Entry *entry;
for (int index=0; index<[logArray count]; index++)
{
entry = [logArray objectAtIndex:index];
NSLog(@" fechaOperacion: %@ ", entry.fechaOperacion);
NSLog(@" fechaValor: %@ ", entry.fechaValor);
NSLog(@" concepto: %@ ", entry.concepto);
NSLog(@" importe: %@ ", entry.importe);
NSLog(@" saldo: %@ ", entry.saldo);
LogIt(@" categoria: %@ ", entry.matchingCategory);
}
}
- (BOOL)addEntry:(Entry *)newEntry
{
NSString *localized = [NSString stringWithString:[newEntry concepto]];
NSString *delocalized = [NSString stringWithUTF8String:
[localized cStringUsingEncoding:[NSString defaultCStringEncoding]]];
newEntry.concepto = [NSString stringWithString:delocalized];
// Relleno el Match para que no se quede vacio y asi no estropee la ejecución de la
// TableView.
newEntry.matchingCategory.categoryMatched = @"";
[newEntry.matchingCategory.tagsMatched addObject:(NSString *)@""];
newEntry.matchingCategory.votes = 0;
[logArray addObject:newEntry];
return YES;
}
- (void) printEntries
{
Entry *e;
int numUnclassfied=0;
for (e in logArray) {
if (e.matchingCategory != nil)
LogIt(@"%@ (%.0f.€) .... %@", [e.matchingCategory categoryMatched], [e.importe floatValue], e.concepto);
else {
numUnclassfied++;
LogIt(@"NULL (%.0f.€) .... %@", [e.importe floatValue], e.concepto);
}
}
LogIt(@"\n%d Entries.\n%d Unclassified.\n%.02f %% Success rate.\n",
[logArray count], numUnclassfied,
(((float)[logArray count]-(float)numUnclassfied)/(float)[logArray count])*100.0 );
}
- (void) printEntriesSummaries
{
Entry *e;
NSCountedSet *summary = [[NSCountedSet alloc] init];
for (e in logArray) {
if (e.matchingCategory != nil) {
[summary addObject:[e.matchingCategory categoryMatched]];
}
}
for (id s in summary) {
// "n" es el numero de veces que esta presente esa categoria en todo el LOG.
int n = [summary countForObject:s];
LogIt(@"%@ - %.0f%% (%d)", s,
((float)n/(float)[logArray count])*100.0, n );
}
}
- (void) printMoneyForAllCategories
{
Entry *e;
NSCountedSet *summary = [[NSCountedSet alloc] init];
float total=0.0;
// Saco todas las categorias que me han salido en el log.
for (e in logArray) {
if (e.matchingCategory != nil) {
[summary addObject:[e.matchingCategory categoryMatched]];
}
total += [e.importe floatValue];
}
// Para cada categoria, sumo la cantidad de pasta que sale.
for (NSString *category in summary) {
float money = 0.0;
// Si esta entrada pertenece a esta categoria, hago cuentas...
for (e in logArray) {
NSComparisonResult res = [category caseInsensitiveCompare:[e.matchingCategory categoryMatched]];
if (res == NSOrderedSame) {
money += [e.importe floatValue];
}
}
LogIt(@"%@: %.0f.€", category, money);
}
}
- (void) printMoneyPerCategory
{
Entry *e;
NSCountedSet *summary = [[NSCountedSet alloc] init];
NSArray *categories;
// Saco todas las categorias que me han salido en el log.
for (e in logArray) {
if (e.matchingCategory != nil) {
[summary addObject:[e.matchingCategory categoryMatched]];
}
}
categories = [summary allObjects];
for (int i=0; i<categories.count; i++) {
printf ( "%d) %s\n", i, [[categories objectAtIndex:i] cStringUsingEncoding:NSUTF8StringEncoding] );
}
int d;
printf("\nWhat category? ");
scanf ("%d", &d);
NSString *category = [categories objectAtIndex:d];
float money=0.0;
for (e in logArray) {
NSComparisonResult res = [category caseInsensitiveCompare:[e.matchingCategory categoryMatched]];
if (res == NSOrderedSame) {
LogIt(@"%@ (%.0f.€) .... %@", [e.matchingCategory categoryMatched], [e.importe floatValue], e.concepto);
money += [e.importe floatValue];
}
}
LogIt(@"\nTOTAL %@: %.0f.€", category, money);
}
@end