Part V: Inverted index generation (optional, does not count in grade)
在前面几个部分,我们完成了一个单机分布式的map-reduce模型,part-5类似part-2的word-cnt
,这部分比较简单,在word-cnt
上稍加修改即可。
这里贴上源码
func mapF(document string, value string) (res []mapreduce.KeyValue) {
// Your code here (Part V).
spliter := func(c rune) bool{
return !unicode.IsLetter(c)
}
words:=strings.FieldsFunc(value,spliter)
keyValueList := make([]mapreduce.KeyValue,len(words))
for i:=0;i<len(words);i++{
keyValueList[i].Key = words[i]
keyValueList[i].Value = document
}
return keyValueList
}
// The reduce function is called once for each key generated by Map, with a
// list of that key's string value (merged across all inputs). The return value
// should be a single output value for that key.
func reduceF(key string, values []string) string {
// Your code here (Part V).
var reducedString []string
reducedString = append(reducedString, values[0])
for i:=1;i<len(values);i++{
if strings.Compare(values[i],values[i-1])!=0{
reducedString = append(reducedString,values[i])
}
}
s:= strings.Join(reducedString,",")
return strconv.Itoa(len(reducedString))+" "+s
}