Skip to content

Instantly share code, notes, and snippets.

@outloudvi
Last active May 9, 2020 16:24
Show Gist options
  • Save outloudvi/c69da95a6d7a4391411a30bda5285a9b to your computer and use it in GitHub Desktop.
Save outloudvi/c69da95a6d7a4391411a30bda5285a9b to your computer and use it in GitHub Desktop.
Categorize names in YouTube video descriptions.
export interface Alias {
name: string,
aliases: string[];
}
export interface Results {
[key: number]: string;
}
export interface categorizeOptions {
urlDistance: number,
blockSplitDistance: number
}
export default function nameCategorize(
text: string,
aliasList: Alias[],
options: categorizeOptions = {
urlDistance: 5,
blockSplitDistance: 50
}
) {
let target = text;
target = replaceUrl(target, options.urlDistance);
const locations = matchKnownAliases(target, aliasList);
const groups: string[][] = [];
let currentGroup: string[] = [];
let lastLocation = 0;
// @ts-ignore
for (const [location, name] of Object.entries(locations)) {
if (location - lastLocation > options.blockSplitDistance) {
if (currentGroup.length) {
groups.push(currentGroup);
currentGroup = [];
}
}
// @ts-ignore
if (!currentGroup.includes(name))
currentGroup.push(name);
lastLocation = location;
}
if (currentGroup.length) {
groups.push(currentGroup);
}
return groups;
}
// Replace URL to spaces.
function replaceUrl(text: string, space: number): string {
// TODO
return text;
}
// Replace known aliases to names.
function matchKnownAliases(text: string, aliasList: Alias[]): Results {
let ret = {};
for (const aliasItem of aliasList) {
let name = aliasItem.name;
const matchLocations = [];
for (const alias of aliasItem.aliases) {
let loc = -1;
do {
loc = text.indexOf(alias, loc + 1);
if (loc !== -1) {
matchLocations.push(loc);
text = text.slice(0, loc) + text.slice(loc + alias.length);
ret[loc] = name;
}
} while (loc !== -1);
}
}
return ret;
}
/// ----------------------------------------------------------------------------------------
// Example
const desc =
`配信タグ:#おしがまテトリス
🌸最強のレイドボス🌸
星街すいせい\nhttps://www.youtube.com/channel/UC5CwaMl1eIgY8h02uZw7u8A
🌸本日の犠牲者の皆さん🌸
神楽めあ\nhttps://www.youtube.com/channel/UCWCc8tO-uUl_7SJXIKJACMw
夏色まつり\nhttps://www.youtube.com/channel/UCQ0UDLQCjY0rmuxCDE38FGg
しぐれうい\nhttps://www.youtube.com/channel/UCt30jJgChL8qeT9VPadidSw
赤井はあと\nhttps://www.youtube.com/channel/UC1CfXB_kRs3C-zaeTG3oGyg
--------------------------
🌸こちらもチャンネル登録お願いします!🌸
🐕犬山たまきの妹「白雪みしろ」\nhttps://www.youtube.com/channel/UCC0i9nECi4Gz7TU63xZwodg\nhttps://twitter.com/mishiro_seiso
🐕犬山たまきの妹「愛宮みるく」\nhttps://www.youtube.com/channel/UCJCzy0Fyrm0UhIrGQ7tHpjg\nhttps://twitter.com/Enomiya_MILK
--------------------------
🐕チャンネルメンバーシップ登録について\n「メンバーになる」ボタンからメンバーシップを登録して頂くと\n名前の後ろにマークが付きます!\n佃煮のりおママ特製の絵文字も使えます♪\nコミュニティにメンバー限定自撮りも公開中(U^ω^)\nhttps://www.youtube.com/channel/UC8NZiqKx6fsDT3AVcMiVFyA/join
🐕Super chatについて\n・配信中にコメントに反応できるかは、その時次第です。\n・スパチャを送ってくれた方のお名前を、配信の最後に読み上げさせて頂きます。\n・コラボの時のスパチャも個人枠でまとめて読ませて頂きます。\n・スパチャはご無理をしない程度にお願いします。いつも皆様の応援、感謝しておりますm(_ _)m
🐕ボクとの約束ゴト\n・視聴者さん同士のチャット内会話はご遠慮ください。\n・悪質なコメントが続く場合はNGさせて頂く場合が御座います。\n・「のりプロ」はライバルではなく仲間です。演者同士を比べたり、対立煽りになるようなコメントはNGにさせて頂く場合が御座います。\n・安全確認、自己管理の上で配信を行っております。心配や杞憂はご遠慮下さい。心配ではなく、応援をして頂けると嬉しいです!\n・マナーを守って楽しい配信にしましょう!
--------------------------
🐕Twitter\n佃煮のりお/犬山たまき 兼用Twitter\nhttps://twitter.com/norioo_
🐕Live2D制作:rariemonn様
🐕チャンネルロゴ:木緒なち/葉山みど様
🐕チャンネル背景デザイン:しお様
🐕犬山たまき3Dモデル\nモデラ―:ヒノイチ様\nモデラー:トミタケ様\nVRM変換:大葉真琴様\n仲介協力:ナルパジン様\n3D技術・スタジオ:KiLA(LiveCartoon.LLC)`;
const aliases = [
{
name: "星街彗星",
aliases: [
"星街すいせい",
"https://www.youtube.com/channel/UC5CwaMl1eIgY8h02uZw7u8A"
]
},
{
name: "神乐Mea",
aliases: [
"神楽めあ",
"https://www.youtube.com/channel/UCWCc8tO-uUl_7SJXIKJACMw",
]
}, {
name: "夏色祭",
aliases: [
"夏色まつり",
"https://www.youtube.com/channel/UCQ0UDLQCjY0rmuxCDE38FGg"
]
}, {
name: "时雨",
aliases: [
"しぐれうい",
"https://www.youtube.com/channel/UCt30jJgChL8qeT9VPadidSw"
]
}, {
name: "赤井心",
aliases: [
"赤井はあと",
"https://www.youtube.com/channel/UC1CfXB_kRs3C-zaeTG3oGyg"
]
},
{
name: "白雪深白",
aliases: [
"白雪みしろ",
"https://www.youtube.com/channel/UCC0i9nECi4Gz7TU63xZwodg",
"https://twitter.com/mishiro_seiso"
]
}, {
name: "爱宫Milk",
aliases: [
"愛宮みるく",
"https://www.youtube.com/channel/UCJCzy0Fyrm0UhIrGQ7tHpjg",
"https://twitter.com/Enomiya_MILK"
]
}
];
console.log(nameCategorize(desc, aliases));
@outloudvi
Copy link
Author

outloudvi commented May 9, 2020

$ tsc categorize.ts
$ node categorize.js
[ [ '星街彗星', '神乐Mea', '夏色祭', '时雨', '赤井心' ], [ '白雪深白', '爱宫Milk' ] ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment