UNPKG

@aituber-onair/voice

Version:

Voice synthesis library for AITuber OnAir

58 lines (57 loc) 1.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EmotionParser = exports.EMOTION_TAG_CLEANUP_REGEX = exports.EMOTION_TAG_REGEX = void 0; const voice_1 = require("../types/voice"); /** * Regular expressions for emotion tag parsing */ exports.EMOTION_TAG_REGEX = /\[([a-z]+)\]/i; exports.EMOTION_TAG_CLEANUP_REGEX = /\[[a-z]+\]\s*/gi; /** * Utility class for parsing and handling emotion tags in text */ class EmotionParser { /** * Extract emotion from text and return clean text * @param text Text that may contain emotion tags like [happy] * @returns Object containing extracted emotion and clean text */ static extractEmotion(text) { const match = text.match(exports.EMOTION_TAG_REGEX); if (match) { const emotion = match[1].toLowerCase(); const cleanText = text.replace(exports.EMOTION_TAG_CLEANUP_REGEX, '').trim(); return { emotion, cleanText, }; } return { cleanText: text }; } /** * Check if an emotion is valid * @param emotion Emotion string to validate * @returns True if the emotion is valid */ static isValidEmotion(emotion) { return voice_1.emotions.includes(emotion); } /** * Remove all emotion tags from text * @param text Text containing emotion tags * @returns Clean text without emotion tags */ static cleanEmotionTags(text) { return text.replace(exports.EMOTION_TAG_CLEANUP_REGEX, '').trim(); } /** * Add emotion tag to text * @param emotion Emotion to add * @param text Text content * @returns Text with emotion tag prepended */ static addEmotionTag(emotion, text) { return `[${emotion}] ${text}`; } } exports.EmotionParser = EmotionParser;