Skip to content

Instantly share code, notes, and snippets.

@kishorpawar
Created July 23, 2019 10:48
Show Gist options
  • Save kishorpawar/6ae90ddd7d66e5f9dafd95d2e78dfad9 to your computer and use it in GitHub Desktop.
Save kishorpawar/6ae90ddd7d66e5f9dafd95d2e78dfad9 to your computer and use it in GitHub Desktop.
/***
Please test this in browser by disabling web security to avoid CORS issues.
***/
let Analyse = class
{
let bigtext_url = "http://norvig.com/big.txt";
let lookup_url = "https://dictionary.yandex.net/api/v1/dicservice.json/lookup"
let api_key = "dict.1.1.20170610T055246Z.0f11bdc42e7b693a.eefbde961e10106a4efa7d852287caa49ecc68cf"
let get_big_text = function()
{
let bigtext = new XMLHttpRequest();
bigtext.onreadystatechange = function()
{
if(this.readyState == 4)
{
find_occurances(this.responseText);
}
}
bigtext.open("GET", bigtext_url, true);
bigtext.send()
};
let find_occurances = function(content)
{
console.log("Hold on!!! This shall take time.")
//For English only as specified in the big text;
content = content.replace(/[^a-zA-Z ]/g, " ");
unique_words = Array.from(new Set(content.toLowerCase().split(' ')));
counts = [];
for(var j = 0; j < unique_words.length - 1; j++)
{
expr = new RegExp(`${unique_words[j]}`, 'gi').strip();
count = (content.match(expr) || []).length;
counts.push([unique_words[j], count]);
}
get_top(counts, 10);
}
let get_top = function(arr, top=10)
{
arr = arr.sort((a,b) => b[1]-a[1]);
top_10 = arr.splice(0, top);
output = {}
for(var i=0; i<top_10.length; i++)
{
details = get_details(top_10[i][0]);
details['count'] : top_10[i][1];
output[top_10[i]] = details;
}
console.log(output)
}
let get_details = function(word)
{
let details = new XMLHttpRequest();
details.onreadystatechange = function()
{
if(this.readyState == 4 && this.status == 200)
{
result = JSON.parse(this.responseText);
pos = result.def[0].pos
synonyms = result.def[0].tr.map(syno=>syno.text);
return {'pos' : pos, 'synonyms' : synonyms};
}
}
params = "?key="+api_key+"&lang=en-en&text="+word;
details.open('GET', lookup_url+params, true);
details.send();
}
}
analyse = new Analyse();
analyse.get_big_text();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment