You probably played the game of telephone as a kid, but it's kind of boring as an adult. But what if we get the computer to play for us instead? First, let's require some utilities from other notebooks, and define a few of our own.
// translations, powered by yandex. // To use, make sure you set process.env.YANDEX_API_KEY to your own API key! // You can sign up here: https://tech.yandex.com/keys/get/?service=trnsl var yandex = require('notebook')('tonic/yandex-translation-api/1.1.0'); // Some common english phrases var sayings = require('notebook')('capicue/sayings/2.0.0'); // Just a few niceties around Math.random() var rand = require('notebook')('tonic/random/5.0.0'); function randomLanguage() { var languageKeys = Object.keys(languages); return languageKeys[rand(languageKeys.length)]; } // Grab a random language from the supported languages. var languages = await yandex.languages();
Alright, now we're ready. The basic idea is to start with a phrase and run it through a translation API a whole bunch of times, finally translating it back into the original language to see what we get.
// The core game async function telephone(text, language = "en", steps = 10) { return await step([{lang: language, text: text}]); async function step(history) { var last = history[history.length - 1]; var toLanguage = (history.length === steps) ? history[0].lang : randomLanguage(); var result = await yandex.translate(last.lang, toLanguage, last.text); var results = history.concat([{text: result, lang: toLanguage}]); if (history.length === steps) return results.map(function(step){ return [step.text, languages[step.lang]]; }); return step(results); } } var sentence = sayings.random(); await telephone(sentence);
Turn oil.

no comments

    sign in to comment

    running