Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Example of using tables
#1
Again, it's not the type of program NaaLaa has been designed for, but it's one from my long list of simple programs I test every single language I try with. Some the members here may remember the word count challenge we had on basicprogramming.org. N6 was pretty useless at this task, but N7 is a completely different beast. I was impressed by how easy (thanx to tables, the split function and the free val procedure) was to write this program, considering the fact that NaaLaa doesn't have sorting algorithms built-in.

The task is to read a text file, remove all non-word characters, count all words, count unique words, count how many times each word appears in the text, sort the words by occurrence an save the result to the file.  And here is the code:

Code:
open file 1, "Hamlet.txt"

whole_text = ""
x = freadc(1)
while x
    if x < 48 or (x > 57 and x < 65) or (x > 90 and x < 97) or x > 122
        whole_text = whole_text + chr(32)
    elseif x > 64 and x < 91 then
        whole_text = whole_text + chr(x + 32)
    else
        whole_text = whole_text + chr(x)
    endif
    x = freadc(1)
wend

all_words = split(whole_text, " ")

words_number = 0
unique_words = []
foreach n in all_words
    if len(n) > 1 or n = "a" or n ="i"
        unique_words[n] = unique_words[n] + 1
        words_number = words_number + 1
    endif
next

create file 2, "Words.txt"
wln file 2, "All words - " + words_number
wln file 2, "Unique words - " + sizeof(unique_words)
wln file 2, ""

y = 1
while sizeof(unique_words)
    foreach a, b in unique_words
        if b = y then
            wln file 2, a + " - " + b
        endif
    next    
    free val unique_words, y
    y = y + 1
wend

OK. And now let's move to graphics and games. I won't bore you with this kind of programs, anymore. Wink


Attached Files
.zip   words.zip (Size: 72.74 KB / Downloads: 5)
Reply
#2
There are 32.052 words in the "Hamlet.txt"
Part 1.Processing text file in                73.766 seconds
Part 2.Writing on text file (Words.txt)   0.019 seconds
Reply
#3
All words - 32052
Part 1: 33.598
Part 2: 0.012
Logic is the beginning of wisdom.
Reply
#4
And this version?

Code:
unwanted_characters = []
counter = 0

for a = 0 to 96
    unwanted_characters[counter] = chr(a)
    counter = counter + 1
next
for a = 123 to 127
    unwanted_characters[counter] = chr(a)
    counter = counter + 1
next

open file 1, ("Hamlet.txt")
pln "Reading the file..."
x = fread(1)
while x
    whole_text = whole_text + " " + x
    x = fread(1)
wend
free file 1

whole_text = lower(whole_text)

foreach y in unwanted_characters
    whole_text = replace(whole_text, y, " ")
next

all_words = split(whole_text, " ")

pln "Counting..."
words_number = 0
unique_words = []
foreach n in all_words
    if len(n) > 1 or n = "a" or n = "i"
        unique_words[n] = unique_words[n] + 1
        words_number = words_number + 1
    endif
next

pln "Saving the result..."
create file 2, "Words.txt"
wln file 2, "All words - " + words_number
wln file 2, "Unique words - " + sizeof(unique_words)
wln file 2, ""

y = 1
while sizeof(unique_words)
    foreach a, b in unique_words
        if b = y then
            wln file 2, a + " - " + b
        endif
    next    
    free val unique_words, y
    y = y + 1
wend

free file 2

Should be around 3-4 times faster. What's interesting is that the old version takes around 30 sec. on my laptop. How is it possible? My laptop is pretty old and slow, so how come it is faster than your machines??? The new one takes 8 sec., btw.
Reply
#5
The second version 
-------------------------------------------
Reading the file in 15.81 seconds
Counting in            0.033 seconds
Saving the result in 0.034 seconds
--------------------------------------------
It's faster than the first version  Big Grin

Tested on :
- Intel Core i5-6200U CPU @2.30GHz
- RAM 4.00 GB
- Windows 7 64 bit
Reply
#6
So much quicker.... Cool... Thank Tomaaz!

Read file in : 7.262 secs.
Counted in : 0.023 secs.
Saved file in : 0.018 secs.

AMD Ryzen 5 5600G @ 3.9Ghz
RAM 16 GB
64 bit Linux Mint 21.2
Logic is the beginning of wisdom.
Reply
#7
johnno56, 1micha.elok could you test the sped of this version?

Code:
unwanted_characters = []
counter = 0

for a = 0 to 96
    unwanted_characters[counter] = chr(a)
    counter = counter + 1
next
for a = 123 to 127
    unwanted_characters[counter] = chr(a)
    counter = counter + 1
next

pln "Reading and processing the file..."
whole_text = lower(system("type Hamlet.txt"))

foreach y in unwanted_characters
    whole_text = replace(whole_text, y, " ")
next

all_words = split(whole_text, " ")

pln "Counting..."
words_number = 0
unique_words = []
foreach n in all_words
    if len(n) > 1 or n = "a" or n = "i"
        unique_words[n] = unique_words[n] + 1
        words_number = words_number + 1
    endif
next

pln "Saving the result..."
create file 2, "Words.txt"
wln file 2, "All words - " + words_number
wln file 2, "Unique words - " + sizeof(unique_words)
wln file 2, ""

y = 1
while sizeof(unique_words)
    foreach a, b in unique_words
        if b = y then
            wln file 2, a + " - " + b
        endif
    next    
    free val unique_words, y
    y = y + 1
wend

free file 2

Thanx!
Reply
#8
Wow! What are you feeding that program of yours?

All words - 32051
Unique words - 4595

Reading: 0.175 secs.
Counting: 0.022 secs.
Writing: 0.017 secs.
Logic is the beginning of wisdom.
Reply
#9
Well, some could say I'm cheating a little bit in the last example, but I would happily disagree. Wink The main difference is that the file is read by the system function, but the whole text processing, counting, sorting and saving is done by NaaLaa. It would be rather easy to add a function that can read entire text file to NaaLaa and the result would be more or less the same. The only reason NaaLaa doesn't have a function like that is the fact that it doesn't really need it. Wink Anyway, less than one second? That's impressive. Smile
Reply
#10
It's a quantum leap  Cool
---------------------------------------------------
Processing / Reading the file in
1st  version 73.766 seconds
2nd version 15.81 seconds
3rd  version 0.58 seconds
----------------------------------------------------
Tested on :
- Intel Core i5-6200U CPU @2.30GHz
- RAM 4.00 GB
- Windows 7 64 bit
----------------------------------------------------
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)