This project takes a file with mime type of text/plain, counts the words in the file, and displays some statistics that include the total number of words and the count of each of the words found.
Word Count is divided into a front end Javascript React single page application and a backend Scala Play Framework service.
The backend consists of two controllers and a model:
- FrontEndController was contributed by the seed project and it serves the React applicatio.
- WordCountController wraps the WordCount algorithm and handles marshalling and unmarshalling of inputs and outputs. It has a single method that processes an HTTP POST containing a multipart mime with a single FilePart. This FilePart is expected to be of type text/plain. The controller calls into the WordCount object and then marshalls the output into JSON which it sends back to the caller.
- WordCount contains the core algorithm. The function count() takes a String, and returns both the total number of words and the count of each word. This algorithm has a worst case time complexity of O(n log n) and a worst case space complexity of O(n). The algorithm is designed to count the words in a 10MB string in under 2 seconds.
There are two interesting tests:
- WordCountControllerSpec which tests WordCountController
- WordCountSpec which test which tests WordCount
The frontend consists of normal React plumbing plus the interesting parts which contain the core of the display. The core components are WordCount and WordCountResults.
- WordCount contains a Card for displaying the UI for uploading a file. The problem set requests a separate step for uploading and then processing but I felt the UX was improved by combining those two steps into a single action for the user. WordCount also contains a state machine that shows and hides the WordCountResult component and the busy indicator.
- WordCountResults has no state and it simply turns props passed in from WordCount into a table to display the results.
This project can be completely controlled by the Simple Build Tool (sbt) but it has a few other dependencies that need to be installed first.
- JDK - It is suggested that you use either Oracle's or OpenJDK and it requires at least version 8 of either one of those.
- sbt - The most modern version should be used
- npm - The most modern version should be used
Once these dependencies are installed you can run the entire project through sbt. The two most interesting commands are:
- Use
sbt run
to run both the backend and frontend. When this is executed it will open your browser automatically to the WordCount page. If that process fails for some reason you can see the app at http://localhost:3000 - Use
sbt test
to run both the backend and frontend tests
To look at the seed project used see: https://github.com/yohangz/scala-play-react-seed
This software is licensed under the MIT license