package plagiarism;

import plagiarism.filter.CharacterFilter;
import plagiarism.filter.IrrelevantWordsFilter;
import plagiarism.filter.WordSeparatorFilter;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Plagiarism builds a filter chain to read and filter an input text.
 * <p>
 * The filtered data will be stored in this object and the object provides a method to calculate a simple checksum of the input text.
 * <p>
 * This checksum gives information, if the input text might be a plagiarism or not.
 *
 * @author Hendrik Brinkmann
 */
public class Plagiarism {

    private Map<String, Integer> map;
    private File file;

    public Plagiarism(File file) throws IOException {
        this.file = file;
        this.map = new HashMap<>();
        init();
    }

    private void init() throws IOException {

        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        FilterReader filterChain = new IrrelevantWordsFilter(new WordSeparatorFilter(new CharacterFilter(bufferedReader)));

        char[] buffer = new char[256];
        int readChars;

        while ((readChars = filterChain.read(buffer)) != -1) {
            if (readChars != 0) {
                String s = new String(buffer, 0, readChars);
                map.compute(s, (k, v) -> (v == null) ? 1 : ++v);
            }
        }
    }

    public int checksum() {
        return map.keySet()
                .stream()
                .mapToInt(s -> digitSum(s) * map.get(s))
                .sum();
    }

    private int digitSum(String s) {
        return s.chars().sum();
    }

    @Override
    public String toString() {
        return map.toString();
    }
}

@@ -1224,17 +1224,20 @@
  String 
-s
+word
  = new S
@@ -1293,17 +1293,20 @@
 compute(
-s
+word
 , (k, v)
@@ -1472,17 +1472,20 @@
 apToInt(
-s
+word
  -%3E digi
@@ -1489,17 +1489,20 @@
 igitSum(
-s
+word
 ) * map.
@@ -1505,17 +1505,20 @@
 map.get(
-s
+word
 ))%0A     
@@ -1575,17 +1575,20 @@
 (String 
-s
+word
 ) %7B%0A    
@@ -1598,17 +1598,20 @@
  return 
-s
+word
 .chars()

rename vars