Word frequency: Difference between revisions

no edit summary
No edit summary
Line 2,357:
had 6133
</pre>
 
=={{header|FutureBasic}}==
<lang futurebasic>
include "NSLog.incl"
include "Tlbx CFCharacterSet.incl"
 
local fn WordFrequency( textStr as CFStringRef, caseSensitive as Boolean, ascendingOrder as Boolean ) as CFStringRef
'~'1
CFStringRef wrd, resultStr = NULL
CFDictionaryRef dict
 
if caseSensitive == NO then textStr = fn StringLowercaseString( textStr )
CFStringRef tempStr = fn ArrayComponentsJoinedByString( fn StringComponentsSeparatedByCharactersInSet( textStr, fn CharacterSetInvertedSet( fn CharacterSetLetterSet ) ), @" " )
CFMutableCharacterSetRef separators = fn CFCharacterSetCreateMutable( 0 )
MutableCharacterSetFormUnionWithCharacterSet( separators, fn CharacterSetPuntuationSet )
MutableCharacterSetFormUnionWithCharacterSet( separators, fn CharacterSetWhitespaceAndNewlineSet )
CFArrayRef tempArr = fn StringComponentsSeparatedByCharactersInSet( tempStr, separators )
CFRelease( separators )
CountedSetRef freqencies = fn CountedSetWithArray( tempArr )
EnumeratorRef enumRef = fn CountedSetObjectEnumerator( freqencies )
CFArrayRef array = fn EnumeratorAllObjects( enumRef )
CFMutableArrayRef wordArr = fn MutableArrayWithCapacity( 0 )
 
NSInteger totalWords = 0
for wrd in array
totalWords++
dict = @{ @"count":fn NumberWithUnsignedInteger( fn CountedSetCountForObject( freqencies, wrd ) ), @"object":wrd }
if ( fn StringLength( wrd ) != 0 )
MutableArrayAddObject( wordArr, dict )
end if
next
 
AppSetProperty( @"totalWords", fn StringWithFormat( @"%d", totalWords ) )
SortDescriptorRef descriptors = fn SortDescriptorWithKey( @"count", ascendingOrder )
CFArrayRef sortedArray = fn ArraySortedArrayUsingDescriptors( wordArr, @[descriptors] )
CFMutableStringRef mutStr = fn MutableStringWithCapacity( 0 )
 
NSInteger count = 1
for dict in sortedArray
MutableStringAppendString( mutStr, fn StringWithFormat( @"%-7d %-7lu %@\n", count, fn StringIntegerValue( fn DictionaryValueForKey( dict, @"count" ) ), fn DictionaryValueForKey( dict, @"object" ) ) )
count++
next
 
resultStr = fn StringWithFormat( @"%@", mutStr )
end fn = resultStr
 
CFAbsoluteTime startTime
CFURLRef textURL
CFStringRef textStr, frequencyStr
 
textURL = fn URLWithString( @"https://www.gutenberg.org/files/135/135-0.txt" )
textStr = fn StringWithContentsOfURL( textURL, NSUTF8StringEncoding, NULL )
 
startTime = fn CFAbsoluteTimeGetCurrent
frequencyStr = fn WordFrequency( textStr, NO, NO )
NSLog( @"%@", frequencyStr )
NSLog( @"Total words in document: %@", fn AppProperty( @"totalWords" ) )
NSLog( @"Elapsed time: %f milliseconds.", ( fn CFAbsoluteTimeGetCurrent - startTime ) * 1000.0 )
 
HandleEvents
</lang>
{{Out}}
<pre>
1 41095 the
2 19955 of
3 14939 and
4 14546 a
5 13954 to
6 11218 in
7 9649 he
8 8622 was
9 7924 that
10 6661 it
11 6470 his
12 6193 is
 
//-------------------
 
22900 1 millstones
22901 1 fumbles
22902 1 shunned
22903 1 avoids
22904 1 poitevin
22905 1 muleteer
22906 1 idolizes
22907 1 lapsed
22908 1 reptitalmus
22909 1 bled
22910 1 isabella
 
Total words in document: 22911
Elapsed time: 595.407963 milliseconds.
</pre>
 
 
 
 
 
=={{header|Go}}==
723

edits