[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:

Uploaded Image: tut1.jpg

Some of the files You can see there:
For more information about that see [U8] Application startup

2) We will start with the Smalltalk part
Uploaded Image: tut2.jpg
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
Uploaded Image: tut3.jpg

build.js
load("../s8/boot.js");
	load("../s8/s8.image.js");
	load("../s8/initialize.js");
	load("../s8/initialize.st.js");
	smalltalk.launchImage("../s8/s8.image.js");

	emmit('../tutorial/WordCounterToolApp.st');

	  smalltalk.$imageFileName = "../tutorial/console.snapshot.js";
	" Snapshot outputToFile: Smalltalk current imageFileName ".doIt();


_build.sh
          ../s8vm --expose-gc --shell build.js

Run _build.sh

4) Executing our tool
       ../s8vm --expose-gc --shell console.snapshot.js

Uploaded Image: tut4.jpg

$ ./_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