wordcloud-art-svg 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +27 -0
- data/exe/wordcloud-art-svg +32 -0
- data/lib/wordcloud_art_svg/version.rb +3 -0
- data/lib/wordcloud_art_svg.rb +63 -0
- metadata +50 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 24d01c246c65aac006387ffb99724a50e83a4d640f6b6af605911d10094768ef
|
|
4
|
+
data.tar.gz: e76789dab4ba284e660659c24220a2ee4d3b463bdc1d6dcc1df1966e995d7eb5
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: '0297d296948576df7ca8887f7c006f45a9f7d299f374fe7b0217d744ded423af60aabd4c55aa6b4314c6ef9d2211c4e2c9e7a964e4185529a3be59d8284a3102'
|
|
7
|
+
data.tar.gz: 00d0c05c69d5f1e361826ce64aa2745994c726334570d1b6fea447ffd81d6662b2669ee91f300b046e5466df1acf2d42ad0756f136ebcb7aaafd486837460dea
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Wordcloud.art
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Wordcloud.art SVG
|
|
2
|
+
|
|
3
|
+
Generate an SVG word cloud locally from text or a weighted word list. The command does not require a Wordcloud.art account or send input to a server. Words are placed horizontally using a simple collision-free spiral layout; results will differ from the interactive site's designs.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
gem install wordcloud-art-svg
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Use
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
wordcloud-art-svg --text 'design design words colors colors' --output cloud.svg
|
|
15
|
+
wordcloud-art-svg --input notes.txt --output notes.svg --width 1200 --height 800
|
|
16
|
+
wordcloud-art-svg --weights words.tsv --output weighted.svg
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Weighted input is UTF-8 TSV with one word and positive weight per line:
|
|
20
|
+
|
|
21
|
+
```text
|
|
22
|
+
design 8
|
|
23
|
+
visualization 5
|
|
24
|
+
words 3
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
For shaped word clouds, more colors, and animation, visit [Wordcloud.art](https://www.wordcloud.art/).
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
require 'optparse'
|
|
3
|
+
require_relative '../lib/wordcloud_art_svg'
|
|
4
|
+
|
|
5
|
+
options = { width: 800, height: 500, background: 'white', max_words: 80 }
|
|
6
|
+
parser = OptionParser.new do |opts|
|
|
7
|
+
opts.banner = 'Usage: wordcloud-art-svg (--text TEXT | --input FILE | --weights FILE) --output FILE.svg'
|
|
8
|
+
opts.on('--text TEXT', 'Text to render') { |v| options[:text] = v }
|
|
9
|
+
opts.on('--input FILE', 'UTF-8 text file') { |v| options[:input] = v }
|
|
10
|
+
opts.on('--weights FILE', 'UTF-8 TSV file: WORD<TAB>WEIGHT') { |v| options[:weights] = v }
|
|
11
|
+
opts.on('--output FILE', 'SVG output file') { |v| options[:output] = v }
|
|
12
|
+
opts.on('--width NUMBER', Integer) { |v| options[:width] = v }
|
|
13
|
+
opts.on('--height NUMBER', Integer) { |v| options[:height] = v }
|
|
14
|
+
opts.on('--background COLOR', 'SVG color value') { |v| options[:background] = v }
|
|
15
|
+
opts.on('--max-words NUMBER', Integer) { |v| options[:max_words] = v }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
begin
|
|
19
|
+
parser.parse!
|
|
20
|
+
sources = [:text, :input, :weights].count { |key| options.key?(key) }
|
|
21
|
+
raise ArgumentError, 'choose exactly one of --text, --input, or --weights' unless sources == 1
|
|
22
|
+
raise ArgumentError, '--output must end in .svg' unless options[:output]&.downcase&.end_with?('.svg')
|
|
23
|
+
text = options[:text] || File.read(options[:input] || options[:weights], encoding: 'UTF-8')
|
|
24
|
+
frequencies = options[:weights] ? WordcloudArtSvg.frequencies_from_tsv(text) : WordcloudArtSvg.frequencies_from_text(text)
|
|
25
|
+
svg = WordcloudArtSvg.render(frequencies, width: options[:width], height: options[:height], background: options[:background], max_words: options[:max_words])
|
|
26
|
+
File.write(options[:output], svg, encoding: 'UTF-8')
|
|
27
|
+
puts options[:output]
|
|
28
|
+
rescue OptionParser::ParseError, ArgumentError, SystemCallError => error
|
|
29
|
+
warn "wordcloud-art-svg: #{error.message}"
|
|
30
|
+
warn parser
|
|
31
|
+
exit 1
|
|
32
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
require 'cgi'
|
|
2
|
+
|
|
3
|
+
module WordcloudArtSvg
|
|
4
|
+
module_function
|
|
5
|
+
|
|
6
|
+
def frequencies_from_text(text)
|
|
7
|
+
words = text.scan(/[[:alnum:]][[:alnum:]'’-]*/u)
|
|
8
|
+
counts = Hash.new(0.0)
|
|
9
|
+
words.each { |word| counts[word.downcase] += 1.0 }
|
|
10
|
+
raise ArgumentError, 'input has no words' if counts.empty?
|
|
11
|
+
counts
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def frequencies_from_tsv(text)
|
|
15
|
+
counts = Hash.new(0.0)
|
|
16
|
+
text.each_line.with_index(1) do |raw, line_number|
|
|
17
|
+
line = raw.strip
|
|
18
|
+
next if line.empty? || line.start_with?('#')
|
|
19
|
+
fields = line.split("\t", -1)
|
|
20
|
+
raise ArgumentError, "line #{line_number}: expected WORD<TAB>WEIGHT" unless fields.length == 2 && !fields[0].strip.empty?
|
|
21
|
+
weight = Float(fields[1]) rescue nil
|
|
22
|
+
raise ArgumentError, "line #{line_number}: weight must be positive and finite" unless weight && weight.finite? && weight.positive?
|
|
23
|
+
counts[fields[0].strip] += weight
|
|
24
|
+
end
|
|
25
|
+
raise ArgumentError, 'word list has no usable entries' if counts.empty?
|
|
26
|
+
counts
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def render(frequencies, width: 800, height: 500, background: 'white', max_words: 80)
|
|
30
|
+
raise ArgumentError, 'width and height must be positive' unless width.to_i.positive? && height.to_i.positive?
|
|
31
|
+
raise ArgumentError, 'max_words must be positive' unless max_words.to_i.positive?
|
|
32
|
+
raise ArgumentError, 'word list has no usable entries' if frequencies.empty?
|
|
33
|
+
width, height = width.to_i, height.to_i
|
|
34
|
+
entries = frequencies.sort_by { |word, weight| [-weight, word] }.first(max_words.to_i)
|
|
35
|
+
highest = entries.first[1].to_f
|
|
36
|
+
placed = []
|
|
37
|
+
elements = []
|
|
38
|
+
entries.each_with_index do |(word, weight), index|
|
|
39
|
+
next unless weight.to_f.finite? && weight.to_f.positive?
|
|
40
|
+
size = (14 + 58 * Math.sqrt(weight.to_f / highest)).round
|
|
41
|
+
size = [size, [width / 3, height / 3].min].min
|
|
42
|
+
estimated_width = word.each_char.sum { |char| char.ord > 0x2e80 ? 1.0 : 0.6 } * size
|
|
43
|
+
box_width = estimated_width + 8
|
|
44
|
+
box_height = size * 1.25 + 8
|
|
45
|
+
next if box_width > width || box_height > height
|
|
46
|
+
1800.times do |step|
|
|
47
|
+
radius = 2.6 * Math.sqrt(step)
|
|
48
|
+
angle = step * 2.399963229728653 + index
|
|
49
|
+
x = width / 2.0 + radius * Math.cos(angle) - box_width / 2.0
|
|
50
|
+
y = height / 2.0 + radius * Math.sin(angle) - box_height / 2.0
|
|
51
|
+
next if x < 0 || y < 0 || x + box_width > width || y + box_height > height
|
|
52
|
+
next if placed.any? { |bx, by, bw, bh| x < bx + bw && x + box_width > bx && y < by + bh && y + box_height > by }
|
|
53
|
+
placed << [x, y, box_width, box_height]
|
|
54
|
+
color = %w[#275dad #29a69a #ef7d57 #6b5b95 #c15c86][index % 5]
|
|
55
|
+
elements << %(<text x="#{(x + 4).round(1)}" y="#{(y + size + 2).round(1)}" font-size="#{size}" fill="#{color}">#{CGI.escapeHTML(word)}</text>)
|
|
56
|
+
break
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
raise ArgumentError, 'canvas is too small to place any words' if elements.empty?
|
|
60
|
+
safe_background = CGI.escapeHTML(background.to_s)
|
|
61
|
+
%(<?xml version="1.0" encoding="UTF-8"?>\n<svg xmlns="http://www.w3.org/2000/svg" width="#{width}" height="#{height}" viewBox="0 0 #{width} #{height}">\n<rect width="100%" height="100%" fill="#{safe_background}"/>\n<g font-family="Arial, Helvetica, sans-serif" font-weight="bold">\n#{elements.join("\n")}\n</g>\n</svg>\n)
|
|
62
|
+
end
|
|
63
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: wordcloud-art-svg
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Wordcloud.art
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-11-03 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: A small offline Ruby command that lays out weighted words in a collision-free
|
|
14
|
+
SVG word cloud.
|
|
15
|
+
email:
|
|
16
|
+
executables:
|
|
17
|
+
- wordcloud-art-svg
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- LICENSE
|
|
22
|
+
- README.md
|
|
23
|
+
- exe/wordcloud-art-svg
|
|
24
|
+
- lib/wordcloud_art_svg.rb
|
|
25
|
+
- lib/wordcloud_art_svg/version.rb
|
|
26
|
+
homepage: https://www.wordcloud.art/
|
|
27
|
+
licenses:
|
|
28
|
+
- MIT
|
|
29
|
+
metadata:
|
|
30
|
+
homepage_uri: https://www.wordcloud.art/
|
|
31
|
+
post_install_message:
|
|
32
|
+
rdoc_options: []
|
|
33
|
+
require_paths:
|
|
34
|
+
- lib
|
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '2.6'
|
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
41
|
+
requirements:
|
|
42
|
+
- - ">="
|
|
43
|
+
- !ruby/object:Gem::Version
|
|
44
|
+
version: '0'
|
|
45
|
+
requirements: []
|
|
46
|
+
rubygems_version: 3.0.3.1
|
|
47
|
+
signing_key:
|
|
48
|
+
specification_version: 4
|
|
49
|
+
summary: Generate SVG word clouds from text or weighted word lists
|
|
50
|
+
test_files: []
|