Tools: GO-RELOADED PROJECT (article)

Tools: GO-RELOADED PROJECT (article)

Source: Dev.to

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 HOW THE PROGRAM WORKS
The program receives two command line: STEPS THE PROGRAM FOLLOWS: 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 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 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 : 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: 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: 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: 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. Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse - 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
- Working with strings
- Converting numbers (binary and hexadecimal)
- Applying text transformation like uppercase, lowercase and capitalize
- Fixing punctuation and grammar rules automatically
This helps to place every punctuation in order the way it should like quotes after the . - The input file name (sample.txt)
- The output file name (result.txt)
How to run it use: go run . sample.txt result.txt
Easy to run and also implement - Check if the user provided exactly two command lines
- Read the input file content using os.ReadFile
- Apply multiple text transformation one after another
- Write the final corrected text to the output file/ result using os.WriteFile
- Prints "Done" if everything worked successfully. - Detect the keyword like (up)
- Read the number in the next word like (3)
- Loop backwards and update the previous N words
- Remove the formatting tags from the final output - 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 - splits the text into words
- removes extra spaces automatically
- makes it easier to scan word by word and detect tags like (up)
After converting the strings it joins it using strings.Join(words, " ") - Work with command-line arguments using "os.Args"
- Convert strings into numbers strconv
3.Manipulate words using loops and string functions
- Build a program using a step by step transformation approach. - File handling in Go
- string processing
- 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