aboutsummaryrefslogtreecommitdiff
path: root/InvertedIndex.java
blob: b2e1fd005ee70ecfed43bc6e0a29bb8880561179 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import mapreduce.MapReduce;

public class InvertedIndex {

    public Map<String, List<String>> run(List<String> filenames) {
        List<Document> inputs = new LinkedList<>();
        for (String filename : filenames) {
            inputs.add(new Document(filename, FileParser.parse(filename)));
        }
        // TODO: instantiate a MapReduce object with correct input, key, value, and output types
        MapReduce<Document, String, String, List<String>> mapReduce = new MapReduce<>();

        // TODO: set the mapper and reducer suppliers, and set the inputs
        mapReduce.setMapperSupplier(Mapper::new);
        mapReduce.setReducerSupplier(Reducer::new);
        mapReduce.setInput(inputs);

        // TODO: execute the MapReduce object and return the result
        return mapReduce.call();
    }

    class Document {

        String name;
        List<String> words;

        public Document(String name, List<String> words) {
            this.name = name;
            this.words = words;
        }

    }

    class Mapper
            extends mapreduce.Mapper<Document, String, String> {

        @Override
        public Map<String, String> compute() {
            // TODO: implement the Map function for inverted index
            Map<String, String> map = new HashMap<>();
            for (String word : input.words) {
                map.put(word, input.name);
            }
            return map;
        }

    }

    class Reducer
            extends mapreduce.Reducer<String, String, List<String>> {

        @Override
        public List<String> compute() {
            // TODO: implement the Reduce function for inverted index
            List<String> list = new LinkedList<>();
            list.addAll(valueList);
            return list;
        }
    }

}