Line data Source code
1 : // Copyright 2025 The terCAD team. All rights reserved.
2 : // Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file.
3 :
4 : import 'package:app_finance/_classes/structure/bill_app_data.dart';
5 : import 'package:flutter_currency_picker/flutter_currency_picker.dart';
6 :
7 : typedef _Sample = ({String category, Set<String> words, String? account, String? range});
8 :
9 : class _Feature {
10 : final _counts = <String, Map<String, int>>{};
11 :
12 4 : void add(String? key, String category, int delta) {
13 : if (key == null) return;
14 16 : final scope = _counts.putIfAbsent(key, () => {});
15 8 : final value = (scope[category] ?? 0) + delta;
16 4 : if (value > 0) {
17 4 : scope[category] = value;
18 : } else {
19 1 : scope.remove(category);
20 3 : if (scope.isEmpty) _counts.remove(key);
21 : }
22 : }
23 :
24 : // P(category | key)
25 4 : Map<String, double> probability(String? key) {
26 8 : final scope = key != null ? _counts[key] : null;
27 : if (scope == null) return const {};
28 16 : final total = scope.values.fold(0, (a, b) => a + b);
29 16 : return scope.map((category, count) => MapEntry(category, count / total));
30 : }
31 : }
32 :
33 : class BudgetPrediction {
34 : static const weightDescription = 0.5;
35 : static const weightAccount = 0.3;
36 : static const weightAmount = 0.2;
37 : static const threshold = 0.25;
38 : static const limit = 3;
39 :
40 12 : static final _separator = RegExp(r'[^\p{L}\p{N}]+', unicode: true);
41 :
42 : final bool Function(String category)? isValid;
43 :
44 : final _words = _Feature();
45 : final _accounts = _Feature();
46 : final _ranges = _Feature();
47 : final _samples = <String, _Sample>{};
48 :
49 4 : BudgetPrediction({this.isValid});
50 :
51 4 : static Set<String> _tokenize(String? value) =>
52 32 : (value ?? '').toLowerCase().split(_separator).where((e) => e.length > 2).toSet();
53 :
54 4 : static String? _range(double? amount, Currency? currency) {
55 4 : if (amount == null || amount == 0) return null;
56 20 : return '${amount.abs().round().toString().length}${currency?.code ?? ''}';
57 : }
58 :
59 4 : void _apply(_Sample sample, int delta) {
60 8 : for (final word in sample.words) {
61 8 : _words.add(word, sample.category, delta);
62 : }
63 8 : _accounts.add(sample.account, sample.category, delta);
64 8 : _ranges.add(sample.range, sample.category, delta);
65 : }
66 :
67 4 : void remove(BillAppData item) {
68 7 : final previous = item.uuid != null ? _samples.remove(item.uuid) : null;
69 : if (previous != null) {
70 2 : _apply(previous, -1);
71 : }
72 : }
73 :
74 4 : void add(BillAppData item) {
75 4 : remove(item);
76 12 : if (item.hidden || item.category.isEmpty) return;
77 : final sample = (
78 4 : category: item.category,
79 8 : words: _tokenize(item.title),
80 12 : account: item.account.isNotEmpty ? item.account : null,
81 12 : range: _range(item.details, item.currency),
82 : );
83 4 : _apply(sample, 1);
84 4 : if (item.uuid != null) {
85 3 : _samples[item.uuid!] = sample;
86 : }
87 : }
88 :
89 4 : List<String> predict(BillAppData item) {
90 4 : final scores = <String, double>{};
91 4 : void score(Map<String, double> probability, double weight) =>
92 24 : probability.forEach((category, value) => scores[category] = (scores[category] ?? 0) + value * weight);
93 :
94 8 : final words = _tokenize(item.title);
95 4 : if (words.isNotEmpty) {
96 4 : final description = <String, double>{};
97 8 : for (final word in words) {
98 4 : _words
99 4 : .probability(word)
100 20 : .forEach((category, value) => description[category] = (description[category] ?? 0) + value);
101 : }
102 24 : score(description.map((category, value) => MapEntry(category, value / words.length)), weightDescription);
103 : }
104 24 : score(_accounts.probability(item.account.isNotEmpty ? item.account : null), weightAccount);
105 24 : score(_ranges.probability(_range(item.details, item.currency)), weightAmount);
106 :
107 30 : final result = scores.entries.where((e) => e.value >= threshold && (isValid?.call(e.key) ?? true)).toList()
108 4 : ..sort((a, b) => b.value.compareTo(a.value));
109 20 : return result.take(limit).map((e) => e.key).toList();
110 : }
111 : }
|