Skip to content

Instantly share code, notes, and snippets.

@pressure679
Created October 15, 2020 16:52
Show Gist options
  • Save pressure679/680b465d022f32b2808ebf27bfd4f7d7 to your computer and use it in GitHub Desktop.
Save pressure679/680b465d022f32b2808ebf27bfd4f7d7 to your computer and use it in GitHub Desktop.
package main
import (
"strings"
"flag"
"os"
"bufio"
"time"
"image/png"
"math/rand"
// "sort"
"github.com/liamcottam/wordcloud"
)
func main() {
file := flag.String("file", "", "File to read from")
flag.Parse()
osFile, err := os.Open(*file)
if err != nil { panic(err) }
// var mapwordcloud map[string]float64 = make(map[string]float64)
bufioReader := bufio.NewReader(osFile)
var wcwords []*wordcloud.Word
var cnt uint8 = 1
for {
line, _, err := bufioReader.ReadLine()
if err != nil {
if err.Error() == "EOF" {
break
} else {
panic(err)
}
}
parts := strings.Split(string(line), " $:$ ")
// mapwordcloud[parts[1]], err = strconv.ParseFloat(parts[0], 64)
if cnt <= 6{
wcwords = append(wcwords, &wordcloud.Word{
Text: parts[1],
Size: float64(28),
})
} else if cnt > 6 && cnt <= 18 {
wcwords = append(wcwords, &wordcloud.Word{
Text: parts[1],
Size: float64(19),
})
} else {
// case cnt > 9 && cnt <= 18:
wcwords = append(wcwords, &wordcloud.Word{
Text: parts[1],
Size: float64(11),
})
}
if cnt == 36 { break }
cnt++
}
if err = osFile.Close(); err != nil { panic(err) }
source := rand.NewSource(time.Now().UnixNano())
rand := rand.New(source)
f, err := os.Create(*file + ".wc.png")
if err != nil { panic(err) }
defer f.Close()
generator := wordcloud.WordCloud{}
img, err := generator.Generate(&wordcloud.GeneratorOptions{
Width: 480,
Height: 640,
MinSize: 11,
FontPath: "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf",
Words: wcwords,
Rand: rand,
Angles: []float64{
0,
90,
},
})
if err != nil { panic(err) }
img = wordcloud.Crop(img)
png.Encode(f, img)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment