func main() { classifier := c.Classifier() names := []string{ "hank", "mark", "hannah", "rachael", "edward", "norah", "henry", "charlie", "ben", "claire", "matt", "lauren", "marsha", } for i, _ := range names { name := names[i] gender, probability := c.Classify(classifier, name) fmt.Println(name, gender, probability) } }
func predictGenderStats(names []string) (f, m float64) { classifier := c.Classifier() var numFemale int var femaleNames []string var numMale int var maleNames []string for _, name := range names { gender, _ := c.Classify(classifier, name) if gender == string(c.Girl) { numFemale += 1 femaleNames = append(femaleNames, name) } if gender == string(c.Boy) { numMale += 1 maleNames = append(maleNames, name) } } printNames(maleNames, femaleNames) numTotal := len(names) f = percent(numFemale, numTotal) m = percent(numMale, numTotal) return }
func main() { classifier := c.Classifier() year := 2000 filename := fmt.Sprintf("names/yob%d.txt", year) file, _ := os.Open(filename) defer file.Close() reader := csv.NewReader(file) wrong := 0.0 rows := 0.0 for { record, err := reader.Read() if err == io.EOF { break } name := strings.ToLower(record[0]) gender := c.Gender(record[1]) guess, _ := c.Classify(classifier, name) if guess != string(gender) { // fmt.Println(name, gender, guess) wrong++ } rows++ } // Calculate the accuracy of the classifier // based a sample of the Census data. accuracy := 1. - wrong/rows fmt.Println("Rows:", rows) fmt.Println("Wrong:", wrong) fmt.Println("Accuracy:", accuracy) }
func main() { file, _ := os.Open("startup_weekend/sw.csv") defer file.Close() classifier := c.Classifier() reader := csv.NewReader(file) reader.TrailingComma = true i := 0 genders := make(map[string]map[string]map[string]int) // fmt.Println(genders) genders["Organizer"] = genderMap() genders["Mentor"] = genderMap() genders["Speaker"] = genderMap() genders["Judge"] = genderMap() for { i++ record, err := reader.Read() if err == io.EOF { break } else if err != nil { // fmt.Println(err) } role := record[0] // fmt.Println(record) year := record[2] if year == "Date" { continue } name := strings.Split(record[1], " ")[0] name = strings.Split(record[1], "-")[0] gender, _ := c.Classify(classifier, name) // fmt.Println(name, gender) if genders[role] == nil { fmt.Println("setup", year) } else { genders[role][year][gender]++ } } // fmt.Println(genders) for role, years := range genders { fmt.Println() fmt.Println(role) for year, genderCounts := range years { fmt.Println(year) total := float64(genderCounts["Male"] + genderCounts["Female"] + genderCounts["N/A"]) fmt.Println("Total:", total) fmt.Println("N/A:", genderCounts["N/A"]) females := float64(genderCounts["Female"]) diff := females / total fmt.Println("Female:", females, "(", diff*100, "%)") fmt.Println("Male:", genderCounts["Male"]) } } }
func main() { file, _ := os.Open("attendees/attendees.csv") defer file.Close() classifier := c.Classifier() reader := csv.NewReader(file) reader.TrailingComma = true i := 0 genders := map[string]map[string]int{} years := []string{ "2009", "2010", "2011", "2012", "2013", "2014", } for _, year := range years { genders[year] = map[string]int{ "Female": 0, "Male": 0, "N/A": 0, } } for { i++ record, err := reader.Read() if err == io.EOF { break } else if err != nil { fmt.Println("ERROR: ", err) } if len(record) == 0 { continue } name := strings.Split(record[0], " ")[0] year := strings.Split(record[1], "-")[0] gender, _ := c.Classify(classifier, name) genders[year][gender]++ } for year, genderCounts := range genders { fmt.Println(year) total := float64(genderCounts["Male"] + genderCounts["Female"]) fmt.Println("Total:", total) fmt.Println("N/A:", genderCounts["N/A"]) females := float64(genderCounts["Female"]) diff := females / total fmt.Println("Female:", females, "(", diff*100, "%)") fmt.Println("Male:", genderCounts["Male"]) fmt.Println() } }
package main import ( "encoding/json" "fmt" c "github.com/hstove/gender/classifier" "log" "net/http" "os" "strconv" ) var classifier = c.Classifier() func classify(res http.ResponseWriter, req *http.Request) { name := req.URL.Path[len("/classify/"):] gender, prob := c.Classify(classifier, name) prob = prob * 100 probability := strconv.FormatFloat(prob, 'f', 6, 64) jsonMap := map[string]string{ "name": name, "gender": gender, "probability": probability, } json, _ := json.Marshal(jsonMap) fmt.Println(jsonMap) res.Header().Set("Content-Type", "application/json; charset=utf-8") res.Write(json) } func main() {