@aituber-onair/voice
Version:
Voice synthesis library for AITuber OnAir
54 lines (53 loc) • 1.64 kB
JavaScript
import { emotions } from '../types/voice';
/**
* Regular expressions for emotion tag parsing
*/
export const EMOTION_TAG_REGEX = /\[([a-z]+)\]/i;
export const EMOTION_TAG_CLEANUP_REGEX = /\[[a-z]+\]\s*/gi;
/**
* Utility class for parsing and handling emotion tags in text
*/
export 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(EMOTION_TAG_REGEX);
if (match) {
const emotion = match[1].toLowerCase();
const cleanText = text.replace(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 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(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}`;
}
}