[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 397
  • Last Modified:

Create short name from long name

I need to create an abbreviated 4 character prefix from a list of names. All abbrevations need to be unique.

For example, some of the names are almost identical:

  XYZ Childcare Centre
  XYZ Childcare Centre Pty Ltd
  XYZ Child Care Creche

  Toyota Car Dealer
  Toyota Motor Dealer
  Brisbane Toyota Motor Dealer

How do I go about creating unique 4 character prefixes from any name, similar to others, or not?

Thanks.

0
tvtech
Asked:
tvtech
  • 2
  • 2
  • 2
1 Solution
 
HooKooDooKuCommented:
How "human readable" do these "abbreviations" need to be?

One obvious solution is to simply assign random 4 digit names to each and generate a table that tells you what abbrev goes with which name.

There's what I would call the "stock ticker" approach, where the 1st name you encounter is given an obvious abbreviation, and as new ones come allong, make a little more cryptic abbreviation (but you still need a chart of what abrev goes to which name).

As an example:
XYZC = XYZ Childcare centre
XYZP = XYZ childcare centre Pty Ltd
XYCC = XYz Childcare Creche
TYCD = ToYota Car Dealer
TYMD = ToYota Motor Dealer
TMDB = Brisbane Toyota Motor Dealer

Are these going to be human assigned abreviations?  Or do you need a computer program to do it for you  (in which case, the Abrevs might be the 1st few characters and a unique number).
0
 
tvtechAuthor Commented:
As the names are saved to file, they need the human readable abbreviations to be assigned by the software.

Thanks.
0
 
GrahamSkanCommented:
That was a bit more complex than expected, but try this anyway
Sub CallCP()
    Dim MyArray(3) As String
    Dim i As Integer
    
    MyArray(0) = "Toyota Car Dealer"
    MyArray(1) = "Toyota Caravan Dealer"
    MyArray(2) = "Brisbane Toyota Motor Dealer"
    MyArray(3) = "Toyota Motor Dealer"
    CreatePrefix MyArray()
    
    For i = 0 To 3
        Debug.Print MyArray(i)
    Next i
End Sub
Sub CreatePrefix(Names() As String)

Dim strName As String
Dim strFirst As String
Dim strPrefix As String
Dim strWords() As String
Dim i As Integer
Dim j As Integer
Dim c As Integer
Dim bNew As Boolean
Dim bUnique As Boolean

For i = 0 To UBound(Names)
    strName = Names(i)
    strPrefix = GetPrefix(strName)
    If strPrefix = "" Then
        strWords = Split(strName, " ")
        For c = 0 To UBound(strWords)
            Select Case Mid$(strWords(c), 1, 1)
                Case "A" To "Z", "a" To "z"
                    strPrefix = strPrefix & UCase(Mid$(strWords(c), 1, 1))
            End Select
            If Len(strPrefix) = 4 Then
                Exit For
            End If
        Next c
        strPrefix = Left$(strPrefix & "AAAA", 4)
        bNew = True
    End If
    bUnique = False
    Do Until bUnique
        bUnique = True
        For j = 0 To UBound(Names)
            If j <> i Then
                If strPrefix = GetPrefix(Names(j)) Then
                    bUnique = False
                    Exit For
                End If
            End If
        Next j
        If Not bUnique Then
            strPrefix = IncrementPrefix(strPrefix)
        End If
    Loop
    If bNew Then
        strName = strPrefix & " " & strName
    Else
        strWords(0) = strPrefix
        strName = Join(strWords, " ")
    End If
    Names(i) = strName
Next i
End Sub
Function GetPrefix(strName As String) As String
    Dim strFirst As String
    Dim c As Integer

    strFirst = Split(strName, " ")(0)
    If Len(strFirst) = 4 Then
        'bPrefix = True
        For c = 1 To 3
            Select Case Mid$(strFirst, c, 1)
                Case "A" To "Z"
                Case Else
                    Exit Function
            End Select
        Next c
    Else
        Exit Function
    End If
    GetPrefix = strFirst
End Function

Function IncrementPrefix(strPrefix As String) As String
Dim i As Integer
For i = 4 To 1 Step -1
    If Mid$(strPrefix, i, 1) <> "Z" Then
        Mid$(strPrefix, i, 1) = Chr$(Asc(Mid$(strPrefix, i, 1)) + 1)
        Exit For
    End If
Next i
IncrementPrefix = strPrefix
End Function

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
HooKooDooKuCommented:
Human readable AND assigned by software...  that could be a very complex algorithum... potentally gets into the realm of Artificial Intelegence.  But since you're posting this question under the Visual Basic topic, I assume you're not looking to try to implement something a little simpler.

I'm afraid the simplest thing that I can think of is that when ever a new name is encountered, you prompt a user to key in a new abreviation.  Before accepting the new abreviation, test that it is unique and provide the user a way see a list of current abbreviations.

If you need something more automated, the next level of complexity I can think of is that when ever a new name is encountered, assign a unique number to it.  Periodically (hourly, daily, weekly, etc) you display a list of abreviations that are comprized entirely of numbers (0000 to 9999) and allow a human to override the numeric abreviation.  You then simply to a mass file rename (and update any data) to change the old numeric abreviations to human made/readable abreviations.

After that, you start getting into complex multistep AI algorithums.
0
 
tvtechAuthor Commented:
Perfect, Graham. Thanks very much! :)
0
 
GrahamSkanCommented:
I should have posted an explanation.

If the names is not prefixed with a four-character, uppercase word, a new one is created from the first letters of the existing title, padded with capital As if necessary.

The existing, or new prefix is then checked against any other existing prefixes. If it is the same, a character, starting with the rightmost is moved on to the next letter until it is a Z.

In adding this description, I realise that I should be resetting the previously 'overflowed' character from 'Z' to 'A'  when moving to the next character on the left. However with small datasets, this oversight is theoretical and unlikely to cause any problem.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now