func findMoeEntriesForWords(words []string, charSet chinese.CharSet) ([]moedict.Entry, error) { moeEntries := make([]moedict.Entry, 0) // TODO explain diff. between trad and simp if charSet == chinese.Trad { for _, word := range words { entry, err := moedict.FindEntry(word) if err != nil { return nil, err } if entry != nil { moeEntries = append(moeEntries, *entry) } } } // if the sentences is in simplified characters, find cedict records first // (there may be multiple records per word) and find moedict records using // the traditional word of the cedict records. if charSet == chinese.Simp { for _, word := range words { records, err := cedict.FindRecords(word, chinese.Simp) if err != nil { return nil, err } individualRecords := make([]cedict.Record, 0) for _, r := range records { recordInSlice := false for _, ir := range individualRecords { if r.Trad == ir.Trad { recordInSlice = true } } if !recordInSlice { individualRecords = append(individualRecords, r) } } for _, record := range individualRecords { entry, err := moedict.FindEntry(record.Trad) if err != nil { return nil, err } if entry != nil { moeEntries = append(moeEntries, *entry) } } } } return moeEntries, nil }
func moeVocabLookupHandler(writer http.ResponseWriter, request *http.Request) { word := getLastPathComponent(request) colors := getColors(request) fmt.Println("moeVocabLookupHandler: " + word) // convert to trad tradWord, err := cedict.Simp2Trad(word) if err != nil { fmt.Fprint(writer, `{"error": "`+err.Error()+`", "word": "`+word+`"}`) return } moeEntry, err := moedict.FindEntry(tradWord) if moeEntry == nil { fmt.Fprint(writer, `{"error": "No matches found", "word": "`+word+`"}`) return } if err != nil { fmt.Fprint(writer, `{"error": "`+err.Error()+`", "word": "`+word+`"}`) return } // construct csv row var output string output += tradWord output += "\t" output += moeEntry.ToHTML(colors) // use json.Marshal with an anonymous variable to escape the \t and " characters // in the response j, err := json.Marshal(map[string]interface{}{ "error": "nil", "csv": output, }) if err != nil { fmt.Fprint(writer, `{"error": "`+err.Error()+`", "word": "`+word+`"}`) return } fmt.Fprint(writer, string(j)) }