Skip to content

Instantly share code, notes, and snippets.

@lkrych
Created August 11, 2018 18:51
Show Gist options
  • Save lkrych/b862b326575a06b34bc50e528d56377d to your computer and use it in GitHub Desktop.
Save lkrych/b862b326575a06b34bc50e528d56377d to your computer and use it in GitHub Desktop.
How to build a frequency table with a Symbol Table
func createFreqTable(arr []string) SymbolTable {
st := &SymbolTable{
data: &BSTree{},
}
r := regexp.MustCompile("[ .,123456789]")
//iterate through words
for _, word := range arr {
if r.Match([]byte(word)) {
//skip punctuation, spaces or numbers
continue
}
//see if word can be found
found := st.get(word)
if found != nil {
//if so, increment the count
st.put(word, found.val+1)
} else {
//if not, add it to the symbol table
st.put(word, 1)
}
}
return *st
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment