yaejunyang/packages/utils/nyaize.ts
2026-02-09 18:31:56 +00:00

137 lines
No EOL
4 KiB
TypeScript

export const nyaWords = {
'나': "냐",
'낙': "냑",
'낚': "냒",
'낛': "냓",
'난': "냔",
'낝': "냕",
'낞': "냖",
'낟': "냗",
'날': "냘",
'낡': "냙",
'낢': "냚",
'낣': "냛",
'낤': "냜",
'낥': "냝",
'낦': "냞",
'낧': "냟",
'남': "냠",
'납': "냡",
'낪': "냢",
'낫': "냣",
'났': "냤",
'낭': "냥",
'낮': "냦",
'낯': "냧",
'낰': "냨",
'낱': "냩",
'낲': "냪",
'낳': "냫",
};
export const nyaWords2 = {
'내': "냥",
'넹': "냥",
'넴': "냥",
'넵': "냥",
'냐': "냥",
'님': "냥",
'니': "냥",
'다': "다냥",
'까': "까냥",
'네': "네냥",
'야': "야냥",
'꺼': "꺼냥",
'래': "래냥",
'해': "해냥",
'지': "지냥",
'라': "라냥",
'요': "요냥",
'가': "가냥",
'데': "데냥",
'돼': "돼냥",
'줘': "줘냥",
'마': "마냥",
'와': "와냥",
'어': "어냥",
'자': "자냥",
'죠': "죠냥",
'서': "서냥",
'게': "게냥",
};
function replacePunctuation(input: string): string {
return input.replace(/(^|\s)([?!,.;~^@()]+)/g, (match, p1, p2) => {
const firstChar = p2[0];
let transformed;
if (firstChar === '?') transformed = '냥?';
else if (firstChar === '!') transformed = '냥!';
else if (firstChar === ',') transformed = '냥,';
else if (firstChar === '.') transformed = '냥.';
else if (firstChar === ';') transformed = '냥;';
else if (firstChar === '~') transformed = '냥~';
else if (firstChar === '^') transformed = '냥^';
else if (firstChar === '@') transformed = '냥@';
else if (firstChar === '(') transformed = '냥(';
else if (firstChar === ')') transformed = '냥)';
else transformed = p2;
return p1 + transformed + p2.slice(1);
});
}
function addNyangAtMWord(sentence: string): string {
return sentence.split(' ').map((word) => {
const match = word.match(/^([가-힣]+)([?!,.;~^@()]*)$/);
if (!match) return word;
const baseWord = match[1];
const punctuation = match[2];
const lastChar = baseWord[baseWord.length - 1];
const charCode = lastChar.charCodeAt(0);
if (charCode >= 0xAC00 && charCode <= 0xD7A3) {
const baseCode = charCode - 0xAC00;
const jongseong = baseCode % 28;
if (jongseong === 16) {
return baseWord + "냥" + punctuation;
}
}
return word;
}).join(' ');
}
export function nyaize(text: string): string {
for (let key in nyaWords2) {
text = text.replaceAll(key + ".", nyaWords2[key as keyof typeof nyaWords2] + "."); // yeah I gotta optimize these
text = text.replaceAll(key + ",", nyaWords2[key as keyof typeof nyaWords2] + ",");
text = text.replaceAll(key + "?", nyaWords2[key as keyof typeof nyaWords2] + "?");
text = text.replaceAll(key + "!", nyaWords2[key as keyof typeof nyaWords2] + "!");
text = text.replaceAll(key + ";", nyaWords2[key as keyof typeof nyaWords2] + ";");
text = text.replaceAll(key + "~", nyaWords2[key as keyof typeof nyaWords2] + "~");
text = text.replaceAll(key + "^", nyaWords2[key as keyof typeof nyaWords2] + "^");
text = text.replaceAll(key + "@", nyaWords2[key as keyof typeof nyaWords2] + "@");
text = text.replaceAll(key + "(", nyaWords2[key as keyof typeof nyaWords2] + "(");
text = text.replaceAll(key + ")", nyaWords2[key as keyof typeof nyaWords2] + ")");
text = text.replaceAll(key + " ", nyaWords2[key as keyof typeof nyaWords2] + " ");
if (text.endsWith(key)) {
text = text.slice(0, -1) + nyaWords2[key as keyof typeof nyaWords2];
}
}
for (let key in nyaWords) {
text = text.replaceAll(key, nyaWords[key as keyof typeof nyaWords]);
}
text = replacePunctuation(text);
text = addNyangAtMWord(text);
return text;
}