Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,709 questions

55,473 answers

573 users

How to perform high‑performance reversible text compression using a word dictionary in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic
Imports System.Text

'
' =====================================================================
' High‑Performance Reversible Text Compression Using a Word Dictionary
' ---------------------------------------------------------------------
' This program compresses text by replacing repeated words with tokens
' like @0, @1, @2... and stores each unique word in a dictionary.
'
' The compressed text is fully reversible.
'
' WHY THIS VERSION IS FAST (VB.NET):
' ---------------------------------
' • Uses Dictionary(Of String, Integer) for O(1) average lookup.
' • Uses List(Of String) for compact dictionary storage.
' • Uses StringBuilder for efficient string construction.
' • Manual scanning avoids regex overhead.
' • Clean, idiomatic, modern VB.NET design.
'
' OUTPUT EXAMPLE:
'     Original:      this is is a test test compression string string test
'     Compressed:    @0 @1 @1 @2 @3 @3 @4 @5 @5 @3
'     Decompressed:  this is is a test test compression string string test
' =====================================================================
'

Module WordDictionaryCompression

    ' -----------------------------------------------------------------
    ' Dictionary structure: List + Dictionary
    ' -----------------------------------------------------------------
    Public Class WordDictionary
        Public Words As New List(Of String)()                ' index → word
        Public IndexMap As New Dictionary(Of String, Integer)() ' word → index
    End Class


    ' -----------------------------------------------------------------
    ' Find or add a word to the dictionary (O(1) average)
    ' -----------------------------------------------------------------
    Function FindOrAdd(dict As WordDictionary, word As String) As Integer
        Dim existing As Integer

        If dict.IndexMap.TryGetValue(word, existing) Then
            Return existing
        End If

        Dim newIndex As Integer = dict.Words.Count
        dict.Words.Add(word)
        dict.IndexMap(word) = newIndex

        Return newIndex
    End Function


    ' -----------------------------------------------------------------
    ' Compress text into @ID tokens
    ' -----------------------------------------------------------------
    Function Compress(input As String, dict As WordDictionary) As String
        Dim out As New StringBuilder(input.Length * 2)
        Dim i As Integer = 0

        While i < input.Length

            Dim c As Char = input(i)

            ' Pass punctuation/spaces directly
            If Not Char.IsLetterOrDigit(c) Then
                out.Append(c)
                i += 1
                Continue While
            End If

            ' Extract word
            Dim start As Integer = i
            While i < input.Length AndAlso Char.IsLetterOrDigit(input(i))
                i += 1
            End While

            Dim word As String = input.Substring(start, i - start)

            ' Get dictionary index
            Dim id As Integer = FindOrAdd(dict, word)

            ' Write token
            out.Append("@"c)
            out.Append(id)
        End While

        Return out.ToString()
    End Function


    ' -----------------------------------------------------------------
    ' Decompress @ID tokens back into original text
    ' -----------------------------------------------------------------
    Function Decompress(compressed As String, dict As WordDictionary) As String
        Dim out As New StringBuilder(compressed.Length * 2)
        Dim i As Integer = 0

        While i < compressed.Length

            Dim c As Char = compressed(i)

            ' Token?
            If c = "@"c Then
                i += 1
                Dim id As Integer = 0

                ' Parse digits
                While i < compressed.Length AndAlso Char.IsDigit(compressed(i))
                    id = id * 10 + (Convert.ToInt32(compressed(i)) - Convert.ToInt32("0"c))
                    i += 1
                End While

                If id >= 0 AndAlso id < dict.Words.Count Then
                    out.Append(dict.Words(id))
                End If

            Else
                ' Pass punctuation/spaces
                out.Append(c)
                i += 1
            End If

        End While

        Return out.ToString()
    End Function


    ' -----------------------------------------------------------------
    ' Main
    ' -----------------------------------------------------------------
    Sub Main()
        Dim original As String =
            "this is is a test test compression string string test " &
            "this is a test compression"

        Dim dict As New WordDictionary()

        Dim compressed As String = Compress(original, dict)
        Dim decompressed As String = Decompress(compressed, dict)

        Console.WriteLine("Original:      """ & original & """")
        Console.WriteLine("Compressed:    """ & compressed & """")
			Console.WriteLine("Decompressed:  """ & decompressed & """" & Environment.NewLine)

        Console.WriteLine("Dictionary:")
        For i As Integer = 0 To dict.Words.Count - 1
            Console.WriteLine("  @" & i & " => " & dict.Words(i))
        Next
    End Sub

End Module


'
' run:
'
' Original:      "this is is a test test compression string string test this is a test compression"
' Compressed:    "@0 @1 @1 @2 @3 @3 @4 @5 @5 @3 @0 @1 @2 @3 @4"
' Decompressed:  "this is is a test test compression string string test this is a test compression"
'
' Dictionary:
'   @0 => this
'   @1 => is
'   @2 => a
'   @3 => test
'   @4 => compression
'   @5 => string
'

 



answered Jul 31 by avibootz

Related questions

...