Пример #1
0
        protected BaseLayout(WordCloudInput wordCloud)
        {
            var size = new SizeD(wordCloud.Width, wordCloud.Height);

            Surface   = new RectangleD(new PointD(0, 0), size);
            QuadTree  = new QuadTree <LayoutItem>(Surface);
            Center    = new PointD(Surface.X + size.Width / 2, Surface.Y + size.Height / 2);
            WordCloud = wordCloud;
        }
Пример #2
0
        static int Main(string[] args)
        {
            var command = new RootCommand
            {
                new Argument <FileInfo>("output",
                                        () => new FileInfo(Path.Join(Environment.CurrentDirectory, "output.png")),
                                        "Path to the output file, default is `output.png`.")
            };

            command.Description = "Takes words on input and generates word cloud as PNG from them.";

            command.Handler = CommandHandler.Create <FileInfo>(output =>
            {
                // Process words on input.
                var freqs       = new Dictionary <string, int>();
                var whitespaces = new Regex(@"\s+");
                while (Console.ReadLine() is string line)
                {
                    foreach (var word in whitespaces.Split(line))
                    {
                        if (!freqs.TryGetValue(word, out var freq))
                        {
                            freq = 0;
                        }
                        freqs[word] = freq + 1;
                    }
                }

                // Generate topic cloud.
                var wordCloud = new WordCloudInput(
                    freqs.Select(p => new WordCloudEntry(p.Key, p.Value)))
                {
                    Width       = 1024,
                    Height      = 256,
                    MinFontSize = 8,
                    MaxFontSize = 32
                };
                var sizer        = new LogSizer(wordCloud);
                using var engine = new SkGraphicEngine(sizer, wordCloud);
                var layout       = new SpiralLayout(wordCloud);
                var wcg          = new WordCloudGenerator <SKBitmap>(wordCloud, engine, layout);

                // Draw the bitmap on white background.
                using var final  = new SKBitmap(wordCloud.Width, wordCloud.Height);
                using var canvas = new SKCanvas(final);
                canvas.Clear(SKColors.White);
                canvas.DrawBitmap(wcg.Draw(), 0, 0);

                // Save to PNG.
                using var data   = final.Encode(SKEncodedImageFormat.Png, 100);
                using var writer = output.Create();
                data.SaveTo(writer);
            });

            return(command.Invoke(args));
        }
Пример #3
0
        public LogSizer(WordCloudInput wordCloud)
        {
            fontDelta   = wordCloud.MaxFontSize - wordCloud.MinFontSize;
            minFontSize = wordCloud.MinFontSize;

            var wordCounts = wordCloud.Entries.Select(e => e.Count).DefaultIfEmpty(0);
            var minCount   = wordCounts.Min();
            var maxCount   = wordCounts.Max();

            minLog  = Math.Log(minCount);
            divisor = Math.Log(maxCount) - minLog;
        }
Пример #4
0
 public SkGraphicEngine(ISizer sizer, WordCloudInput wordCloud,
                        SKTypeface?font = null)
 {
     Sizer     = sizer;
     Bitmap    = new SKBitmap(wordCloud.Width, wordCloud.Height);
     canvas    = new SKCanvas(Bitmap);
     textPaint = new SKPaint
     {
         Color    = SKColor.Parse(wordCloud.TextColor),
         Typeface = font
     };
     this.wordCloud = wordCloud;
 }
Пример #5
0
 public SpiralLayout(WordCloudInput wordCloud) : base(wordCloud)
 {
 }