I have retrieved a document from a URL and saved the response.
There are 3 tasks I need to accomplish here:-
- Calculate the word count in the document.
- Gather information for the top 3 words (sorted by frequency) including synonyms and parts of speech.
API :- https://dictionary.yandex.net/api/v1/dicservice.json/lookup Key :- something..
- Display the results of the top 3 words list in JSON format
All operations should be done asynchronously.
const fetch = require("node-fetch");
async function fetchTest() {
let response = await
fetch('http://norvig.com/big.txt')
.then(response => response.text())
.then((response) => {
console.log(response)
})
.catch(err => console.log(err));
}
(async() => {
await fetchTest();
})();
EDIT:-
const fetch = require("node-fetch");
async function fetchTest(responses) {
let response = await
fetch('http://norvig.com/big.txt')
.then(response => response.text())
.then((response) => {
console.log(response); // ------------------> read text from doc
var words = response.split(/[ \.\?!,\*'"]+/);
console.log(words);
var array = Object.keys(words).map(function(key) {
return { text: words[key], size: key };
});
console.log(array); //-----------------------> occurence count of words
var sortedKeys = array.sort(function (a, b) {
return b.size - a.size ;
});
var newarr = sortedKeys.slice(0, 10);
console.log(newarr); // --------------> top 10 text and occurence
var permittedValues = newarr.map(value => value.text);
console.log(permittedValues); //---------------> sorted key in array
fetch('https://dictionary.yandex.net/api/v1/dicservice.json/lookup?key=domething_&lang=en-en&text=in&callback=myCallback')
.then(responses => responses.text())
.catch(err => console.error(err));
console.log(this.responses);
})
.catch(err => console.log(err));
}
(async() => {
await fetchTest();
})();
Having trouble with fetching API data in JSON format to display synonyms and parts of speech. Can someone help me figure out where I am making a mistake?