Homework10_A

Implement your own algorithm to compute a frequency distribution of the words from any text (possibly judiciously scraped from websites) and draw some personal graphical representation of the “word cloud”

Code VB.NET

https://drive.google.com/file/d/1nHYV1BiQIPjvwI08zaqnm7j_e2BoSfGO/view?usp=sharing

Connect to web page

'avoid pop-up
WebBrowser1.ScriptErrorsSuppressed = True

        If String.IsNullOrEmpty(url1) Then Return
        If url1.Equals("about:blank") Then Return
        If Not url1.StartsWith("http://") And
        Not url1.StartsWith("https://") Then
            url1 = "https://" & url1
        End If

'in web browser window upload the page in url
        Try
            WebBrowser1.Navigate(New Uri(url1))
        Catch ex As System.UriFormatException
            Return
        End Try

Copy all the words

WebBrowser1.Document.ExecCommand("SelectAll", False, Nothing)
        WebBrowser1.Document.ExecCommand("Copy", False, Nothing)
        WebBrowser1.Document.ExecCommand("Unselect", False, Type.Missing)
        RTXBCloud.Text = Clipboard.GetText
        Clipboard.Clear()

Build rettangle

 'orderDistrW is in decrescending order
        For Each kvp In orderDistrW
            Dim rect As RectangleF
            Dim f As New Font("arial", (kvp.Value * 200) / orderDistrW.First.Value)
            Dim s As SizeF = g.MeasureString(kvp.Key, f)


         
            Dim tries As Integer
            Do
                'build a rectangle with size of the word and save them for check if the position in the picturbeox is free with 'occupied' function
                'points x and y of rectangle are taken to not go out the picturebox
                Dim x = viewport.Left + ((viewport.Right + 1 - s.Width) - viewport.Left) * R.NextDouble()
                Dim y = viewport.Top + ((viewport.Bottom + 1 - s.Height) - viewport.Top) * R.NextDouble()

              
                rect = New RectangleF(New PointF(x, y), s)
                If Not Occupied(rect, listOfRect) Then Exit Do
                tries += 1
                If tries >= 100000 Then Continue For
            Loop

            g.DrawString(kvp.Key, f, New SolidBrush(Color.FromArgb(R.Next(256), R.Next(256), R.Next(256))), New Point(rect.X, rect.Y))
            listOfRect.Add(rect)

        Next

Lascia un commento