packages/cozy-konnector-libs/src/libs/categorization/index.js
async function createCategorizer() {
  const classifierOptions = { tokenizer }

  // We can't initialize the model in parallel using `Promise.all` because with
  // it is not possible to manage errors separately
  let globalModel, localModel

  try {
    globalModel = await createGlobalModel(classifierOptions)
  } catch (e) {
    log('info', 'Failed to create global model:')
    log('info', e.message)
  }

  try {
    localModel = await createLocalModel(classifierOptions)
  } catch (e) {
    log('info', 'Failed to create local model:')
    log('info', e.message)
  }

  const modelsToApply = [globalModel, localModel].filter(Boolean)

  const categorize = transactions => {
    modelsToApply.forEach(model => model.categorize(transactions))

    return transactions
  }

  return { categorize }
}
async function createCategorizer() {
  const classifierOptions = { tokenizer }

  // We can't initialize the model in parallel using `Promise.all` because with
  // it is not possible to manage errors separately
  let globalModel, localModel

  try {
    globalModel = await createGlobalModel(classifierOptions)
  } catch (e) {
    log('info', 'Failed to create global model:')
    log('info', e.message)
  }

  try {
    localModel = await createLocalModel(classifierOptions)
  } catch (e) {
    log('info', 'Failed to create local model:')
    log('info', e.message)
  }

  const modelsToApply = [globalModel, localModel].filter(Boolean)

  const categorize = transactions => {
    modelsToApply.forEach(model => model.categorize(transactions))

    return transactions
  }

  return { categorize }
}
async function createCategorizer ( ) { const classifierOptions = { tokenizer } // We can't initialize the model in parallel using `Promise.all` because with // it is not possible to manage errors separately let globalModel , localModel try { globalModel = await createGlobalModel ( classifierOptions ) } catch ( e ) { log ( 'info' , 'Failed to create global model:' ) log ( 'info' , e . message ) } try { localModel = await createLocalModel ( classifierOptions ) } catch ( e ) { log ( 'info' , 'Failed to create local model:' ) log ( 'info' , e . message ) } const modelsToApply = [ globalModel , localModel ] . filter ( Boolean ) const categorize = transactions => { modelsToApply . forEach ( model => model . categorize ( transactions ) ) return transactions } return { categorize } }
Initialize global and local models and return an object exposing a
`categorize` function that applies both models on an array of transactions

The global model is a model specific to hosted Cozy instances. It is not available for self-hosted instances. It will just do nothing in that case.

The local model is based on the user manual categorizations.

Each model adds two properties to the transactions:
* The global model adds `cozyCategoryId` and `cozyCategoryProba`
* The local model adds `localCategoryId` and `localCategoryProba`

In the end, each transaction can have up to four different categories. An application can use these categories to show the most significant for the user. See https://github.com/cozy/cozy-doctypes/blob/master/docs/io.cozy.bank.md#categories for more informations.

@return {Object} an object with a `categorize` method

@example
const { BaseKonnector, createCategorizer } = require('cozy-konnector-libs')

class BankingKonnector extends BaseKonnector {
async saveTransactions() {
const transactions = await this.fetchTransactions()
const categorizer = await createCategorizer
const categorizedTransactions = await categorizer.categorize(transactions)

// Save categorizedTransactions
}
}
Initialize global and local models and return an object exposing a categorize function that applies both models on an array of transactions