giovedì 11 novembre 2010

Esercizio Text Mining di Fabrizi Claudia, Fabrizi Valentina, Tranquilli Valentina

Imports System.Drawing.Drawing2D

Public Class Form1

    Public b As Bitmap
    Public g As Graphics
    Public ParoleDaIgnorare As Dictionary(Of String, Boolean)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ParoleDaIgnorare = New Dictionary(Of String, Boolean)(StringComparer.InvariantCultureIgnoreCase)

        Me.ParoleDaIgnorare.Add(" I ", Nothing)
        Me.ParoleDaIgnorare.Add(" IL ", Nothing)
        Me.ParoleDaIgnorare.Add(" LA ", Nothing)
        Me.ParoleDaIgnorare.Add(" LE ", Nothing)
        Me.ParoleDaIgnorare.Add(" GLI ", Nothing)
        Me.ParoleDaIgnorare.Add(" IN ", Nothing)
        Me.ParoleDaIgnorare.Add(" CON ", Nothing)
        Me.ParoleDaIgnorare.Add(" SU ", Nothing)
        Me.ParoleDaIgnorare.Add(" PER ", Nothing)
        Me.ParoleDaIgnorare.Add(" TRA ", Nothing)
        Me.ParoleDaIgnorare.Add(" FRA ", Nothing)
        Me.ParoleDaIgnorare.Add(" UN ", Nothing)
        Me.ParoleDaIgnorare.Add(" UNO ", Nothing)
        Me.ParoleDaIgnorare.Add(" UNA ", Nothing)
        Me.ParoleDaIgnorare.Add(" O ", Nothing)
        Me.ParoleDaIgnorare.Add(" A ", Nothing)
        Me.ParoleDaIgnorare.Add(" DI ", Nothing)
        Me.ParoleDaIgnorare.Add(" CHE ", Nothing)
        Me.ParoleDaIgnorare.Add("PER ", Nothing)
        Me.ParoleDaIgnorare.Add(" SIA ", Nothing)
        Me.ParoleDaIgnorare.Add(" ALLE ", Nothing)
        Me.ParoleDaIgnorare.Add(" ALLA ", Nothing)
        Me.ParoleDaIgnorare.Add(" AL ", Nothing)

        Dim Testoo As String = Me.RichTextBox1.Text
        Dim Testo_Normalizzato As String = Testoo.ToUpper.Replace(",", " ").Replace(vbCrLf, " ").Replace("!", " ").Replace("  ", " ").Replace("(", " ").Replace(")", " ")

        For Each ParolaDaRimuovere As String In Me.ParoleDaIgnorare.Keys
            Testo_Normalizzato = Testo_Normalizzato.Replace(ParolaDaRimuovere, " ")
        Next ParolaDaRimuovere
        Testo_Normalizzato.Replace("  ", " ")

        Me.RichTextBox2.Text = Testo_Normalizzato

        Dim MinimaDimensioneFont As Integer = 10
        Dim MassimaDimensioneFont As Integer = 50
        Dim MinFrequenza = 1
        Dim MaxFrequenza = 0
        Dim temp = 0
        Dim RND As New Random
        Dim colorNames As String() = System.Enum.GetNames(GetType(KnownColor))
        Dim lstColors As New List(Of String)(colorNames)
        Dim Testo As String = Me.RichTextBox1.Text

        Testo.ToUpper()

        Dim Parole() As String = Testo_Normalizzato.Split((vbCrLf & " ").ToCharArray, StringSplitOptions.RemoveEmptyEntries)
        Dim ListaParoleDiverse As New SortedList(Of String, InfoParola)

        For Each parola As String In Parole
            If Not ListaParoleDiverse.ContainsKey(parola) Then
                Dim infoP As New InfoParola
                infoP.numeroOccorrenze = 1
                infoP.PAROLA = parola
                ListaParoleDiverse.Add(parola, infoP)
            Else
                ListaParoleDiverse.Item(parola).numeroOccorrenze += 1
                temp = ListaParoleDiverse.Item(parola).numeroOccorrenze
                If MaxFrequenza < temp Then MaxFrequenza = temp
            End If


        Next

        b = New Bitmap(Me.PictureBox1.Width, Me.PictureBox1.Height)
        g = Graphics.FromImage(b)
        g.SmoothingMode = SmoothingMode.HighQuality
        g.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias

        Dim Raggio As Double = b.Width / 3

        'Determiniamo la dimensione del font corrispondente alla frequenza

        For Each InfoParola As InfoParola In ListaParoleDiverse.Values

            With InfoParola

                Dim NumeroCasuale0_1 As Double = RND.NextDouble
                Dim PathRettangoli As New GraphicsPath

                'Rettangolo della stringa

                Dim IndiceCasuale As Integer = RND.Next(0, lstColors.Count - 1)
                .Colore = System.Drawing.Color.FromName(lstColors(IndiceCasuale))
                Dim ANgoloCasuale As Double = RND.NextDouble * 2 * Math.PI
                Dim DIstanzaCasuale As Double = RND.NextDouble * Raggio


                .x = b.Width / 2 + Math.Cos(ANgoloCasuale) * DIstanzaCasuale
                .y = b.Height / 2 + Math.Sin(ANgoloCasuale) * DIstanzaCasuale


                .DimensioneDelFontCorrispondenteAllaFrequenza = CSng(MinimaDimensioneFont + (.numeroOccorrenze - MinFrequenza) * (MassimaDimensioneFont - MinimaDimensioneFont) / (MaxFrequenza - MinFrequenza))
                Dim FontProporzionato As New Font("Arial", .DimensioneDelFontCorrispondenteAllaFrequenza, FontStyle.Regular, GraphicsUnit.Pixel)

                Dim Size As SizeF = g.MeasureString(.PAROLA, FontProporzionato, Point.Empty, StringFormat.GenericDefault)
                Dim LarghezzaParola = Size.Width
                Dim AltezzaParola = Size.Height

                .SIzeFString = g.MeasureString(.PAROLA, FontProporzionato)
                Dim RettangoloStringa As New Rectangle(New Point(.x, .y), .SIzeFString.ToSize)


                Dim R As New Region(PathRettangoli)
                If Not R.IsVisible(RettangoloStringa) Then
                    g.DrawString(.PAROLA, FontProporzionato, New SolidBrush(.Colore), .x, .y)
                    PathRettangoli.AddRectangle(RettangoloStringa)
                End If

            End With
        Next InfoParola


        Me.PictureBox1.Image = b
        Application.DoEvents()

        Me.PictureBox1.Image = b

    End Sub


End Class

Public Class InfoParola
    Public x As Double
    Public y As Double
    Public PAROLA As String
    Public numeroOccorrenze As Integer
    Public DimensioneDelFontCorrispondenteAllaFrequenza As Single
    Public Colore As Color
    Public SIzeFString As SizeF
End Class

Nessun commento:

Posta un commento

Nota. Solo i membri di questo blog possono postare un commento.