0

I have fetched a text doc from an url and stored in the response.

I want to do 3 things here:-

  1. Find count of words from the doc.
  2. Collect details for top 3 words (order by word occurrences) and show synonyms and part of speech.

API :- https://dictionary.yandex.net/api/v1/dicservice.json/lookup Key :- something..

  1. Show above top 3 word list results in json format

All should be in asynchronous way.

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();
})();

Unable to do fetch API with json response to show synonyms and part of speech. Where am I going wrong ?

NoobCoder
  • 493
  • 8
  • 25

1 Answers1

0

This sounds a lot like a homework question. If you look at the documentation for the lookup call:

https://tech.yandex.com/dictionary/doc/dg/reference/lookup-docpage/

it offers both an array of synonyms and the part of speech for free. which means you only need to determine the most used words.

It may not be the flashiest solution, but you can solve this in short order using a dictionary. Iterate once storing a count in a dictionary then order the key/value pairs by value and you have the solution. You'll have the top ten words and you have an api to provide the rest, with your current code showing how to async call the api.

Seems straight forward from there.

Lucas Roe
  • 16
  • 4
  • My concern is how can I call short using dictionary and also how can I use API and key here for result. Could you help with the code ? – NoobCoder Jul 23 '20 at 16:12
  • I'm not really sure it's helpful to the exercise if I give you the entire code, or really any of it as it's only a few lines. However, I'd be happy to assist. In this case you are given the text as what i presume is a string. Split that string into words by using .split, and run a loop over them inserting them into an object as keys keeping count of how many you've seen. Then you can create an array of object key values from that object and sort the array by count using sort. You don't send the dictionary to the api call you make 10 different calls with the values. then grab the info you need. – Lucas Roe Jul 23 '20 at 16:18
  • Algorithm would look like. Split response into array of words. Make object out of array of words with values being count. Sort Key Value pairs to get top 10 responses. Send each response to the api 1 by 1. Get values from response you need and print them. – Lucas Roe Jul 23 '20 at 16:19
  • As for how to use the api, I have linked the doc page and you have a perfect API call example in your code. Copy what is being done for "http://norvig.com/big.txt" and change out the link witht he proper params – Lucas Roe Jul 23 '20 at 16:25
  • De nada. If you have any specific issues you bump into feel free to comment back here and I'd be happy to point you back in the right direction. Good luck! – Lucas Roe Jul 23 '20 at 16:30
  • Hi , I am unable to do the API call for each top 10 key value. Can you help me with the code? added in edit. – NoobCoder Jul 23 '20 at 20:24
  • Yeah just looked. So you're pretty close to done. The part your missing kinda depends on what you're being taught and what your teacher may expect. I'm going to assume this is some sort of class using promises. So If I were you,I would take your array and map it into a bunch of fetch calls. Each fetch call is a promise. Then use Promise.All() to wait on the new array. This should return a set of json values which contains what you need. Also, side note, I don't think you need the callback portion of the params. And make sure you swap in your given work for the text portion instead of "in" – Lucas Roe Jul 23 '20 at 22:45
  • So the rest of the algorith would be something like: Take your array of words and map it into an array of promises (aka the fetch command). Run Promise.All on that new array of promises. Extract values. – Lucas Roe Jul 23 '20 at 22:46
  • Also, when you're looking to extract the values, remember it's JSON, not strictly text, that you want. Might want to google the fetch function. – Lucas Roe Jul 23 '20 at 22:47
  • another side note, looking at what you're doing to get a word count, I don't think what your doing is giving you that. Have you done a manual count to double check it? That looks like it's just giving you the indexes for the words and not the count. I would suggest looping over the word list and adding them manually to a dictionary incrementing a counter. Easier to debug until you're sure it's functional. – Lucas Roe Jul 23 '20 at 23:02
  • I checked with word count . you are right. But I am unable to find correct solution for count. Manually its more than 10000 words , not possible. And for fetching the function it is not helping me still . Can you do some changes in my code for this ? – NoobCoder Jul 24 '20 at 07:31
  • For the word count, create an object (let map = {}) run a for loop over the set of words inserting each one if the key doesnt already exist (if (...) map[word]=1) else increase the value map[word]++. Try it on a smaller set of values in a test project to make sure it's doing what you expect. – Lucas Roe Jul 24 '20 at 13:03
  • 1
    As for the fetch one. There are a couple of great examples of calling fetch in a promise.all already available. One is https://stackoverflow.com/questions/31710768/how-can-i-fetch-an-array-of-urls-with-promise-all . You want to do something like this, but instead of passing the word directly into fetch like its' doing pass the word into a string replacing the text query param. – Lucas Roe Jul 24 '20 at 13:07
  • I tried this but something is getting error. Can you edit my code related to what you said? – NoobCoder Jul 27 '20 at 18:20