Last active
September 23, 2022 18:10
-
-
Save shiracamus/d5faac1f1eb81a53872edc31f1d76fdd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const WORDS = { | |
"ポケモン": ['株式会社ポケモン', '任天堂', 'ゲームソフト'], | |
"株式会社ポケモン": ["登場する架空の生物", "アニメ", "メディアミックス"], | |
"任天堂": ["マリオ", "ゼルダ", "星のカービィ"], | |
}; | |
function getAncWords(word) { | |
// 引数の文字列を元に検索でキーワードを文字列の配列で返す処理 | |
// 例:['株式会社ポケモン', '任天堂', 'ゲームソフト']、空配列が返る事もある | |
console.log("getAncWords:", word); | |
return WORDS[word] || []; | |
} | |
const r = { | |
question: async (prompt, callback) => { | |
const poke = await callback("ポケモン"); | |
console.log("poke:", JSON.stringify(poke, null, ' ')); | |
}, | |
}; | |
function makeNode(name) { | |
return { name, nodes: [] }; | |
} | |
async function setNodes(node, limit = 1) { | |
node.nodes = getAncWords(node.name).map(makeNode); | |
if (--limit <= 0) return; | |
for (const child of node.nodes) { | |
await new Promise(resolve => { | |
setTimeout(() => resolve(setNodes(child, limit)), 1000); | |
}); | |
} | |
} | |
r.question( | |
"キーワードを入力して下さい。Enterを押すと探索を開始します。\n", | |
async (name) => { | |
const root = makeNode(name); | |
await setNodes(root, limit = 3); | |
return root; | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment