Word frequency: Difference between revisions

no edit summary
m (Fixed a typo in C output)
No edit summary
Line 2,870:
9 7922 that
10 6659 it
</pre>
 
=={{header|PureBasic}}==
<lang PureBasic>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</lang>
{{out}}
<pre>
Elapsed milliseconds: 462
File: 135-0.txt
Rank Count Word
1 the 41093
2 of 19954
3 and 14943
4 a 14558
5 to 13953
6 in 11219
7 he 9649
8 was 8622
9 that 7924
10 it 6661
</pre>
 
164

edits