[View] [Edit] [Lock] [References] [Attachments] [History] [Home] [Changes] [Search] [Help]
Word counter tutorial
This tutorial illustrates the use of console mode console mode to create command line tools & applications.
Inspired in sites like http://www.wordcounter.net and http://wordcounttools.com we will construct a Word Counter tool.
Writers, journalists, bloggers or students. usually have some requirements about minimum and maximum amount of words for an article, essay or paper... so this tool can help them.
1) Download the console contribution for the platform you use
Follow the instructions You'll find in the contribution and You'll have a folder structure like this one:
Some of the files You can see there:
For more information about that see [U8] Application startup
- s8vm: S8 Virtual Machine.
- s8/s8.image.js: Basic S8 binary image file.
- s8/boot.js: Containing boot definitions of a S8 system structure.
2) We will start with the Smalltalk part
The guy that will count the words will be an object of class WordCounter.
- We can also split our code in more than one file, but in this case our app is really small so it's ok to write all the code in just one file
This class will implement some useful methods:
|"return the number of words inside the given string: aString"|
| #countWords: aString |
|"return the number of words inside the file named aFileName or nil if the file does not exists"|
| #countWordsInFile: aFileName|
|"return the sum of words inside all the chapters of a book." |
|#countWordsInFolder: aBookFolderPathName withChapterPrefix: aChapterPrefix|
|"return the sum of words inside all the chapters of a book, using the default configured folder"|
|"This method will be invoked when system starts up. So it will start up our application"|
full source code of WordCounter class can be found here
3) Building the project
- Create a build folder
- Create a Missing File (/U8/uploads/_build.sh) batch file with Shell (batch) instructions for executing build.js
- load, boot, initialize and launch basic S8 Smalltalk image
- Load our Smalltalk application code
- Create the special image for our app
smalltalk.$imageFileName = "../tutorial/console.snapshot.js";
" Snapshot outputToFile: Smalltalk current imageFileName ".doIt();
- Tells the VM to execute build.js
../s8vm --expose-gc --shell build.js
4) Executing our tool
- After building our project There is a new image file console.snapshot.js in the tutorial folder.
- We have to create a new command line shell script that starts the VM with our new image.
../s8vm --expose-gc --shell console.snapshot.js
- Create a sub folder with the chapters (text files) to be analyzed with our tool
- Execute _wordCounter.sh script from console Terminal
- It will produce a result like this one:
Alice in Wonderland/chapter-1.txt has 2165 words and 11353 characters
Alice in Wonderland/chapter-2.txt has 2099 words and 10991 characters
Alice in Wonderland/chapter-3.txt has 1702 words and 9554 characters
The entire book has 5966 words
s8vm (V8 3.30.22)
build and tutorial folders are ready to download here:tutorial.zip