I am trying to alphabetize by generating a weight of a word, saving those numbers, and then comparing which numbers are greater. Don't ask me why I need to do this way.

So I am trying to come up with a function that calculates the value of a certain word. I am using the asc function to get the ascii value, but I am not good enough at math to figure out how to calculate this.

Here is what I have. It's written in VB, but I really don't care what language, I just want to understand the algorithm.

Dim byt As Integer
Dim acc As Integer

If txt = "" Then
    lbl = "0"
Else
    For i = 1 To Len(txt)
        byt = Asc(Mid(txt, i, 1))
        acc = acc + (byt / i)
    Next i
   
    lbl = Str(acc)
End If

It is in VBA.

Dim byt As Integer  <-- declare variable
Dim acc As Integer  <-- declare variable

If txt = "" Then
    lbl = "0"
Else
    For i = 1 To Len(txt)  <-- go through the loop from 1 .. text length
        byt = Asc(Mid(txt, i, 1))  <-- get the ASCII value of the selected character position "i"
        acc = acc + (byt / i)  <-- accumulate the value of each ASCII divided by its position in text
    Next i
   
    lbl = Str(acc)  <-- convert the value to string
End If

It looks like you made no changes to the code. You just added comments. You realize that the function is not working, and that's why I need help changing or completely rewriting. I just put it here to make it clearer what I was trying to do. It does not work.

Having a scoring function on words, means having heuristic. Need to know what the problem is? Do you want more occurring words to having higher weight? Do you want longer length words to have higher weights? Do you want words that have vowels to have higher weights? You see you objective function depends on your objective. So what is your objective?

Yes, here I tested concept in Python, the weight does not put short words which start in alphabet later after the long ones with previous letters:

>>> def weight(word):
	return sum(ord(c)/float(ind1) for ind1, c in enumerate(word,1))

>>> sorted(('abc', 'aa', 'aac', 'acde','bed'), key = weight)
['aa', 'aac', 'abc', 'bed', 'acde']
>>>

Oh, I'm sorry. I misunderstood what you mean. Do you want it to be similar to pyTony or you want the value to be lower for lower ascii regardless its length?

For example, the same input array from pyTony will result the value below if you want the sum value to ignore the length.

abc(107.78999999999999) aa(106.7) aac(107.69) acde(108.001) bed(109.1)

However, it has limitation of string length due to the limitation of floating points. I use the char position ASCII value and divide it with 10*position of its own position in the string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.