N
N
nevatas2014-06-24 20:02:57
Delphi
nevatas, 2014-06-24 20:02:57

A program for calculating the relative frequency of occurrence of a word in a text. How should she look?

Hello, I want to say right away that you don’t need to write anything for me, I just want a consultation. Dali write a program:
The relative frequency of occurrence of each word in the text is calculated, the boundaries of words are symbols: dot, comma, colon, semicolon, space, brackets, dash, question and exclamation marks.
The text is entered from the keyboard, it should be possible to write it to a file; output of results to the screen, and at the request of the user to the printer or to a file.
How do you see it, and how do you think the result of the program execution should look like?
The program must be in Delphi.
I imagine the program like this:
There is a text field (Memo), text is inserted into it, a button is pressed - calculate.
The result will look like this:

1. Я - 2 раза.
2. и - 3 раза.
3. машина - 1 раз.
... //И так все слова в тексте

As I understand it, it is better to implement using two dynamic arrays. One is string, the other is numeric. The program goes through the text, encounters a word, checks whether it is in the first array, if not, then adds it to the first array, and if it is, then to the element of the second array with the index of the element of the first array that contains the word add 1.
At the output, we get two array of the same size, in the first word, and in the second array (numeric) with the same index - how many times in the text they met.
Or am I thinking too hard?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Z
zed, 2014-06-25
@zedxxx

If we abstract from the programming language and the language of the text, then here is an example: Frequency analyzer of English words, written in... . Look at the code, you might get some ideas for yourself.

V
Vit, 2014-06-24
@fornit1917

You are generally thinking correctly, but instead of ordinary arrays, it is better to use a data structure like HashMap. This is similar to an array, but the key may not be a number, but a string. Those. the key is the word, the value is the frequency. Then getting and changing the frequency word by word will be faster than bypassing a regular array.
I don't know if Delphi has its own map, but in my opinion, TStringList can be adapted for this case: www.delphibasics.ru/TStringList.php
You can look for some other options, but HashMap is basically what you need. Good luck.

L
luis, 2014-07-03
@luis

No array needed :)
Make an object

MyRec = object
  MyWord:string;
  MyFreq:integer;
end;

And add it to the TList collection or create your own collection based on generics

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question