Advertisement
TizzyT

VBES2 Analysis -TizzyT

Jun 19th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.03 KB | None | 0 0
  1. Imports System.Text
  2. Module Module1
  3.  
  4.     Private Letters As String = "*abcdefghijklmnopqrstuvwxyz"
  5.  
  6.  
  7.     Sub Main()
  8.         Dim meh As New LetterAnalysis("C:\Users\TizzyT\Desktop\VBES 2 Analysis\words_alpha.txt")
  9.         Dim sb As New StringBuilder
  10.         If meh.AnalyzeSecond Then
  11.             While meh.Analyzing
  12.             End While
  13.  
  14.             sb.AppendLine("Analysis by TizzyT:")
  15.             sb.AppendLine(meh.WordsCount & " words")
  16.             sb.AppendLine(meh.LettersCount & " letters")
  17.             sb.AppendLine()
  18.             sb.AppendLine("=============================")
  19.             For Each l As Char In Letters
  20.                 Dim Pop As Double = 0
  21.  
  22.                 Dim ordr As New List(Of LetterStat)
  23.  
  24.                 For Each ll As Char In Letters
  25.                     Dim c As Integer
  26.                     c = meh.SecondFrequency(l, ll)
  27.  
  28.                     ordr.Add(New LetterStat(ll, c))
  29.  
  30.                     Pop += c
  31.                 Next
  32.  
  33.                 sb.AppendLine()
  34.  
  35.                 ordr.Sort()
  36.                 ordr.Reverse()
  37.  
  38.                 For Each e As LetterStat In ordr
  39.                     sb.AppendLine((" " & l & " | " & e.Ch & vbTab & e.Value.ToString.PadLeft(6, " ") & vbTab & "%" & ((e.Value / (Pop - 1)) * 100).ToString.PadRight(12, "0"c).Substring(0, 12)).Replace("*", " "))
  40.                 Next
  41.  
  42.                 sb.AppendLine()
  43.                 sb.AppendLine(("     Score :: " & Pop).Replace("*", " "))
  44.                 sb.AppendLine()
  45.                 sb.AppendLine("=============================")
  46.             Next
  47.             IO.File.WriteAllText("C:\Users\TizzyT\Desktop\VBES 2 Analysis\Frequency.txt", sb.ToString)
  48.             Console.WriteLine("Done")
  49.         Else
  50.             Console.WriteLine("There was a problem in the analysis")
  51.         End If
  52.     End Sub
  53.  
  54.     Public Structure LetterStat
  55.         Implements IComparable
  56.  
  57.         Public ReadOnly Value As Integer
  58.         Public ReadOnly Ch As Char
  59.  
  60.         Public Sub New(ByVal Character As Char, ByVal Value As Integer)
  61.             Ch = Character
  62.             Me.Value = Value
  63.         End Sub
  64.  
  65.         Public Function CompareTo(obj As Object) As Integer Implements IComparable.CompareTo
  66.             Return Value.CompareTo(CType(obj, LetterStat).Value)
  67.         End Function
  68.  
  69.         Public Shared Widening Operator CType(ByVal N As LetterStat) As Integer
  70.             Return N.Value
  71.         End Operator
  72.     End Structure
  73.  
  74.     Public Class LetterAnalysis
  75.         Private FrequencyCounter As New Dictionary(Of Char, Dictionary(Of Char, Integer))
  76.  
  77.         Public ReadOnly Property SecondFrequency(ByVal LetterA As Char, ByVal LetterB As Char) As Integer
  78.             Get
  79.                 If FrequencyCounter.ContainsKey(LetterA) Then
  80.                     If FrequencyCounter(LetterA).ContainsKey(LetterB) Then
  81.                         Return FrequencyCounter(LetterA)(LetterB)
  82.                     End If
  83.                 End If
  84.                 Return 0
  85.             End Get
  86.         End Property
  87.  
  88.         Private AnalysisThread As Threading.Thread
  89.  
  90.         Private Words() As String
  91.         Public ReadOnly Property WordsCount() As Integer
  92.             Get
  93.                 Return Words.Length
  94.             End Get
  95.         End Property
  96.  
  97.         Private _LettersCount As Integer = 0
  98.         Public ReadOnly Property LettersCount() As Integer
  99.             Get
  100.                 Return _LettersCount
  101.             End Get
  102.         End Property
  103.  
  104.         Public Function AnalyzeSecond() As Boolean
  105.             Try
  106.                 AnalysisThread = New Threading.Thread(AddressOf _AnalyzeSecond)
  107.                 AnalysisThread.Start()
  108.                 Return True
  109.             Catch ex As Exception
  110.                 Return False
  111.             End Try
  112.         End Function
  113.         Private Sub _AnalyzeSecond()
  114.             _ProcessedLetters = 0
  115.             _ProcessedWords = 0
  116.             FrequencyCounter.Clear()
  117.             For Each w As String In Words
  118.                 If w.Length > 0 Then
  119.                     Dim pl As Char = "*"c
  120.                     For Each l As Char In w
  121.                         If Not FrequencyCounter.ContainsKey(pl) Then FrequencyCounter.Add(pl, New Dictionary(Of Char, Integer))
  122.                         If FrequencyCounter(pl).ContainsKey(l) Then
  123.                             FrequencyCounter(pl)(l) += 1
  124.                         Else
  125.                             FrequencyCounter(pl).Add(l, 1)
  126.                         End If
  127.                         pl = l
  128.                         _ProcessedLetters += 1
  129.                     Next
  130.                     _ProcessedWords += 1
  131.                 End If
  132.             Next
  133.         End Sub
  134.  
  135.         Private _ProcessedLetters As Integer = 0
  136.         Public ReadOnly Property ProcessedLetters() As Integer
  137.             Get
  138.                 Return _ProcessedLetters
  139.             End Get
  140.         End Property
  141.         Private _ProcessedWords As Integer = 0
  142.         Public ReadOnly Property ProcessedWords() As Integer
  143.             Get
  144.                 Return _ProcessedWords
  145.             End Get
  146.         End Property
  147.  
  148.         Private _Analyzing As Boolean = False
  149.         Public ReadOnly Property Analyzing As Boolean
  150.             Get
  151.                 SyncLock AnalysisThread
  152.                     If AnalysisThread.ThreadState = Threading.ThreadState.Running Then Return True
  153.                 End SyncLock
  154.                 Return False
  155.             End Get
  156.         End Property
  157.  
  158.         Private Sub New()
  159.         End Sub
  160.  
  161.         Public Sub New(ByVal Path As String)
  162.             Words = IO.File.ReadAllLines(Path)
  163.             Dim PreviousWord As String = String.Empty
  164.             For i = 0 To Words.Length - 1
  165.                 Dim _w As String = Words(i).Trim.ToLower
  166.                 If _w.Equals(PreviousWord) Then
  167.                     Words(i) = String.Empty
  168.                 Else
  169.                     _LettersCount += _w.Length
  170.                     PreviousWord = _w
  171.                 End If
  172.             Next
  173.             Console.WriteLine("Word List Loaded")
  174.         End Sub
  175.     End Class
  176.  
  177. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement