import * as tf from '@tensorflow/tfjs';
import axios from "axios";


export default class BottomKeySmashClassifier {

    constructor() {
        self._maxLen = 96;
    }

    async init() {
        self._model = await tf.loadLayersModel('/tfjs_model/model.json');
        const resp = await axios.get("/tokens.json");
        self._tokens = resp.data;
    }

    async predict(message) {
        message = message.toLowerCase();
        const inputIds = Array.from(message).map(c => self._tokens[c]);

        // padding
        while (inputIds.length !== self._maxLen) {
            inputIds.push(0);
        }

        const tensor = tf.tensor(inputIds, [1, 96], "int32");

        let prediction = self._model.predict(tensor)

        return (await prediction.array())[0][1]
    }
}
