Maximum Number of Words Allowed in Word Cloud

I am currently utilizing a well-known word cloud library from this source: https://github.com/jasondavies/d3-cloud

My code is based on a replica of this block: http://bl.ocks.org/blockspring/847a40e23f68d6d7e8b5

In my dataset, I am aiming to limit the maximum number of words displayed in the word cloud. While the library provides functions for rotation, font size, spiral method, and more, it doesn't seem to have a direct way to determine the maximum word count to show.

To optimize computation, I believe feeding a subset of the original word count would be more efficient. However, I'm unsure if the word_count object is sorted by frequency before being processed by cloud.js as there are no apparent .sort calls.

If cloud.js does sort the word_count object by frequency or tf-idf, I would need to wait until after the list is generated to return the top k words, indicating that the entire text file has been iterated through.

Despite potential speed improvements in visualization, limiting the display to the top k (most frequent) words, let's say 20 excluding common words, might not significantly impact the underlying algorithm performance.

Visualizing it, larger font sizes indicate higher frequencies, which aligns with choosing the top k as the largest font-size words.

If anyone experienced with this visualization type can guide me on adjusting the code to return the top k words, I would greatly appreciate it.

Note: I initially posted this query on GitHub but was redirected here due to relevance. I've attempted to clarify and provide sufficient context, albeit feared it may still be considered too vague for stack overflow. Thank you for understanding.

Appreciatively,

Answer №1

Maybe

let sentence = text.split(/[ '\-\(\)\*":;\[\]|{},.!?]+/),
  threshold = 5;
if (sentence.length == 1) {
  word_freq[sentence[0]] = 1;
} else {
  sentence.forEach(function(word) {
    let word = word.toLowerCase();
    if (word != "" && common_words.indexOf(word) == -1 && word.length > 1) {
      if (word_freq[word]) {
        word_freq[word]++;
      } else {
        word_freq[word] = 1;
      }
    }
  });
  for (let word in word_freq) {
    if (word_freq[word] < threshold) delete word_freq[word];
  }
}

You could consider implementing a counter to adjust the limit if there are too many words, ensuring Object.keys(word_freq).length is less than 20000.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Node.js and MongoDB to filter a sub array within an array of objects

Hello everyone, I currently have an array of objects containing some populated fields. Below is the product schema: import mongoose, { Schema } from 'mongoose'; const productSchema = new mongoose.Schema( { name: String, description: S ...

Revalidation of paths in NextJS 13 is functioning properly for newly created comments, however, it is not working as

I'm encountering an issue with the implementation of revalidatePath() in my CommentForm and PostForm components. Despite both components having a similar structure and functionality, only the CommentForm component is able to utilize revalidatePath() c ...

I'm noticing multiple repeated entries appearing when I try to set the cookie - what could be causing

Currently, I am utilizing the library js-cookie instead of my previous use of jquery.cookie. I have encountered an issue related to duplicating cookie entries. There are occasions when I invoke the following method: Cookies.set('my-cookie-name', ...

Can't get className to work in VueJS function

I need help changing the classNames of elements with the "link" class. When I call a method via a click action, I can successfully get the length of the elements, but adding a class does not seem to work. Does anyone have any insights into this issue? HTM ...

Enhance your viewing experience by magnifying a specific element, all while maintaining full control to navigate

Is it possible to create a zoom effect on a webpage that focuses on one specific element while still allowing for navigation and scrolling? I have searched online for plugins like Fancybox and Zoomooz, but none of them offer the functionality I need. I sp ...

Execute JavaScript function with a delay once the webpage has finished loading

I am currently working on a basic function that runs when the page is loaded. However, even though the page has finished loading, there are times when it takes about half a second for the content to actually appear. While this typically wouldn't be an ...

Executing Cascading Style Sheets (CSS) within JQuery/Javascript

I've been encountering a problem with my website. I have implemented grayscale filters on four different images using CSS by creating an .svg file. Now, I'm looking to disable the grayscale filter and show the original colors whenever a user clic ...

It seems that the `to` required prop was missing in the `Link` component of React-Router

Currently, I am facing an issue while trying to integrate react-router. The error message I'm encountering is: Failed propType: Required prop to was not specified in Link. Check the render method of app. Unfortunately, I am unable to pinpoint ex ...

Adding data to an array using jQuery

I have a list of items called data and an empty array called quotations: var data = {}; var quotations = []; My goal is to populate the quotations array with data values. Each time I add new data, it is successfully added, but all data values end u ...

Optimal method for displaying videos in a GatsbyJS front-end, utilizing Node.js for the back-end, and MySQL for the database

I am just starting out with this, so I would appreciate any suggestions on the best way to stream videos in a gatsby js app. The app utilizes node js for the back end and all the data is stored in mysql. My current approach involves storing videos as BLO ...

Unable to execute ajax on dom ready in Internet Explorer 9

Here is some code that needs to be executed on DOM ready without any user interaction: if($.browser.msie){ console.log("Using getJSON"); $.getJSON(baseUrl,function(){ alert('hi'); }); }else{ setTimeou ...

Foundation plus Ajax page reloading equals a powerful combination

There seems to be a problem with my website: Whenever I click on a link in the menu, the page reloads smoothly using ajax but unfortunately, the foundation javascripts stop working. Can anyone provide any suggestions on what might be causing this issue? ...

What is the best approach to implement pagination in the UI-Bootstrap Typeahead Directive?

In my current Angular Project, I am utilizing the UI-Bootstrap's Typeahead Directive for a specific purpose. However, I am encountering an issue when dealing with a large amount of similar data using this directive. It seems that displaying only the ...

Retrieving date from timestamp in a node.js environment

Can someone help me figure out how to display my timestamp as the date in the front end? I've tried multiple methods without success. Here is the code snippet: formulaire.addEventListener('submit', posteValidation); /** * Function to add a ...

What is the best way to preserve text input value when going back a page in PHP?

Is there a way to keep the text box value entered by the user even after navigating away from the page? Below is an example of what I am trying to achieve in my code: **main.php** <html> <head></head> <body> <i ...

Newbie's Guide - Building React/React-Bootstrap JavaScript Components Post Linking CDNs in index.html

Exploring Glitch hosting for a React/React-Bootstrap website as part of my web development training. Although these tools are new to me, I have years of experience as a developer. Successfully linked React, React-Dom, Babel, and React-Bootstrap CDN's ...

Tips for using jQuery dropdown menus

I am currently in the process of creating a prototype for a quick dropdown menu using jQuery. However, I have reached the limits of my knowledge on jQuery and could use some assistance in solving my issue. My objective is to have a dropdown animate down w ...

Ways of converting a negative lookbehind into an ES5-friendly expression

In my code, I have a RegExp that works perfectly, but it only functions with ES2018 due to its use of negative lookbehinds. The problem is that a library is using this RegExp function, so modifying how it's used is not an option. I attempted to add n ...

Create a dynamic Bootstrap progress bar that smoothly transitions from 0 to 100%

I am currently utilizing Twitter Bootstrap to construct my webpage. Here is the snippet of HTML code I have: <div class="btn-group"> <button type="button" class="btn btn-success">Connect</button> <button type="button" class=" ...

Alter the jQuery in an Iframe to make changes to the parent document

Exploring a unique use case where I am sandboxing multiple JavaScript libraries into hidden iframes. Within the website, there are various widgets, all sourced from the same domain, that might require different versions of JS libraries. To avoid global con ...