import * as kuromoji from "kuromoji";
import * as path from "path";
import { fileURLToPath } from "url";
const cache = new Map();
function getTokenizerPromise(dicPath) {
    return new Promise((resolve, reject) => {
        const cacheItem = cache.get(dicPath);
        if (cacheItem) {
            resolve(cacheItem);
        }
        kuromoji
            .builder({
            dicPath,
        })
            .build((err, tokenizer) => {
            if (err) {
                reject(err);
            }
            else {
                cache.set(dicPath, tokenizer);
                resolve(tokenizer);
            }
        });
    });
}
export class Analyzer {
    dicPath;
    constructor(dicPath) {
        this.dicPath = dicPath;
    }
    async tokenize(sentence) {
        return getTokenizerPromise(this.dicPath).then((tokenizer) => tokenizer
            .tokenize(sentence)
            .map((wo) => ({
            surface: wo.surface_form,
            pos: wo.pos,
        })));
    }
    static create(dicPath) {
        const dictDir = path.join(path.dirname(fileURLToPath(new URL(import.meta.url))), "../../dict");
        return new Analyzer(dicPath ?? dictDir);
    }
    static async analyze(sentence, dicPath) {
        return await Analyzer.create(dicPath).tokenize(sentence);
    }
}
export default Analyzer;
