RosettaCodeData/Task/Word-frequency/PureBasic/word-frequency.basic

56 lines
1.3 KiB
Plaintext

EnableExplicit
Structure wordcount
wkey$
count.i
EndStructure
Define token.c, word$, idx.i, start.i, arg$
NewMap wordmap.i()
NewList wordlist.wordcount()
If OpenConsole("")
arg$ = ProgramParameter(0)
If arg$ = "" : End 1 : EndIf
start = ElapsedMilliseconds()
If ReadFile(0, arg$, #PB_Ascii)
While Not Eof(0)
token = ReadCharacter(0, #PB_Ascii)
Select token
Case 'A' To 'Z', 'a' To 'z'
word$ + LCase(Chr(token))
Default
If word$
wordmap(word$) + 1
word$ = ""
EndIf
EndSelect
Wend
CloseFile(0)
ForEach wordmap()
AddElement(wordlist())
wordlist()\wkey$ = MapKey(wordmap())
wordlist()\count = wordmap()
Next
SortStructuredList(wordlist(), #PB_Sort_Descending, OffsetOf(wordcount\count), TypeOf(wordcount\count))
PrintN("Elapsed milliseconds: " + Str(ElapsedMilliseconds() - start))
PrintN("File: " + GetFilePart(arg$))
PrintN(~"Rank\tCount\t\t Word")
If FirstElement(wordlist())
For idx = 1 To 10
Print(RSet(Str(idx), 2))
Print(~"\t")
Print(wordlist()\wkey$)
Print(~"\t\t")
PrintN(RSet(Str(wordlist()\count), 6))
If NextElement(wordlist()) = 0
Break
EndIf
Next
EndIf
EndIf
Input()
EndIf
End