2.0 KiB
File Analyser
Brief introduction
This command line program simply take a plain text file path as input and will process it, then work out the words appeared with frequencies, finally it will print out the top 10 words with hightest frequencies.
About the design
I used a map[string]int to store all the unique words and their frequencies when the program read the data from the file.
Then I transformed the data type from map to internal dataRow and sorted the data by frequency and alphabetic order. By doing so, we can do more compicated operations against the data.
Finally, called the Output method to revert and transform the internal data structure to final output format.
I also created a custom func type HandleLine, so we can have different functions to process data with different rules. Tried to make the program less coupled.
By the way, The reason why I add docker version is we don't need to worry about whether we have correct Go version anymore, also reduce the manual steps like installing dependencies or compiling the program especially for bigger projects.
How to run
Running with Go
go run cmd/main.go ./README.md
# You can also specify how many lines you would like to print out
go run cmd/main.go ./README.md 15
# Against some other files
go run cmd/main.go ./test_files/valid_input.txt
Docker
Build the docker image
docker build -t redhat-analyser .
Run against docker image
docker run --rm -it -v $(pwd):/home/myapp/myfiles redhat-analyser ./analyser myfiles/README.md
If you want to parse other files please replace the directory you'd like to mount from $(pwd) to some other directory.
Then update the input file myfiles/README.md to some other values.
Run the tests
go test ./... -cover
Something I would like to improve if had more time
- Better Logging rather than just use
fmt.Println - Handle different errors with different actions, currently I just print out error message and exit the program