fix ttsify order
This commit is contained in:
parent
d4f8013d94
commit
083a95e241
5 changed files with 33 additions and 36 deletions
|
|
@ -5,13 +5,13 @@ import { Readable } from "stream";
|
||||||
import { createHash } from "node:crypto";
|
import { createHash } from "node:crypto";
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
import { existsSync } from "node:fs";
|
import { existsSync } from "node:fs";
|
||||||
|
import { saferKorean } from "../utils/saferKorean";
|
||||||
|
|
||||||
export abstract class TTSModelBase<RequestId> {
|
export abstract class TTSModelBase<RequestId> {
|
||||||
public ttsify(input: string): string {
|
public ttsify(input: string): string {
|
||||||
return input
|
return saferKorean(
|
||||||
.replace(/:[^:]+:/g, (text: string): string => (TTSModelBase.EMOJI_MAP[text] ?? "이모지"))
|
input.replace(/:[^:]+:/g, (text: string): string => (TTSModelBase.EMOJI_MAP[text] ?? "이모지"))
|
||||||
.replace(/[:*"<>|]/g, "")
|
);
|
||||||
.replace(/[\t\n]/g, " ")
|
|
||||||
}
|
}
|
||||||
public abstract createRequestId(text: string): RequestId
|
public abstract createRequestId(text: string): RequestId
|
||||||
public abstract getVoiceBuffer(id: RequestId): Promise<ArrayBuffer>
|
public abstract getVoiceBuffer(id: RequestId): Promise<ArrayBuffer>
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ import { createHmac } from "crypto";
|
||||||
import { join } from "path";
|
import { join } from "path";
|
||||||
import fetch from "../utils/fetch";
|
import fetch from "../utils/fetch";
|
||||||
import TTSModelBase from ".";
|
import TTSModelBase from ".";
|
||||||
import { saferKorean } from "../utils/saferKorean";
|
|
||||||
|
|
||||||
export class TTSPapagoModel extends TTSModelBase<TTSPapagoModel.RequestId> {
|
export class TTSPapagoModel extends TTSModelBase<TTSPapagoModel.RequestId> {
|
||||||
protected cachedVoice: Map<String, Promise<Buffer>>
|
protected cachedVoice: Map<String, Promise<Buffer>>
|
||||||
|
|
@ -11,9 +10,7 @@ export class TTSPapagoModel extends TTSModelBase<TTSPapagoModel.RequestId> {
|
||||||
this.cachedVoice = new Map();
|
this.cachedVoice = new Map();
|
||||||
}
|
}
|
||||||
ttsify(input: string): string {
|
ttsify(input: string): string {
|
||||||
return super.ttsify(saferKorean(
|
return super.ttsify(input)
|
||||||
input
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
public getVoicePath(id: TTSPapagoModel.RequestId): string {
|
public getVoicePath(id: TTSPapagoModel.RequestId): string {
|
||||||
const audioFileName = TTSModelBase.hashAudioFile(id.text, `.${id.speaker}.${id.speed.replace(/\-/g, "_")}`);
|
const audioFileName = TTSModelBase.hashAudioFile(id.text, `.${id.speaker}.${id.speed.replace(/\-/g, "_")}`);
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
import { join } from "path";
|
import { join } from "path";
|
||||||
import fetch from "../utils/fetch";
|
import fetch from "../utils/fetch";
|
||||||
import TTSModelBase from ".";
|
import TTSModelBase from ".";
|
||||||
import { saferKorean } from "../utils/saferKorean";
|
|
||||||
|
|
||||||
export class TTSSupertonicModel extends TTSModelBase<TTSSupertonicModel.RequestId> {
|
export class TTSSupertonicModel extends TTSModelBase<TTSSupertonicModel.RequestId> {
|
||||||
protected override cachedVoice: Map<String, Promise<Buffer>>
|
protected override cachedVoice: Map<String, Promise<Buffer>>
|
||||||
|
|
@ -10,9 +9,7 @@ export class TTSSupertonicModel extends TTSModelBase<TTSSupertonicModel.RequestI
|
||||||
this.cachedVoice = new Map();
|
this.cachedVoice = new Map();
|
||||||
}
|
}
|
||||||
override ttsify(input: string): string {
|
override ttsify(input: string): string {
|
||||||
return super.ttsify(saferKorean(
|
return super.ttsify(input);
|
||||||
input
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
private async getSupertonicResponse(voiceId: TTSSupertonicModel.RequestId) {
|
private async getSupertonicResponse(voiceId: TTSSupertonicModel.RequestId) {
|
||||||
const payload = {
|
const payload = {
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,7 @@ import { TYPECAST_TOKENS } from "../env";
|
||||||
import fetch from "../utils/fetch";
|
import fetch from "../utils/fetch";
|
||||||
import TTSModelBase from ".";
|
import TTSModelBase from ".";
|
||||||
import { readFileSync, writeFileSync } from "fs";
|
import { readFileSync, writeFileSync } from "fs";
|
||||||
import { cwd, env } from "process";
|
import { cwd } from "process";
|
||||||
import { saferKorean } from "../utils/saferKorean";
|
|
||||||
|
|
||||||
export class TTSTypecastModel extends TTSModelBase<TTSTypecastModel.RequestId> {
|
export class TTSTypecastModel extends TTSModelBase<TTSTypecastModel.RequestId> {
|
||||||
protected cachedVoice: Map<String, Promise<Buffer>>
|
protected cachedVoice: Map<String, Promise<Buffer>>
|
||||||
|
|
@ -15,11 +14,8 @@ export class TTSTypecastModel extends TTSModelBase<TTSTypecastModel.RequestId> {
|
||||||
this.lastUseApiKeyPath = join(cwd(), "cache", "typecast", "lastUseApiToken");
|
this.lastUseApiKeyPath = join(cwd(), "cache", "typecast", "lastUseApiToken");
|
||||||
}
|
}
|
||||||
ttsify(input: string): string {
|
ttsify(input: string): string {
|
||||||
|
|
||||||
return super.ttsify(
|
return super.ttsify(
|
||||||
saferKorean(input)
|
input
|
||||||
.replace(/ㄴㄴ/g, "노노")
|
|
||||||
.replace(/ㅇㅋ/g, "오키")
|
|
||||||
.replace(/ㅜㅜ/g, "눙물")
|
.replace(/ㅜㅜ/g, "눙물")
|
||||||
.replace(/빵/g, "빵 크크")
|
.replace(/빵/g, "빵 크크")
|
||||||
.replace(/[\?]+ *ㄴ/g, "물음표ㄴ")
|
.replace(/[\?]+ *ㄴ/g, "물음표ㄴ")
|
||||||
|
|
|
||||||
|
|
@ -146,7 +146,7 @@ export function processDots(input: string): string {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function saferKorean(input: string): string {
|
export function saferKorean(input: string): string {
|
||||||
return processDots(input)
|
return processDots(input + " ")
|
||||||
// Process isolated symbols
|
// Process isolated symbols
|
||||||
.replace(/^[\?\!\'\"]+ $/, (total)=>(
|
.replace(/^[\?\!\'\"]+ $/, (total)=>(
|
||||||
[...total].map(element => IsolatedSymbolMap[
|
[...total].map(element => IsolatedSymbolMap[
|
||||||
|
|
@ -169,11 +169,25 @@ export function saferKorean(input: string): string {
|
||||||
return lang + "코드블럭";
|
return lang + "코드블럭";
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Process link
|
||||||
|
.replace(/[hH][tT]{2}[pP][sS]?:\/\/(\S+)/g, (_, url: string) => {
|
||||||
|
if (url.startsWith("tenor.com/view")) {
|
||||||
|
return "움짤";
|
||||||
|
}
|
||||||
|
return "링크";
|
||||||
|
})
|
||||||
|
|
||||||
// Process koreans
|
// Process koreans
|
||||||
.replace(/ㅋ{2,}/g, (content) => "크".repeat(content.length))
|
.replace(/ㅋ{2,}/g, (content) => "크".repeat(content.length))
|
||||||
.replace(/[아ㅏ]{3,}/g, "아아아")
|
.replace(/[아ㅏ]{3,}/g, "아아아")
|
||||||
.replace(/ㅌ{2,}/g, "틔틔")
|
.replace(/ㅌ{2,}/g, "틔틔")
|
||||||
.replace(/ㄷ{2,}/g, "덜덜")
|
.replace(/ㄷ{2,}/g, "덜덜")
|
||||||
|
.replace(/ㄴ{2,}/g, "노노")
|
||||||
|
.replace(/(ㅇㅋ)+/g, (content: string) => {
|
||||||
|
return "오키".repeat(
|
||||||
|
Math.min(Math.floor(content.length / 2), 4)
|
||||||
|
);
|
||||||
|
})
|
||||||
.replace(/(ㅊㅋ)+/g, (content: string) => {
|
.replace(/(ㅊㅋ)+/g, (content: string) => {
|
||||||
return "추카".repeat(
|
return "추카".repeat(
|
||||||
Math.min(Math.floor(content.length / 2), 4)
|
Math.min(Math.floor(content.length / 2), 4)
|
||||||
|
|
@ -197,18 +211,18 @@ export function saferKorean(input: string): string {
|
||||||
mod = SIPrefix[mod.toLowerCase() as keyof typeof SIPrefix];
|
mod = SIPrefix[mod.toLowerCase() as keyof typeof SIPrefix];
|
||||||
return `${num} ${mod}바이트 `;
|
return `${num} ${mod}바이트 `;
|
||||||
})
|
})
|
||||||
.replace(/([\d,]+)([mck]?)m/g, (_, num: string, mod: string) => {
|
.replace(/([\d,]+)([m]?)l\s/g, (_, num: string, mod: string) => {
|
||||||
// 10m => 십미터
|
|
||||||
num = IntegerKorean.convertFromString(num);
|
|
||||||
mod = MeterPrefix[mod as keyof typeof MeterPrefix];
|
|
||||||
return `${num} ${mod}미터`;
|
|
||||||
})
|
|
||||||
.replace(/([\d,]+)([m]?)l/g, (_, num: string, mod: string) => {
|
|
||||||
// 10l => 십리터
|
// 10l => 십리터
|
||||||
num = IntegerKorean.convertFromString(num);
|
num = IntegerKorean.convertFromString(num);
|
||||||
mod = LiterPrefix[mod as keyof typeof LiterPrefix];
|
mod = LiterPrefix[mod as keyof typeof LiterPrefix];
|
||||||
return `${num} ${mod}리터 `;
|
return `${num} ${mod}리터 `;
|
||||||
})
|
})
|
||||||
|
.replace(/([\d,]+)([mck]?)m\s/g, (_, num: string, mod: string) => {
|
||||||
|
// 10m => 십미터
|
||||||
|
num = IntegerKorean.convertFromString(num);
|
||||||
|
mod = MeterPrefix[mod as keyof typeof MeterPrefix];
|
||||||
|
return `${num} ${mod}미터 `;
|
||||||
|
})
|
||||||
.replace(/([\d\.]+)\s*([개살시평명])/g, (_, num: string, postfix: string)=>{
|
.replace(/([\d\.]+)\s*([개살시평명])/g, (_, num: string, postfix: string)=>{
|
||||||
// 10명 => 열명
|
// 10명 => 열명
|
||||||
if (num.includes(".")) {
|
if (num.includes(".")) {
|
||||||
|
|
@ -260,18 +274,11 @@ export function saferKorean(input: string): string {
|
||||||
.replace(/㎡/g, "제곱미터")
|
.replace(/㎡/g, "제곱미터")
|
||||||
.replace(/㎢/g, "제곱킬로미터")
|
.replace(/㎢/g, "제곱킬로미터")
|
||||||
|
|
||||||
// Process link
|
|
||||||
.replace(/[hH][tT]{2}[pP][sS]?:\/\/(\S+)/g, (_, url: string) => {
|
|
||||||
if (url.startsWith("tenor.com/view")) {
|
|
||||||
return "움짤";
|
|
||||||
}
|
|
||||||
return "링크";
|
|
||||||
})
|
|
||||||
|
|
||||||
// Process symbol
|
// Process symbol
|
||||||
.replace(/[\%\^\&\*\#\@\.\-\+\_\=\/\\♡\$\|\:\;\>\<]/g, (t) => (
|
.replace(/[\%\^\&\*\#\@\.\-\+\_\=\/\\♡\$\|\:\;\>\<]/g, (t) => (
|
||||||
SymbolMap[t as keyof typeof SymbolMap]
|
SymbolMap[t as keyof typeof SymbolMap]
|
||||||
))
|
))
|
||||||
.replace(/([\?\!]+)/g, (_, content: string) => content[0])
|
.replace(/([\?\!]+)/g, (_, content: string) => content[0])
|
||||||
.replace(/[ \t\f\r]+/g, " ")
|
.replace(/[ \t\f\r]+/g, " ")
|
||||||
|
.trim()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue