DEV Community

Ashomondi
Ashomondi

Posted on

GO-RELOADED PROJECT (article)

Go-reloaded is a small text editing and auto-correct tool written in Go.
The program reads text files from an input file, applies a set of formatting and correction rules, then writes the corrected version into an output file e.g : sample.txt -this is where the inputs are written

result.txt - this where the the corrected version in the sample.txt file is being displayed

What this project does and what is learnt in it

  1. Reading and writing files in Go using os.ReadFile This opens the file, reads the content in it then returns it as a slice of bytes
  2. Working with strings
  3. Converting numbers (binary and hexadecimal)
  4. Applying text transformation like uppercase, lowercase and capitalize
  5. Fixing punctuation and grammar rules automatically This helps to place every punctuation in order the way it should like quotes after the .

HOW THE PROGRAM WORKS
The program receives two command line:

  1. The input file name (sample.txt)
  2. The output file name (result.txt) How to run it use: go run . sample.txt result.txt Easy to run and also implement

STEPS THE PROGRAM FOLLOWS:

  1. Check if the user provided exactly two command lines
  2. Read the input file content using os.ReadFile
  3. Apply multiple text transformation one after another
  4. Write the final corrected text to the output file/ result using os.WriteFile
  5. Prints "Done" if everything worked successfully.

INPUT AND OUTPUT FILES
-Sample.txt (or input.txt) contains the raw text with special tags like (up), (cap), (low, 8), (hex), (bin)
-Result.txt (output.txt) contains the corrected text after the program finishes processing
This makes it easy to run.

THE TEXT TRANSFORMATION RULES
The project supports multiple corrections and formation rules
a) Uppercase version
Whenever the program find (up) it changes the word before it to uppercase
sample.txt
go (up)
result.txt
GO
b) Lowercase version
Whenever the program finds (low) it changes the word before to lowercase(small letter)
sample.txt
GO (low)
result.txt
go
c) Capitalize version
The programs runs and when it finds (cap) anywhere in the sentence it changes the word before to cap
sample.txt
go (cap)
result.txt
Go
Only the first letter of the word will change.
This program is being implemented through
func Tocap(text string) string
func Tolower(text string) string
func Toupper(text string) string
all of the 3 are done using the above function

Uppercase, Lowercase and Capitalize for N words.
The program also supports tags like

  1. up 2
  2. low 4
  3. cap 3

This means : apply the function to the previous N word.
input
my life (cap, 2) is good.
output
My Life is good.
The program removes the word in the bracket after converting correctly.

My life is very good (up, 2)
output
My life is VERY GOOD

MY LIFE IS (low, 3) good.
my life is good.
The difference between up and cap is that up capitalize the whole word while cap only the first letter of the word.

You implemented using this function:
TolowerN
ToupperN
TocapN

These functions:

  1. Detect the keyword like (up)
  2. Read the number in the next word like (3)
  3. Loop backwards and update the previous N words
  4. Remove the formatting tags from the final output

Hexadecimal conversion (hex)
When (hex) appears, the word before it is treated as a hexadecimal number and converted to decimal.

Example:
1E (hex)
output
30

It is being handled by this function:
func Hexadecimal(text string) string
strconv.ParseInt(word, 16, 64)
uses a string convert

Binary conversion (bin)
When (bin) appears, the word before it is treated as a binary and it is converted to decimal.

Example:
10(bin)
output
2
This is the function used:
func Binary(text string) string
strconv.ParseInt(word, 2, 64)

FIXING PUNCTUATION
One important part of the project is puntuation formatting.
The rules include:
a) punctuation like . , ! ? : ; must be attached to the previous word
b) there should be a space after punctuation (when needed)
c) special groups like ... or !? must stay together
d) quotes ' ' must not contain extra spaces inside

This is implemented using this function
func Punctuation(text string) string
This function uses many Strings.ReplaceAll() calls to remove wrong space and fix punctuation.
It is easy to implement and understand.

FIXING "a" to "an"
The final grammar rule is :

  1. Replace "a" with "an" when the next word starts with a) a vowel (a,e,i,o,u) b) or the letter h Example: a untold story output an untold story

Used this function:
func FixAtoAn(text string) string

WHY I USED string.Fields()
In most cases i used :
word := strings.Fields(text)

This is important because:

  1. splits the text into words
  2. removes extra spaces automatically
  3. makes it easier to scan word by word and detect tags like (up) After converting the strings it joins it using strings.Join(words, " ")

PROGRAM FLOW(pipeline style)
In main.go i applied the function pipeline:
text = Toupper(text)
text = Tolower(text)
text = Tocap(text)
text = Hexadecimal(text)
text = Binary(text)
text = TolowerN(text)
text = ToupperN(text)
text = TitleN(text)
text = Punctuation(text)
text = FixAtoAn(text)
This makes the program easy to read because every function focuses on one task

What i learnt from this project:
This project helped me learn how to:

  1. Work with command-line arguments using "os.Args"
  2. Convert strings into numbers strconv 3.Manipulate words using loops and string functions
  3. Build a program using a step by step transformation approach.

CONCLUSION
Go-reloaded is a simple but powerful text formatting tool that automatically edits text using special tags like (up), (hex) and also correct punctuation and grammar.
The project improved my understanding of:

  1. File handling in Go
  2. string processing
  3. building algorithms for real text problems It also taught me how to write code that is readable by separating each rule into its own function

CHALLENGES FACED DURING THIS PROJECT
The challenges i faced was time management, submitting the project on time was quite difficult.
Writing the whole code for the first time was challenging i had to redo it over and over again to get it right with the proper out put.

Got a question or suggestion? Drop a comment, i would love to hear your thoughts.

Top comments (0)