yaejunyang/packages/utils/nyaize.ts

173 lines
3.9 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 (const 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 (const key in nyaWords) {
text = text.replaceAll(key, nyaWords[key as keyof typeof nyaWords]);
}
text = replacePunctuation(text);
text = addNyangAtMWord(text);
return text;
}