Skip to content

Instantly share code, notes, and snippets.

@tafsiri
Created August 19, 2012 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tafsiri/3391176 to your computer and use it in GitHub Desktop.
Save tafsiri/3391176 to your computer and use it in GitHub Desktop.
func main() {
startTime := time.Now()
//Load our name databases in a goroutine
namesChannel := make(chan map[string]bool)
go func() {
fmt.Println("Start loading name database")
//send the result back over then channel
namesChannel <- loadNames("male.names")
namesChannel <- loadNames("female.names")
fmt.Println("Loaded Names")
}()
//Fetch and extract the people on the frontpage in another goroutine
extractChannel := make(chan []string)
go func() {
fmt.Println("Fetch pinterest page")
pageContent, _ := getFrontPage()
//Extract the names of the people pinning and those liking the pins
pinners := getPinners(pageContent)
likers := getPinCommenters(pageContent)
//Put the first names of all into one list.
//Go doesn't seem to have a builtin list concat function.
firstNames := []string{}
for i := range pinners {
firstNames = append(firstNames, strings.Split(pinners[i], " ")[0])
}
for i := range likers {
firstNames = append(firstNames, strings.Split(likers[i], " ")[0])
}
extractChannel <- firstNames
fmt.Println("Extract names from front page")
}()
//Wait for our goroutines to complete and then compute the results
var male map[string]bool = <-namesChannel
var female map[string]bool = <-namesChannel
firstNames := <-extractChannel // can still use type inference
fmt.Println("Building Stats")
//Test the names with our primitive gender guesser
//We will store the names for each match (or lack thereof) in a map of lists
results := map[string][]string{
"Male": []string{},
"Female": []string{},
"Unisex": []string{},
"Undetermined": []string{},
}
//Test if a name is male, female, both or unknown
for _, name := range firstNames {
isMale := false
isFemale := false
name = strings.ToUpper(name)
if male[name] {
isMale = true
}
if female[name] {
isFemale = true
}
switch {
case isMale && isFemale:
results["Unisex"] = append(results["Unisex"], name)
case isMale:
results["Male"] = append(results["Male"], name)
case isFemale:
results["Female"] = append(results["Female"], name)
case true:
results["Undetermined"] = append(results["Undetermined"], name)
}
}
//Print out results
endTime := time.Now()
fmt.Println("Results at", time.Now(), "Computed in", endTime.Sub(startTime))
fmt.Println(len(results["Female"]), " Women")
fmt.Println(len(results["Male"]), " Men")
fmt.Println(len(results["Unisex"]), " Unisex names:", results["Unisex"])
fmt.Println(len(results["Undetermined"]), " Undetermined:", results["Undetermined"])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment