GithubHelp home page GithubHelp logo

wordclouds's Introduction

Wordclouds in go.

alt text

How to use

wordCounts := map[string]int{"important":42, "noteworthy":30, "meh":3}

w := wordclouds.NewWordcloud(
	wordCounts,
	wordclouds.FontFile("fonts/myfont.ttf"),
	wordclouds.Height(2048),
	wordclouds.Width(2048),
)

img := w.Draw()

Options

  • Output height and width
  • Font: Must be a valid TTF file.
  • Font max,min size
  • Colors
  • Background color
  • Placement : random or circular
  • Masking

Masking

A list of bounding boxes where the algorithm can not place words can be provided.

The Mask function can be used to create such a mask given a file and a masking color.

boxes := wordclouds.Mask(
	conf.Mask.File,
	conf.Width,
	conf.Height,
	conf.Mask.Color,
)

See the example folder for a fully working implementation.

Speed

Most wordclouds should take a few seconds to be generated. A spatial hashmap is used to find potential collisions.

There are two possible placement algorithm choices:

  1. Random: the algorithms randomly tries to place the word anywhere in the image space.
    • If it can't find a spot after 500000 tries, it gives up and moves on to the next word. It's quite slow.
  2. Spiral: the algorithm starts to place the words on concentric circles starting at the center of the image. It is very fast and is the default algorithm

Contributing

Feel free to create pull requests, I'll gladly review them!

wordclouds's People

Contributors

dependabot[bot] avatar mcoo avatar psidex avatar psykhi avatar sluedecke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

wordclouds's Issues

When I update my pkg I met some problem (It can work before #9)

package group

import (
	"bytes"
	// "fmt"
	"image/color"
	"image/png"

	// "os"
	// "time"

	"github.com/psykhi/wordclouds"
)

var DefaultColors = []color.RGBA{
	{0x1b, 0x1b, 0x1b, 0xff},
	{0x48, 0x48, 0x4B, 0xff},
	{0x59, 0x3a, 0xee, 0xff},
	{0x65, 0xCD, 0xFA, 0xff},
	{0x70, 0xD6, 0xBF, 0xff},
}

var boxes = wordclouds.Mask(
	"source/mask.png",
	600,
	600,
	color.RGBA{
		R: 0,
		G: 0,
		B: 0,
		A: 0,
	})

var colors = make([]color.Color, 0)

func init() {
	for _, c := range DefaultColors {
		colors = append(colors, c)
	}
}

func Rank(inputWords map[string]int) []byte {

	// Load words
	// inputWords := map[string]int{"消息": 42, "是啊": 30, "中文": 15, "也是": 10, "而我": 5, "撒旦": 11, "落后": 11}

	// Load config

	// start := time.Now()
	w := wordclouds.NewWordcloud(inputWords,
		wordclouds.FontFile("source/font.ttf"),
		wordclouds.FontMaxSize(125),
		wordclouds.FontMinSize(10),
		wordclouds.Colors(colors),
		wordclouds.MaskBoxes(boxes),
		wordclouds.Height(600),
		wordclouds.Width(600),
		wordclouds.RandomPlacement(false))

	// outputFile, _ := os.Create("test.png")
	buf := new(bytes.Buffer)
	// Encode takes a writer interface and an image interface
	// We pass it the File and the RGBA
	png.Encode(buf, w.Draw())

	// Don't forget to close files
	// outputFile.Close()
	// fmt.Printf("Done in %v\n", time.Since(start))
	return buf.Bytes()
}

github.com/fogleman/gg.(*Context).SetFontFace(...)
/usr/go/pkg/mod/github.com/fogleman/[email protected]/context.go:681
github.com/psykhi/wordclouds.(*Wordcloud).setFont(0xc0001ac360, 0xfff8000000000000)
/usr/go/pkg/mod/github.com/psykhi/[email protected]/wordcloud.go:142 +0x15b
github.com/psykhi/wordclouds.(*Wordcloud).Place(0xc0001ac360, {{0xc0000c8a28?, 0x0?}, 0x0?, 0xc00022d968?})
/usr/go/pkg/mod/github.com/psykhi/[email protected]/wordcloud.go:172 +0x1a5
github.com/psykhi/wordclouds.(*Wordcloud).Draw(0xc0001ac360)
/usr/go/pkg/mod/github.com/psykhi/[email protected]/wordcloud.go:208 +0x99
bot/wdCloud.Rank(0xc000196030?)

Unable to install this package "spatialhashmap.go:36:5: assignment mismatch"

Hi,

I am trying to install this package for drawing word cloud, but I run into this problem which says
# github.com/psykhi/wordclouds
github.com\psykhi\wordclouds\spatialhashmap.go:36:5: assignment mismatch: 1 variable but uuid.NewV4 returns 2 values

I know similar issues have been answered and closed, but I am still confused about what did you mean by "not put this package under GOPATH".

Thank you in advance!

Memory Leak?

I was making some wordclouds. But my memory usage was rising very fast.
I think that was memory leak, and i found #10
Is the problem stil exist?

This is my pprof

      flat  flat%   sum%        cum   cum%
         0     0% 0.0054% 34775.93MB 88.79%  github.com/psykhi/wordclouds.(*Wordcloud).Draw
         0     0% 0.0054% 34775.93MB 88.79%  github.com/psykhi/wordclouds.(*Wordcloud).Place
         0     0% 0.0054% 34702.48MB 88.60%  github.com/fogleman/gg.LoadFontFace
         0     0% 0.0054% 34702.48MB 88.60%  github.com/psykhi/wordclouds.(*Wordcloud).setFont
    0.51MB 0.0013% 0.0067% 32529.44MB 83.05%  github.com/golang/freetype/truetype.NewFace
32528.93MB 83.05% 83.06% 32528.93MB 83.05%  image.NewAlpha

image

Thinks a lot.

Memory leak

Begin

File: [vdso]
Type: inuse_space
Time: Jul 26, 2021 at 12:29am (CST)
Showing nodes accounting for 170.15MB, 99.12% of 171.65MB total
Dropped 24 nodes (cum <= 0.86MB)

After I drew Some images. Memory usage is rising. only going up :(

File: [vdso]
Type: inuse_space
Time: Jul 26, 2021 at 12:36am (CST)
Showing nodes accounting for 1456.15MB, 98.23% of 1482.41MB total
Dropped 74 nodes (cum <= 7.41MB)

image
image

spatialhashmap.go assignment mismatch

Reproduction Steps:

$ go get -u github.com/psykhi/wordclouds
# github.com/psykhi/wordclouds
..\..\go\src\github.com\psykhi\wordclouds\spatialhashmap.go:36:5: assignment mismatch: 1 variable but uuid.NewV4 returns 2 values

Golang Version:

$ go version
go version go1.13.1 windows/amd64

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.