4
\$\begingroup\$

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)
}
asked Jun 26, 2013 at 17:38
\$\endgroup\$
2
  • \$\begingroup\$ "for _, string := range splited {" Are you using "string" as a variable Name? \$\endgroup\$ Commented Jun 26, 2013 at 18:54
  • \$\begingroup\$ oh, my bad ;..( \$\endgroup\$ Commented Jun 27, 2013 at 9:25

2 Answers 2

2
\$\begingroup\$

For simply counting word occurrencies, yes:

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!

answered Oct 26, 2013 at 0:24
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Have you considered the same word with different cases: say "Book", "book", "BOOK" should be treated as one word. Maybe convert all words to lower case before getting their counts is a good idea. \$\endgroup\$ Commented Nov 20, 2015 at 14:32
  • \$\begingroup\$ Dependend on the result you want - this would be an improvement :] \$\endgroup\$ Commented Nov 20, 2015 at 16:44
  • 2
    \$\begingroup\$ Instead of regexp you can use strings.FieldsFunc, example play.golang.org/p/GrlgFyZAXB \$\endgroup\$ Commented Mar 15, 2016 at 22:56
1
\$\begingroup\$

For example,

package main
import (
 "code.google.com/p/go-tour/wc"
 "strings"
)
// WordCount returns a map of the counts of each "word" in the string s.
func WordCount(s string) map[string]int {
 words := strings.Fields(s)
 counts := make(map[string]int, len(words))
 for _, word := range words {
 counts[word]++
 }
 return counts
}
func main() {
 wc.Test(WordCount)
}
answered Jun 27, 2013 at 12:00
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Code-only answers do not constitute a review. Please explain how your changes improve the original code. \$\endgroup\$ Commented Jan 28, 2014 at 17:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.