Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Execute Node.js Online

const https = require('https');
var request = require('sync-request');

var dictionary_key = "dict.1.1.20190324T215034Z.b42d9046a9fc4903.e8ea2f4864d4cd4db4b5bec3aa57c56fce83f449";

var dictionary = "https://dictionary.yandex.net/api/v1/dicservice.json/lookup?key=" + dictionary_key + "&lang=en-de&text=";


var httpRequest = function (a){
    var adress = dictionary + a
https.get(adress, (resp) => {
  let data = '';

  // A chunk of data has been recieved.
  resp.on('data', (chunk) => {
    data += chunk;
  });

  // The whole response has been received. Print out the result.
  resp.on('end', () => {
    console.log(data + "here is HTTP request");                  //console log recieved data
    return data
  });

}).on("error", (err) => {
  console.log("Error: " + err.message);
})};

var httpRequestSync = function (a){
  var adress = dictionary +a;
  var res = request('GET', adress);
  var resConverted = res.getBody()
  console.log(resConverted + "here is http request");
  return resConverted
}

var splitString = function (txt, at){
    return txt.split(at)
};
var getAPI = function(b){
    var arr = [];
    for (var i = 0; i<b.length; i++ ){
        arr.push(httpRequestSync(b[i]))
    }
    console.log(arr+ "here is getApi")                    //console log the recieved objects
    return arr
};
var newWord = function (a,b){
    var str = "";
    var tag ="";
    str = a.text + ";" ; //English word
    str = str + a.pos + ";" ; //position
    str = str + b.text + ";"; //German word
    str = str + b.pos + tag +"\n\r"; //position +(tag)+ newline
    return str
};
var createOutput = function(input){
    var outp = "";
    for (var k = 0; k < input.length; k++){//for each searched word
        for(var l = 0;l< input[k].def.length; l++){//for each definition of word
            for(var m=0; m<input[k].def[l].tr.length;m++){//for each translation of word definition
                var definition = input[k].def[l];
                var translation = input[k].def[l].tr[m];
                outp = outp + newWord(defintion, translation)
            }
        }
    }
    return outp
};




var doIt = function (words) {
    
    var wordsArr = splitString(words, ",");
    var responseArr = getAPI(wordsArr);
    var anki = createOutput(responseArr);
    console.log(anki)
    
};

var textToTranslate = "wracked,dispensed,despair"; //Enter text here

doIt(textToTranslate);


{"head":{},"def":[]},{"head":{},"def":[]},{"head":{},"def":[{"text":"despair","pos":"noun","ts":"dɪsˈpɛə","tr":[{"text":"Verzweiflung","pos":"noun","gen":"f","mean":[{"text":"desperation"}]}]},{"text":"despair","pos":"verb","ts":"dɪsˈpɛə","tr":[{"text":"verzweifeln","pos":"verb","mean":[{"text":"desperation"}]}]}]}


Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.