Skip to main content

Augment Beancount importers with machine learning functionality.

Project description

https://github.com/beancount/smart_importer

https://github.com/beancount/smart_importer/actions/workflows/ci.yml/badge.svg?branch=main

Augments Beancount importers with machine learning functionality.

Status

Working protoype, development status: beta

Installation

The smart_importer can be installed from PyPI:

pip install smart_importer

Quick Start

This package provides import hooks that can modify the imported entries. When running the importer, the existing entries will be used as training data for a machine learning model, which will then predict entry attributes.

The following example shows how to apply the PredictPostings hook to an existing CSV importer:

from beancount.ingest.importers import csv
from beancount.ingest.importers.csv import Col

from smart_importer import apply_hooks, PredictPostings


class MyBankImporter(csv.Importer):
    '''Conventional importer for MyBank'''

    def __init__(self, *, account):
        super().__init__(
            {Col.DATE: 'Date',
             Col.PAYEE: 'Transaction Details',
             Col.AMOUNT_DEBIT: 'Funds Out',
             Col.AMOUNT_CREDIT: 'Funds In'},
            account,
            'EUR',
            (
                'Date, Transaction Details, Funds Out, Funds In'
            )
        )


CONFIG = [
    apply_hooks(MyBankImporter(account='Assets:MyBank:MyAccount'), [PredictPostings()])
]

Documentation

This section explains in detail the relevant concepts and artifacts needed for enhancing Beancount importers with machine learning.

Beancount Importers

Let’s assume you have created an importer for “MyBank” called MyBankImporter:

class MyBankImporter(importer.ImporterProtocol):
    """My existing importer"""
    # the actual importer logic would be here...

Note: This documentation assumes you already know how to create Beancount importers. Relevant documentation can be found in the beancount import documentation. With the functionality of beancount.ingest, users can write their own importers and use them to convert downloaded bank statements into lists of Beancount entries. An example is provided as part of beancount v2’s source code under examples/ingest/office.

smart_importer only works by appending onto incomplete single-legged postings (i.e. It will not work by modifying postings with accounts like “Expenses:TODO”). The extract method in the importer should follow the latest interface and include an existing_entries argument.

Applying smart_importer hooks

Any Beancount importer can be converted into a smart importer by applying one of the following hooks:

  • PredictPostings - predict the list of postings.

  • PredictPayees- predict the payee of the transaction.

  • DuplicateDetector - detect duplicates

For example, to convert an existing MyBankImporter into a smart importer:

from your_custom_importer import MyBankImporter
from smart_importer import apply_hooks, PredictPayees, PredictPostings

my_bank_importer =  MyBankImporter('whatever', 'config', 'is', 'needed')
apply_hooks(my_bank_importer, [PredictPostings(), PredictPayees()])

CONFIG = [
    my_bank_importer,
]

Note that the importer hooks need to be applied to an importer instance, as shown above.

Specifying Training Data

The smart_importer hooks need training data, i.e. an existing list of transactions in order to be effective. Training data can be specified by calling bean-extract with an argument that references existing Beancount transactions, e.g., bean-extract -f existing_transactions.beancount. When using the importer in Fava, the existing entries are used as training data automatically.

Usage with Fava

Smart importers play nice with Fava. This means you can use smart importers together with Fava in the exact same way as you would do with a conventional importer. See Fava’s help on importers for more information.

Development

Pull requests welcome!

Executing the Unit Tests

Simply run (requires tox):

make test

Configuring Logging

Python’s logging module is used by the smart_importer module. The according log level can be changed as follows:

import logging
logging.getLogger('smart_importer').setLevel(logging.DEBUG)

Using Tokenizer

Custom tokenizers can let smart_importer support more languages, eg. Chinese.

If you looking for Chinese tokenizer, you can follow this example:

First make sure that jieba is installed in your python environment:

pip install jieba

In your importer code, you can then pass jieba to be used as tokenizer:

from smart_importer import PredictPostings
import jieba

jieba.initialize()
tokenizer = lambda s: list(jieba.cut(s))

predictor = PredictPostings(string_tokenizer=tokenizer)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

smart_importer-0.5.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

smart_importer-0.5-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file smart_importer-0.5.tar.gz.

File metadata

  • Download URL: smart_importer-0.5.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for smart_importer-0.5.tar.gz
Algorithm Hash digest
SHA256 9f49816b2837372ff9787072a270e7aa90de12bbf7b43869e7bedc0a833a9752
MD5 8f9fbb9090765de180ef5661772d29c5
BLAKE2b-256 b9bec7096f5a569e10456338a1db9ef126da8e06aa1c79a9ae61319fb202b210

See more details on using hashes here.

File details

Details for the file smart_importer-0.5-py3-none-any.whl.

File metadata

  • Download URL: smart_importer-0.5-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for smart_importer-0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 8f99a4f444a485477aec6a3b70ed566c6de415ea1cb1552ac4d5524023c2883b
MD5 66f1e69f91340774f30003eb5c9195cc
BLAKE2b-256 e604aafd1307007c2133c859f18699c28be2a68c067d77879b1e5451673d4765

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page