wordclouds

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 2, 2021 License: Apache-2.0 Imports: 17 Imported by: 0

README

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 files.
  • 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!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Box

type Box struct {
	Top    float64
	Left   float64
	Right  float64
	Bottom float64
}

func Mask

func Mask(path string, width int, height int, exclude color.RGBA) ([]*Box, error)

Mask creates a slice of box structs from a given mask image to be passed to wordclouds.MaskBoxes.

func (*Box) String

func (a *Box) String() string

type Option

type Option func(*Options) error

func BackgroundColor

func BackgroundColor(color color.Color) Option

Output file background color

func Colors

func Colors(colors []color.Color) Option

Colors to use for the words

func Debug

func Debug() Option

Draw bounding boxes around words

func Font

func Font(file io.Reader) Option

Font to use.

func FontMaxSize

func FontMaxSize(max int) Option

Max font size

func FontMinSize

func FontMinSize(min int) Option

Min font size

func Height

func Height(h int) Option
func Logo(file io.Reader) Option

Logo to use.

func MaskBoxes

func MaskBoxes(mask []*Box) Option

A list of bounding boxes where words can not be placed. See Mask

func RandomPlacement

func RandomPlacement(do bool) Option

Place words randomly

func Width

func Width(w int) Option

type Options

type Options struct {
	FontMaxSize     int
	FontMinSize     int
	RandomPlacement bool
	Font            *truetype.Font
	Colors          []color.Color
	BackgroundColor color.Color
	Width           int
	Height          int
	Mask            []*Box
	Debug           bool
}

type Wordcloud

type Wordcloud struct {
	// contains filtered or unexported fields
}

Wordcloud object. Create one with NewWordcloud and use Draw() to get the image

func NewWordcloud

func NewWordcloud(wordList map[string]int, options ...Option) (*Wordcloud, error)

Initialize a wordcloud based on a map of word frequency.

func (*Wordcloud) Draw

func (w *Wordcloud) Draw() image.Image

Draw tries to place words one by one, starting with the ones with the highest counts

func (*Wordcloud) Place

func (w *Wordcloud) Place(wc wordCount) bool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL