Letter frequency: Difference between revisions
Content added Content deleted
Line 5,555: | Line 5,555: | ||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
Make it a bag of characters and get the counts: |
Make it a bag of characters and get the counts: |
||
{{works with|Smalltalk/X}} |
|||
<lang smalltalk>bagOfChars := 'someFile' asFilename contentsAsString asBag. |
<lang smalltalk>bagOfChars := 'someFile' asFilename contentsAsString asBag. |
||
bag sortedCounts |
bag sortedCounts |
||
select:[:assoc | assoc value isLetter ] |
select:[:assoc | assoc value isLetter ] |
||
thenDo:[:assoc | assoc printCR].</lang> |
thenDo:[:assoc | assoc printCR].</lang> |
||
If the file is huge, you may not want to read it in as a big string first, but directly feed the chars into the bag: |
|||
<lang smalltalk>bagOfChars := Bag new. |
|||
'someFile' asFilename readingLinesDo:[:eachLine | bagOfChars addAll:eachLine]. |
|||
bag sortedCounts ...</lang> |
|||
To show all counts, replace the "select:thenDo:" by a simple "do:", as in: |
To show all counts, replace the "select:thenDo:" by a simple "do:", as in: |
||
<lang smalltalk>bag sortedCounts do:[:assoc | assoc printCR].</lang> |
<lang smalltalk>bag sortedCounts do:[:assoc | assoc printCR].</lang> |