Need an app that counts and displays letter frequency of a user-input string

I don't want to use an extension or dictionary, which I know means creating a list (or lists) that has 26 items to keep track of counts. I also know that I need to use the text split/at block and loop through the string. Has anybody already created something like this that I can use as a model? It would be greatly appreciated.

(Before you ask, no it is not a homework assignment. I'm actually a math teacher trying to limp through teaching an Intro to Comp Sci class, our current unit is Cryptography and this app would work well with the Frequency Analysis lesson.)

Do you want if the input text is apple then

A-1
P-2
L-1
E-1 like?

For banana
B-1
A-3
N-2

Here is a grouped_sum procedure that can sum in groups without an AI2 dictionary.

The Ai2 dictionary type would have made this easier.

If you split an input text at the empty string, you will get a list of letters.

Are you avoiding the AI2 dictionary data structure, or are you avoiding lists of words?

The Ai2 dictionary type is a natural for this type of problem.

This is precisely what I want

I want to stick to lists because it's an Intro class and that's a concept the students are familiar with. Considering this is a tool to use within the lesson I don't want to struggle with more complex structures if they can be avoided.

Dictionaries make this so much simpler, A shame.


letter_frequency.aia (2.6 KB)

Most of the trouble in this sample is in wrestling with the AI2 chart component.

Without extension, dictionary, anyother global list

When Button1.Click:
  set inputText to TextBox1.Text
  initialize empty list uniqueChars
  initialize empty list counts

  for each character in (split inputText into characters):
    if character not in uniqueChars:
      add character to uniqueChars
      add 1 to counts
    else:
      index = index of character in uniqueChars
      counts[index] = counts[index] + 1

  initialize empty list result
  for each index in uniqueChars:
    create string "character - count"
    add to result

  set ListView1.Elements to result

above blocks are draggable (pls add caps in the code there by all letters into cap)

2 Likes

Thanks, I'll file this away to maybe use with a more advanced class when we cover cryptography

Thank you!!!

I threw in an AI2 pie chart, for what it's worth:
image


letter_frequency.aia (3.0 KB)

1 Like

Here's an all-lists approach for you, with a CSV (Comma Separated Values) table thrown in.
image


letter_frequency_lists_only.aia (3.8 KB)

The algorithm is:

Split the letters
Sort the letters
Walk the list from the end to the front, at each step gobbling up the predecessor if it matches.
(Going in descending index order is necessary because the indexes collapse at each removal.)
This leaves you with a list of letter sequences (aaa,b,nn) one per letter, each holding all the instances of that letter.
The graph and the summary are fed from the letter and the matching sequence length.

1 Like