patterngoMinor
Split and word count in go
Viewed 0 times
andcountsplitword
Problem
Could it be written better?
package main
import (
"code.google.com/p/go-tour/wc"
"fmt"
)
func WordCount(s string) map[string]int {
dict := make(map[string]int)
splited := Split(s)
for _, string := range splited {
_, present := dict[string]
if present {
dict[string]++
} else {
dict[string] = 1
}
}
return dict
}
func Split(s string) []string{
arraySize := 1
for i := 0; i < len(s); i++ {
if s[i] == ' ' {
arraySize++
}
}
array := make([]string, arraySize)
currentStrInd := 0
currentStr := ""
for i := 0; i < len(s); i++ {
if s[i] == ' ' {
array[currentStrInd] = currentStr
currentStrInd++
currentStr = ""
} else {
currentStr += string(s[i])
}
}
array[arraySize - 1] = currentStr
return array;
}
func main() {
fmt.Println(Split("I am learning Go!"))
wc.Test(WordCount)
}Solution
For simply counting word occurrencies, yes:
Supposing, splitting by \w+ gives in most cases, what you want.
Another solution would be
Go play with it!
package main
import (
"fmt"
"regexp"
)
func get_words_from(text string) []string{
words:= regexp.MustCompile("\\w+")
return words.FindAllString(text, -1)
}
func count_words (words []string) map[string]int{
word_counts := make(map[string]int)
for _, word :=range words{
word_counts[word]++
}
return word_counts;
}
func console_out (word_counts map[string]int){
for word, word_count :=range word_counts{
fmt.Printf("%v %v\n",word, word_count)
}
}
func main() {
text := "I am learning Go! Go is a nice language to learn."
console_out(count_words(get_words_from(text)))
}Supposing, splitting by \w+ gives in most cases, what you want.
Another solution would be
(\\b[^\\s]+\\b). Depends on your demands.Go play with it!
Code Snippets
package main
import (
"fmt"
"regexp"
)
func get_words_from(text string) []string{
words:= regexp.MustCompile("\\w+")
return words.FindAllString(text, -1)
}
func count_words (words []string) map[string]int{
word_counts := make(map[string]int)
for _, word :=range words{
word_counts[word]++
}
return word_counts;
}
func console_out (word_counts map[string]int){
for word, word_count :=range word_counts{
fmt.Printf("%v %v\n",word, word_count)
}
}
func main() {
text := "I am learning Go! Go is a nice language to learn."
console_out(count_words(get_words_from(text)))
}Context
StackExchange Code Review Q#27813, answer score: 2
Revisions (0)
No revisions yet.