\$\begingroup\$
\$\endgroup\$
2
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)
}
-
\$\begingroup\$ "for _, string := range splited {" Are you using "string" as a variable Name? \$\endgroup\$AlucardTheRipper– AlucardTheRipper2013年06月26日 18:54:47 +00:00Commented Jun 26, 2013 at 18:54
-
\$\begingroup\$ oh, my bad ;..( \$\endgroup\$Sławosz– Sławosz2013年06月27日 09:25:55 +00:00Commented Jun 27, 2013 at 9:25
2 Answers 2
\$\begingroup\$
\$\endgroup\$
3
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.
answered Oct 26, 2013 at 0:24
-
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\$Nick– Nick2015年11月20日 14:32:22 +00:00Commented Nov 20, 2015 at 14:32
-
\$\begingroup\$ Dependend on the result you want - this would be an improvement :] \$\endgroup\$Thomas Junk– Thomas Junk2015年11月20日 16:44:57 +00:00Commented Nov 20, 2015 at 16:44
-
2\$\begingroup\$ Instead of regexp you can use strings.FieldsFunc, example play.golang.org/p/GrlgFyZAXB \$\endgroup\$ahsankhan– ahsankhan2016年03月15日 22:56:19 +00:00Commented Mar 15, 2016 at 22:56
\$\begingroup\$
\$\endgroup\$
1
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
-
1\$\begingroup\$ Code-only answers do not constitute a review. Please explain how your changes improve the original code. \$\endgroup\$200_success– 200_success2014年01月28日 17:25:42 +00:00Commented Jan 28, 2014 at 17:25
lang-golang