<commit_before>// Requires:
const adjetiveisor = require('adjetiveisor')
const logger = require('loggeraas-node-wrapper')
const h = require('../helpers')

require('dotenv').config()

// Import adjectives:
const translators = require('../translators.json')

// Logger config:
const loggerConfig = {
  host: process.env.LOGGER_HOST,
  path: process.env.LOGGER_PATH,
  port: process.env.LOGGER_PORT,
  hash: process.env.LOGGER_HASH,
  enabled: (process.env.LOGGER_ENABLED === 'true'), // this is required because dotenv doesn't accept boolean params
  verbose: (process.env.LOGGER_VERBOSE === 'true')
}

const l = logger(loggerConfig)
const genericTranslator = adjetiveisor()

const translator = 'puto'

const checkTranslator = (req, res) => {
  if (typeof translators[req.params.adjective] === 'undefined') {
    const responseText = `Translation not configured: ${req.params.adjective}`
    console.warn(responseText)
    const fail = h.apiFail(400, responseText)
    res.status(400).send(fail)
    return false
  }
  return true
}

exports.usage = (req, res) => {
  if (!checkTranslator(req, res)) return false

  const apiDefinition = {
    api: {
      name: req.params.adjective,
      base_url: `${req.protocol}://${req.hostname}${req.path}`,
      endpoints: [
        {
          method: 'GET',
          path: req.path,
          params: []
        },
        {
          method: 'POST',
          path: req.path,
          params: [
            { name: 'text', type: 'string', required: true },
            { name: 'verbose', type: 'boolean', required: false }
          ]
        }
      ]
    }
  }
  const data = apiDefinition
  const result = h.apiResponse(data)
  res.status(200).send(result)
}

exports.translate = (req, res) => {
  if (!checkTranslator(req, res)) return false

  if (typeof req.body.text !== 'string') {
    const fail = h.apiFail(400, 'Bad Request: you need to provide text param')
    res.status(400).send(fail)
    return
  }

  // Config the translator for that adjective:
  genericTranslator.config(translators[req.params.adjective])

  try {
    const text = req.body.text
    const verbose = req.body.verbose || false
    if (verbose === true) {
      genericTranslator.verbose(true)
    } else {
      genericTranslator.verbose(false)
    }
    const textTranslated = genericTranslator.translate(text)
    const data = {
      text,
      translation: textTranslated,
      translator
    }
    const result = h.apiResponse(data)
    res.status(200).send(result)
    l.log({ input: data.text, output: data.translation, translator: data.translator }) // only logs succesful requests
  } catch (e) {
    const fail = h.apiFail()
    res.status(500).send(fail)
    console.log(e)
  }
}
<commit_msg>dynamic translator value for generic controller<commit_after>// Requires:
const adjetiveisor = require('adjetiveisor')
const logger = require('loggeraas-node-wrapper')
const h = require('../helpers')

require('dotenv').config()

// Import adjectives:
const translators = require('../translators.json')

// Logger config:
const loggerConfig = {
  host: process.env.LOGGER_HOST,
  path: process.env.LOGGER_PATH,
  port: process.env.LOGGER_PORT,
  hash: process.env.LOGGER_HASH,
  enabled: (process.env.LOGGER_ENABLED === 'true'), // this is required because dotenv doesn't accept boolean params
  verbose: (process.env.LOGGER_VERBOSE === 'true')
}

const l = logger(loggerConfig)
const genericTranslator = adjetiveisor()

const checkTranslator = (req, res) => {
  if (typeof translators[req.params.adjective] === 'undefined') {
    const responseText = `Translation not configured: ${req.params.adjective}`
    console.warn(responseText)
    const fail = h.apiFail(400, responseText)
    res.status(400).send(fail)
    return false
  }
  return true
}

exports.usage = (req, res) => {
  if (!checkTranslator(req, res)) return false

  const apiDefinition = {
    api: {
      name: req.params.adjective,
      base_url: `${req.protocol}://${req.hostname}${req.path}`,
      endpoints: [
        {
          method: 'GET',
          path: req.path,
          params: []
        },
        {
          method: 'POST',
          path: req.path,
          params: [
            { name: 'text', type: 'string', required: true },
            { name: 'verbose', type: 'boolean', required: false }
          ]
        }
      ]
    }
  }
  const data = apiDefinition
  const result = h.apiResponse(data)
  res.status(200).send(result)
}

exports.translate = (req, res) => {
  if (!checkTranslator(req, res)) return false

  if (typeof req.body.text !== 'string') {
    const fail = h.apiFail(400, 'Bad Request: you need to provide text param')
    res.status(400).send(fail)
    return
  }

  // Config the translator for that adjective:
  genericTranslator.config(translators[req.params.adjective])

  try {
    const text = req.body.text
    const verbose = req.body.verbose || false
    if (verbose === true) {
      genericTranslator.verbose(true)
    } else {
      genericTranslator.verbose(false)
    }
    const textTranslated = genericTranslator.translate(text)
    const data = {
      text,
      translation: textTranslated,
      translator: req.params.adjective
    }
    const result = h.apiResponse(data)
    res.status(200).send(result)
    l.log({ input: data.text, output: data.translation, translator: data.translator }) // only logs succesful requests
  } catch (e) {
    const fail = h.apiFail()
    res.status(500).send(fail)
    console.log(e)
  }
}

{'fraction_non_alphanumeric': 0.13711583924349882, 'fraction_numerical': 0.007637752318603383, 'mean_word_length': 2.962536023054755, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4205949', 'n_tokens_mistral': 1791, 'n_tokens_neox': 1745, 'n_words': 401}