🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of damiankorz

damiankorz's solution

to Word Count in the CFML Track

Published at Dec 21 2018 · 0 comments
Instructions
Test suite
Solution

Given a phrase, count the occurrences of each word in that phrase.

For example for the input "olly olly in come free"

olly: 2
in: 1
come: 1
free: 1

To run the code in this exercise, you will only need to have CommandBox CLI installed. This binary runs CFML code from the command line.

To run the tests, cd into the exercise folder and run the following:

box task run TestRunner
# Or start up a test watcher that will rerun when files change
box task run TestRunner --:watcher

The tests leverage a library called TestBox which supports xUnit and BDD style of testing. All test suites will be written in the BDD style which uses closures to define test specs. You won't need to worry about installing TestBox. The CLI test runner will take care of that for you. You just need to be connected to the internet the first time you run it. You can read more about it here:

https://testbox.ortusbooks.com/content/

Source

This is a classic toy problem, but we were reminded of it by seeing it in the Go Tour.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

SolutionTest.cfc

component extends="WordCountTest" {

	function beforeAll(){
	  SUT = createObject( 'Solution' );
	}

}

WordCountTest.cfc

component extends="testbox.system.BaseSpec" {

	function beforeAll(){
	  SUT = createObject( 'WordCount' );
	}

	function run(){
	
		describe( "My WordCount class", function(){			

			it( 'count one word', function(){
				expect( SUT.countWords( sentence='word' ) ).toBe( {"word":1} );
			});

			it( 'count one of each word', function(){
				expect( SUT.countWords( sentence='one of each' ) ).toBe( {"one":1,"of":1,"each":1} );
			});

			it( 'multiple occurrences of a word', function(){
				expect( SUT.countWords( sentence='one fish two fish red fish blue fish' ) ).toBe( {"one":1,"fish":4,"two":1,"red":1,"blue":1} );
			});

			it( 'handles cramped lists', function(){
				expect( SUT.countWords( sentence='one, two, three' ) ).toBe( {"one":1,"two":1,"three":1} );
			});

			it( 'handles expanded lists', function(){
				expect( SUT.countWords( sentence='one, #chr( 10 )#two, #chr( 10 )#three' ) ).toBe( {"one":1,"two":1,"three":1} );
			});

			it( 'ignore punctuation', function(){
				expect( SUT.countWords( sentence='car: carpet as java: javascript!!&@$%^&' ) ).toBe( {"car":1,"carpet":1,"as":1,"java":1,"javascript":1} );
			});

			it( 'include numbers', function(){
				expect( SUT.countWords( sentence='testing,  1,  2 testing' ) ).toBe( {"testing":2,"1":1,"2":1} );
			});

			it( 'normalize case', function(){
				expect( SUT.countWords( sentence='go Go GO Stop stop' ) ).toBe( {"go":3,"stop":2} );
			});

			it( 'with apostrophes', function(){
				expect( SUT.countWords( sentence='First: don''t laugh. Then: don''t cry.' ) ).toBe( {"first":1,"don't":2,"laugh":1,"then":1,"cry":1} );
			});

			it( 'with quotations', function(){
				expect( SUT.countWords( sentence='Joe can''t tell between ''large'' and large.' ) ).toBe( {"joe":1,"can't":1,"tell":1,"between":1,"large":2,"and":1} );
			});

			it( 'multiple spaces not detected as a word', function(){
				expect( SUT.countWords( sentence=' multiple   whitespaces' ) ).toBe( {"multiple":1,"whitespaces":1} );
			});

		});
		
	}
 
}
component {
	/**
	* @returns
	*/
	public struct function countwords( required string sentence ) {
		wordStruct = {};
		sentence = reReplace(sentence, "[^0-9A-Za-z' ]", "", "all");
		sentence = sentence.split(" ");
		for( word in sentence ){
			if( word != "" ){
				var quote = "'";
				if(word[1] == quote && word[len(quote)] == quote){
					word = replace(word, quote, "", "all");
				}
				if( !structKeyExists( wordStruct, word ) ){
					wordStruct[word] = 1;
				}else{
					wordStruct[word] += 1;
				}
			}
		}
		return wordStruct;
	}
}

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?