var events = require("../events.js");
function getGroups(text, size){
return text
.toLowerCase()
.split(/[^a-z]/) //split on any non a-z character to get words
.map((word)=>{ //turn each word into all size-length groups
groups = []
for(var i = 0; i <= (word.length - size); i++){
groups.push(word.substr(i, size));
}
return groups;
})
.reduce((allGroups, groups)=>( //merge these groups
allGroups.concat(groups)
), []);
}
function getFirstLetters(text){
return text
.toLowerCase()
.split(/[^a-z]/) //split on any non a-z character to get words
.filter((word)=>(word.length > 0)) //filter out zero length words
.map((word)=>(word[0])); //return first char
}
function getFrequency(groups){
return groups
.reduce((frequencies, group)=>{
if(!frequencies[group]){ //add to object if not already present
frequencies[group] = 0;
}
frequencies[group] += 1;
return frequencies;
}, {})
};
function topGroupsByFrequency(groups, alreadyPercentage){
var output = {};
output.labels = Object.keys(groups).sort((a, b)=>(groups[b] - groups[a])).slice(0, 26); //get top 26 titles by key
output.values = output.labels.map((groupName)=>(groups[groupName]));
if(!alreadyPercentage){
var total = Object.values(groups).reduce((a, b)=>(a + b));
output.values = output.values.map((value)=>(value / total * 100)); //calculates as percentage of all items
}
return output;
}
module.exports = {
name: "Frequency Analysis",
inputs: {
input: {
name: "Input",
type: "text",
required: true,
inline: false
}
},
output: false,
execute: function({input}, block){
var topGroups = {};
if(!isNaN(parseInt(block.properties.type))){
//frequency of group with length type
topGroups = topGroupsByFrequency(getFrequency(getGroups(input, parseInt(block.properties.type))));
}
else if(block.properties.type == "first"){
//first letter of each word
topGroups = topGroupsByFrequency(getFrequency(getFirstLetters(input, parseInt(block.properties.type))));
}
$(block.elem).data("chartTop").data.labels = topGroups.labels;
$(block.elem).data("chartTop").data.datasets[0].data = topGroups.values;
$(block.elem).data("chartTop").update();
},
size: { //if you change this, update static widths in HTML as well
height: 400,
width: 400
},
pageBlock: {
html: `