| Gentee Programming Language > Documentation > Tutorial | Download documentation |
Exercise 1
Calculate the frequency of a word in a file specified by a user. In order to make the task easy, let us assume that a word consisits of letters of English alphabet only and its length does not exceed more than 32 symbols.
To begin with, write down the file2words function, which will read the file, search out words and increment the entry counter in the associative array. The associative array (hash table) will be used for storing and searching words that have been already found. The hash table will be passed to the function by means of this parameter in order to be reused and to process the results.
func uint file2words( str filename, hash words )
{
...
data.read( filename )
print( "Read \( filename )...
Size = \( *data )
Processing...\n" )We check file data symbol by symbol and define the word length. First, we store the current position in the len and calculate the length of the word.
while i < *data
{
len = i
while ( data[i] >= 'A' && data[i] <= 'Z' ) ||
( data[i] >= 'a' && data[i] <= 'z' )
{
i++
}
len = i - lenIf a word length is less than 32 symbols, but more than zero, we assign this word to the stemp variable. Then we increment the counter value of the obtained word in the hash array. If this word has not been included in the result list, it will be added automatically. Furthermore, we increment the counter of total amount of processed words.
if len && len < 32
{
stemp.copy( data.ptr() + i - len, len )
words[ stemp ]++
count++
}
i++
}
return countLet us use the results function for data processing and data outputting. We insert all obtained words from the hash array into the string array awords that will be sorted out then.
func results( hash words, uint count )
{
...
foreach cur, words.keys
{
awords += cur
}
awords.sortstr()We output the words and their entry frequency into the temporary string. We store the string into the words.txt file and open it in order to be browsed.
foreach acur, awords
{
result += "\(acur): \( words[\(acur)])\l"
}
result += "==================================
Total words: \( count )
Different words: \( *awords )\l"
result.write("words.txt")
shell("words.txt")The name of the file being processed will be obtained from the command line with help of the main function or a user will be requested to input it. Then the file should be processed and the result - be outputted.
uint count
hash words
str filename
if argc() : argv( filename, 1 )
while !fileexist( filename )
{
if *filename : @"File \(filename) does not exist!\n"
congetstr( "Specify the filename to calculate words: ", filename )
}
count = file2words( filename, words )
results( words, count )Exercise 2
Calculate the frequency of words found in all .g files in the current directory.
Copyright © 2004-2006 Gentee Inc. All rights reserved. |