[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.
Let's start
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:
- s8vm: S8 Virtual Machine.
- s8/s8.image.js: Basic S8 binary image file.
- s8/boot.js: Containing boot definitions of a S8 system structure.
For more information about that see [U8] Application startup
2) We will start with the Smalltalk part
- 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
The guy that will count the words will be an object of class WordCounter.
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" |
#countWordsInBookFolder |
"This method will be invoked when system starts up. So it will start up our application" |
#launch |
full source code of WordCounter class can be found here
3) Building the project
- Create a build folder
- Create a build.js file with JavaScript instructions for create the special image for our application.
- Create a Missing File (/U8/uploads/_build.sh) batch file with Shell (batch) instructions for executing build.js
build.js
- load, boot, initialize and launch basic S8 Smalltalk image
load("../s8/boot.js");
load("../s8/s8.image.js");
load("../s8/initialize.js");
load("../s8/initialize.st.js");
smalltalk.launchImage("../s8/s8.image.js");
- Load our Smalltalk application code
emmit('../tutorial/WordCounterToolApp.st');
- Create the special image for our app
smalltalk.$imageFileName = "../tutorial/console.snapshot.js";
" Snapshot outputToFile: Smalltalk current imageFileName ".doIt();
_build.sh
- Tells the VM to execute build.js
../s8vm --expose-gc --shell build.js
Run _build.sh
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:
$ ./_wordCounter.sh
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