这部分很简单,前面我们是调用的代码已经写好的mapF函数和reduceF,所以这一部分就相当于要我们自己实现自定义的map和reduce函数,map函数用来生成键值对序列,reduce函数用来统计词频。
-
map函数
func mapF(filename string, contents string) []mapreduce.KeyValue { // Your code here (Part II). words := strings.FieldsFunc(contents, func(r rune) bool { return !unicode.IsLetter(r) }) res := []mapreduce.KeyValue{} for _, w := range words { res = append(res, mapreduce.KeyValue{w, ""}) } return res }
-
reduce函数
return strconv.Itoa(len(values))